Skip to content

Implement CBOR parser following ASN.1 paradigm with full test coverage#23

Open
Copilot wants to merge 23 commits intomasterfrom
copilot/implement-cbor-parser-asn1
Open

Implement CBOR parser following ASN.1 paradigm with full test coverage#23
Copilot wants to merge 23 commits intomasterfrom
copilot/implement-cbor-parser-asn1

Conversation

Copy link

Copilot AI commented Feb 9, 2026

Overview

Implements RFC 8949 CBOR (Concise Binary Object Representation) parser and encoder in Scapy, following the same architectural pattern as ASN.1 (separation of type system from codec).

Implementation

Core Components (scapy/cbor/)

  • cbor.py: Type system with CBOR_Object base class, major type objects (0-7), and CBOR_MajorTypes registry using metaclass pattern
  • cborcodec.py: Codec with CBORcodec_Object encoders/decoders, including IEEE 754 half-precision float conversion
  • RandCBORObject: Fuzzing support with gaussian distribution for integers, exponential sizing for collections, depth limiting for nested structures

Type Coverage

  • Integers (unsigned/negative), byte/text strings (UTF-8), arrays, maps, semantic tags
  • Simple values (true, false, null, undefined)
  • Floats (16/32/64-bit IEEE 754)

Testing

Test Suite (test/scapy/layers/cbor.uts)

  • 161 total tests: 119 core tests + 42 cbor2 interoperability tests
  • Roundtrip encoding/decoding validation
  • RFC 8949 compliance verification (specific hex encodings, boundary values)
  • Stress tests with random generation (100+ objects)
  • Performance optimized: auto-switch to simple types in recursive structures to prevent exponential nesting

Interoperability

  • Bidirectional compatibility with cbor2 library (test-only dependency)
  • Both Scapy→cbor2 and cbor2→Scapy tested

Platform Fixes

Windows UTF-8 handling (scapy/tools/UTscapy.py)

# Fixed file opens to use UTF-8 explicitly
with open(test_file, encoding='utf-8') as f:  # Was: open(test_file)

Windows defaults to cp1252, causing 'café' to misread as 'café'.

UTScapy test format

  • Added blank lines between compound statements and assertions for Python's interactive parser

Documentation

Added CBOR section to doc/scapy/advanced_usage.rst mirroring ASN.1 structure with usage examples, type descriptions, and interoperability notes.

Dependencies

Added cbor2 to tox.ini test dependencies for interoperability validation (used only in tests, not in implementation).

Example Usage

from scapy.cbor import *

# Create and encode
obj = CBOR_MAP({
    'key': CBOR_TEXT_STRING('value'),
    'number': CBOR_UNSIGNED_INTEGER(42)
})
encoded = bytes(obj)

# Decode
decoded, remainder = CBOR_Codecs.CBOR.dec(encoded)

# Fuzzing
rand_obj = RandCBORObject()._fix()

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits February 9, 2026 19:41
Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
…oper half-float decoding

Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement CBOR parser following ASN.1 paradigm Implement CBOR parser following ASN.1 paradigm Feb 9, 2026
Copilot AI requested a review from polybassa February 9, 2026 19:47
Copilot stopped work on behalf of polybassa due to an error February 11, 2026 19:44
Copilot AI and others added 2 commits February 11, 2026 19:48
Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
Copilot AI changed the title Implement CBOR parser following ASN.1 paradigm Add cbor2 interoperability tests for CBOR parser validation Feb 11, 2026
Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
Copilot AI changed the title Add cbor2 interoperability tests for CBOR parser validation Add CBOR documentation to advanced_usage.rst Feb 11, 2026
Copilot AI changed the title Add CBOR documentation to advanced_usage.rst Implement RFC 8949 CBOR parser following ASN.1 architecture Feb 11, 2026
@polybassa polybassa marked this pull request as ready for review February 11, 2026 20:22
Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
Copilot AI changed the title Implement RFC 8949 CBOR parser following ASN.1 architecture Add adapted CBOR tests from PR #4875 for RFC 8949 compliance validation Feb 11, 2026
Copilot AI requested a review from polybassa February 11, 2026 20:44
… file opens

Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
Copilot AI changed the title Add adapted CBOR tests from PR #4875 for RFC 8949 compliance validation Fix UTF-8 test failures on Windows by specifying encoding in file opens Feb 11, 2026
… section

Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
Copilot AI changed the title Fix UTF-8 test failures on Windows by specifying encoding in file opens Remove trailing whitespace in CBOR documentation causing Sphinx build warning Feb 11, 2026
Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
Copilot AI changed the title Remove trailing whitespace in CBOR documentation causing Sphinx build warning Add RandCBORObject for fuzzing following ASN.1 paradigm Feb 13, 2026
…re generation

Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
Copilot AI changed the title Add RandCBORObject for fuzzing following ASN.1 paradigm Fix exponential complexity in CBOR fuzzing test generation Feb 13, 2026
…ertions

Co-authored-by: polybassa <1676055+polybassa@users.noreply.github.com>
Copilot AI changed the title Fix exponential complexity in CBOR fuzzing test generation Implement CBOR parser following ASN.1 paradigm with full test coverage Feb 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants