Skip to content

Conversation

lunkwill42
Copy link
Member

A device that sends traps with non-Ascii community values would cause trap PDU processing to crash with a UnicodeDecodeError.

Community values are defined as ASCII in the SNMP specs, but some devices do not behave. netsnmp-cffi tries to be helpful to the client by automatically making a Python string from the community, but if there's a misbehaving device, this shouldn't break everything. This PR falls back to storing the raw bytes objects in the trap data structure if it cannot be successfully decoded as ASCII.

Copy link

github-actions bot commented Sep 4, 2025

🦙 MegaLinter status: ✅ SUCCESS

Descriptor Linter Files Fixed Errors Warnings Elapsed time
✅ PYTHON black 2 0 0 0.69s
✅ PYTHON isort 2 0 0 0.28s
✅ PYTHON ruff 2 0 0 0.16s

See detailed report in MegaLinter reports
Set VALIDATE_ALL_CODEBASE: true in mega-linter.yml to validate all sources, not only the diff

MegaLinter is graciously provided by OX Security

@lunkwill42 lunkwill42 force-pushed the bugfix/handle-garbage-community branch from 864a7ef to 85972d7 Compare September 4, 2025 11:24
@lunkwill42 lunkwill42 self-assigned this Sep 4, 2025
@lunkwill42 lunkwill42 added the bug Something isn't working label Sep 4, 2025
@lunkwill42 lunkwill42 requested a review from a team September 4, 2025 11:24
Copy link

github-actions bot commented Sep 4, 2025

Test results

  4 files    4 suites   40s ⏱️
 25 tests  25 ✅ 0 💤 0 ❌
100 runs  100 ✅ 0 💤 0 ❌

Results for commit 7bc9d71.

♻️ This comment has been updated with latest results.

@codecov-commenter
Copy link

codecov-commenter commented Sep 4, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.62%. Comparing base (4120d26) to head (7bc9d71).

Additional details and impacted files
@@            Coverage Diff             @@
##           master       #9      +/-   ##
==========================================
+ Coverage   72.05%   77.62%   +5.56%     
==========================================
  Files          10       10              
  Lines        1256     1260       +4     
==========================================
+ Hits          905      978      +73     
+ Misses        351      282      -69     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lunkwill42 lunkwill42 force-pushed the bugfix/handle-garbage-community branch 2 times, most recently from 7b5f2e6 to 6073f43 Compare September 4, 2025 11:36
A device that sends traps with non-ascii community values will cause
trap PDU processing to crash with a `UnicodeDecodeError`. community
values are defined as ASCII in the SNMP specs, but some devices do not
behave. netsnmp-cffi tries to be helpful to the client by automatically
making a Python string from the community, but if there's a misbehaving
device, this should not break everything.
If the community value of an incoming trap cannot be parsed as ASCII,
we take care to just leave the raw bytes object for the client to
inspect for themselves.
@lunkwill42 lunkwill42 force-pushed the bugfix/handle-garbage-community branch from 6073f43 to 7bc9d71 Compare September 4, 2025 12:52
@lunkwill42 lunkwill42 marked this pull request as ready for review September 4, 2025 12:55
@lunkwill42 lunkwill42 merged commit 0bc294b into master Sep 8, 2025
8 checks passed
@lunkwill42 lunkwill42 deleted the bugfix/handle-garbage-community branch September 8, 2025 15:18
@he32
Copy link

he32 commented Sep 8, 2025

Community values are defined as ASCII in the SNMP specs

Really? RFC 1902 (yes, obsoleted), has

        Message ::=
                SEQUENCE {
                     version
                        INTEGER {
                            version(1)  -- modified from RFC 1157
                        },

                    community           -- community name
                        OCTET STRING,

                    data                -- PDUs as defined in [4]
                        ANY
                }
        }

[...] but some devices do not behave.

I would perhaps not have said it that way. Instead, I would have said that "it is a bad assumption to insist that everyone configuring SNMP community strings input only utf-8 valid sequences in the community string".

Other than that, I think this change does the right thing and no longer insist on conformance to utf-8 coding, and instead just looks at this as an octet string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants