go-coap icon indicating copy to clipboard operation
go-coap copied to clipboard

PUT with unknown critical options #99/#13 updates /store instead of returning 4.02 Bad Option

Open gahssi opened this issue 5 months ago • 0 comments

Summary

I found that go-coap accepts Confirmable PUT requests carrying unknown critical options and applies the PUT payload to /store instead of returning 4.02 Bad Option.

I observed this with two closely related raw CoAP sequences:

  1. a PUT carrying unknown critical option number 99;
  2. a PUT carrying unknown critical option number 13 after a Content-Format option.

In both cases, go-coap returned 2.04 Changed. A subsequent GET returned marker value B, showing that the PUT carrying the unknown critical option changed /store.

I understand that CoAP libraries may need to support application-specific critical options, and that blindly rejecting all unfamiliar critical options at the wrong layer could break handlers that intentionally process them. The concern in this test is narrower: the request appears to reach the application handler and mutate resource state even though the critical option is not recognized or handled by the tested /store resource.

Relevant RFC text

RFC 7252, Section 5.4.1, says:

Unrecognized options of class "critical" that occur in a Confirmable request MUST cause the return of a 4.02 (Bad Option) response.

The same section says the response should include a diagnostic payload describing the unrecognized option(s). RFC 7252, Section 5.9.2.3, defines 4.02 Bad Option as the response code for a request that could not be understood because of unrecognized or malformed options.

Tested version

  • Version/provenance observed locally: 3.5.1
  • Transport: CoAP over UDP, local test server
  • Resource used by the test: /store

Expected behavior

Because each probe is a Confirmable PUT containing an unrecognized critical option, I expected the server to return 4.02 Bad Option and leave /store at marker value A.

Observed behavior

Reproducer A: unknown critical option number 99

Step Response observed from go-coap Response payload Raw response bytes
setup-put-store-a ACK 2.04 empty 63446f1066f301
unknown-critical-option-99-probe ACK 2.04 empty 63446f1166f302
fresh-get-store-readout ACK 2.05 marker value B 63456f1266f3ffc0ff61746c61732d637269746963616c2d42

Reproducer B: unknown critical option number 13 after Content-Format

Step Response observed from go-coap Response payload Raw response bytes
setup-put-store-a ACK 2.04 empty 63446f1066f301
unknown-critical-option-13-after-content-format-probe ACK 2.04 empty 63446f1166f302
fresh-get-store-readout ACK 2.05 marker value B 63456f1266f3ffc0ff61746c61732d637269746963616c2d42

The final GET response contains atlas-critical-B, which means the edge-case PUT changed /store.

Request packet sequences

Reproducer A: unknown critical option number 99

Step Purpose CoAP fields Raw request bytes
setup-put-store-a Create/update /store with marker value A. PUT; MID 28432; Token 66f301; path /store; payload atlas-critical-A 43036f1066f301b573746f726510ff61746c61732d637269746963616c2d41
unknown-critical-option-99-probe Attempt to update /store with marker value B while carrying the unknown critical option. PUT; MID 28433; Token 66f302; path /store; payload atlas-critical-B; unknown critical option number 99 with value 01 43036f1166f302b573746f726510d14a01ff61746c61732d637269746963616c2d42
fresh-get-store-readout Read /store using a fresh GET request. GET; MID 28434; Token 66f3ff; path /store 43016f1266f3ffb573746f7265

Reproducer B: unknown critical option number 13 after Content-Format

Step Purpose CoAP fields Raw request bytes
setup-put-store-a Create/update /store with marker value A. PUT; MID 28432; Token 66f301; path /store; payload atlas-critical-A 43036f1066f301b573746f726510ff61746c61732d637269746963616c2d41
unknown-critical-option-13-after-content-format-probe Attempt to update /store with marker value B while carrying the unknown critical option. PUT; MID 28433; Token 66f302; path /store; payload atlas-critical-B; Content-Format option followed by unknown critical option number 13 with value 01 43036f1166f302b573746f7265101101ff61746c61732d637269746963616c2d42
fresh-get-store-readout Read /store using a fresh GET request. GET; MID 28434; Token 66f3ff; path /store 43016f1266f3ffb573746f7265

Cross-implementation comparison

Implementation Version Option #99 probe Option #99 final /store Option #13 variant probe Option #13 variant final /store
libcoap 4.3.5b 4.02 Bad Option marker value A 4.02 Bad Option marker value A
Californium 3.14.0 4.02 Bad Option marker value A 4.02 Bad Option marker value A
aiocoap 0.4.17 2.04 Changed marker value B 4.02 Bad Option marker value A
go-coap 3.5.1 2.04 Changed marker value B 2.04 Changed marker value B

The option #99 case shows that go-coap and aiocoap both accepted the PUT. The option #13 ordering variant was go-coap-specific in this comparison: libcoap, Californium, and aiocoap rejected the same request with 4.02 Bad Option.

Minimal reproduction

I used a small raw UDP reproducer. Run one variant at a time against the local go-coap server:

python3 reproduce_raw_coap_sequence.py --host 127.0.0.1 --port 56834 --variant option99
python3 reproduce_raw_coap_sequence.py --host 127.0.0.1 --port 56834 --variant option13
reproduce_raw_coap_sequence.py
from __future__ import annotations

import argparse
import socket
import time

PACKETS = {
    "option99": [
        (
            "setup-put-store-a",
            "43036f1066f301b573746f726510ff61746c61732d637269746963616c2d41",
        ),
        (
            "unknown-critical-option-99-probe",
            "43036f1166f302b573746f726510d14a01ff61746c61732d637269746963616c2d42",
        ),
        (
            "fresh-get-store-readout",
            "43016f1266f3ffb573746f7265",
        ),
    ],
    "option13": [
        (
            "setup-put-store-a",
            "43036f1066f301b573746f726510ff61746c61732d637269746963616c2d41",
        ),
        (
            "unknown-critical-option-13-after-content-format-probe",
            "43036f1166f302b573746f7265101101ff61746c61732d637269746963616c2d42",
        ),
        (
            "fresh-get-store-readout",
            "43016f1266f3ffb573746f7265",
        ),
    ],
}


def coap_type_name(type_id: int) -> str:
    return {0: "CON", 1: "NON", 2: "ACK", 3: "RST"}.get(type_id, f"type-{type_id}")


def coap_code_name(code_byte: int) -> str:
    klass = code_byte >> 5
    detail = code_byte & 0x1F
    return f"{klass}.{detail:02d}"


def payload_from_response(raw: bytes) -> str:
    if len(raw) < 4:
        return ""

    token_length = raw[0] & 0x0F
    index = 4 + token_length

    while index < len(raw):
        current = raw[index]

        if current == 0xFF:
            payload = raw[index + 1 :]
            try:
                return payload.decode("utf-8")
            except UnicodeDecodeError:
                return payload.hex()

        index += 1
        option_delta = current >> 4
        option_length = current & 0x0F

        if option_delta == 13:
            index += 1
        elif option_delta == 14:
            index += 2

        if option_length == 13:
            if index >= len(raw):
                return ""
            option_length = raw[index] + 13
            index += 1
        elif option_length == 14:
            if index + 1 >= len(raw):
                return ""
            option_length = int.from_bytes(raw[index : index + 2], "big") + 269
            index += 2
        elif option_length == 15:
            return ""

        index += option_length

    return ""


def main() -> int:
    parser = argparse.ArgumentParser(description="Replay raw CoAP datagrams for this issue")
    parser.add_argument("--host", default="127.0.0.1", help="target host/IP")
    parser.add_argument("--port", type=int, default=56834, help="target UDP port")
    parser.add_argument(
        "--variant",
        choices=sorted(PACKETS),
        required=True,
        help="which raw packet sequence to replay",
    )
    parser.add_argument("--timeout", type=float, default=2.0, help="receive timeout per packet")
    parser.add_argument("--delay", type=float, default=0.15, help="delay between packets")
    args = parser.parse_args()

    addr = (args.host, args.port)
    print(f"target={args.host}:{args.port}")
    print(f"variant={args.variant}")

    with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
        sock.settimeout(args.timeout)

        for step, hex_bytes in PACKETS[args.variant]:
            raw = bytes.fromhex(hex_bytes)
            print(f"\n>>> {step}")
            print(f"sent_hex={hex_bytes}")

            sock.sendto(raw, addr)

            try:
                response, peer = sock.recvfrom(4096)
            except socket.timeout:
                print("response=timeout")
            else:
                msg_type = coap_type_name((response[0] >> 4) & 0x03) if response else "parse-error"
                code = coap_code_name(response[1]) if len(response) >= 2 else "parse-error"
                payload = payload_from_response(response)

                print(f"from={peer[0]}:{peer[1]}")
                print(f"response_type={msg_type}")
                print(f"response_code={code}")
                print(f"response_payload={payload!r}")
                print(f"response_hex={response.hex()}")

            time.sleep(args.delay)

    return 0


if __name__ == "__main__":
    raise SystemExit(main())

Recorded output: option #99

target=127.0.0.1:56834
variant=option99

>>> setup-put-store-a
sent_hex=43036f1066f301b573746f726510ff61746c61732d637269746963616c2d41
from=127.0.0.1:56834
response_type=ACK
response_code=2.04
response_payload=''
response_hex=63446f1066f301

>>> unknown-critical-option-99-probe
sent_hex=43036f1166f302b573746f726510d14a01ff61746c61732d637269746963616c2d42
from=127.0.0.1:56834
response_type=ACK
response_code=2.04
response_payload=''
response_hex=63446f1166f302

>>> fresh-get-store-readout
sent_hex=43016f1266f3ffb573746f7265
from=127.0.0.1:56834
response_type=ACK
response_code=2.05
response_payload='atlas-critical-B'
response_hex=63456f1266f3ffc0ff61746c61732d637269746963616c2d42

Recorded output: option #13 after Content-Format

target=127.0.0.1:56834
variant=option13

>>> setup-put-store-a
sent_hex=43036f1066f301b573746f726510ff61746c61732d637269746963616c2d41
from=127.0.0.1:56834
response_type=ACK
response_code=2.04
response_payload=''
response_hex=63446f1066f301

>>> unknown-critical-option-13-after-content-format-probe
sent_hex=43036f1166f302b573746f7265101101ff61746c61732d637269746963616c2d42
from=127.0.0.1:56834
response_type=ACK
response_code=2.04
response_payload=''
response_hex=63446f1166f302

>>> fresh-get-store-readout
sent_hex=43016f1266f3ffb573746f7265
from=127.0.0.1:56834
response_type=ACK
response_code=2.05
response_payload='atlas-critical-B'
response_hex=63456f1266f3ffc0ff61746c61732d637269746963616c2d42

Notes

I have not inferred a root cause from this test. I am sharing the raw packet sequences and cross-implementation comparison so maintainers can confirm whether the behavior is intended for this implementation.

gahssi avatar Jun 23 '26 06:06 gahssi