Repository navigation
fix: catch disabled TLS checks in session calls - #149
Conversation
Take over FOCA's shared semgrep rules as widened in elixir-cloud-aai/foca#784 (identical apart from `paths`), with their tests: - tls-verification-disabled: also calls on sessions, e.g. session.get(url, verify=False), and any falsy constant - jwt-decode-without-verification: any falsy value (e.g. None), also through constants - jwt-decode-unsafe-algorithms: taint mode, so that the header's alg is also found through variables The rules find nothing in the code. Part of elixir-cloud-aai/foca#783 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideConfigurationBroaden the Semgrep rules to detect falsy TLS and JWT verification settings, and use taint analysis to catch unsafe JWT algorithms propagated through variables and collections.
TestsUpdate the Semgrep fixtures to cover the expanded falsy-value cases, session-based TLS calls, and unsafe JWT algorithms passed through variables or collections, alongside safe cases.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #149 +/- ##
=======================================
Coverage 81.55% 81.55%
=======================================
Files 16 16
Lines 694 694
=======================================
Hits 566 566
Misses 128 128 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Take over the changes of FOCA's shared semgrep rules from the review of elixir-cloud-aai/foca#784: - tls-verification-disabled: also 0.0, [] and {} in calls, and None as the session's setting - jwt-decode-without-verification: also [] and {} - jwt-decode-unsafe-algorithms: report `none` also inside a set difference Co-Authored-By: Claude Opus 5.5 <[email protected]>
Linked issuesChecked at commit 2fc7289 #150 fix: catch disabled TLS checks in session calls · 2 of 3 met · 1 to check by hand
|
There was a problem hiding this comment.
Hey - I've found 1 security issue, and 9 other issues
Security issues:
- Detected JWT token decoded with 'verify=False'. This bypasses any integrity checks for the token which means the token could be tampered with by malicious actors. Ensure that the JWT token is verified. (link)
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path=".semgrep/pro-wes.yml" line_range="86" />
<code_context>
+ - pattern: '"=~/^[nN][oO][nN][eE]$/"'
+ pattern-sinks:
+ - patterns:
+ - pattern: $JWT.$DECODE(..., algorithms=$ALGORITHMS, ...)
+ - metavariable-regex:
+ metavariable: $DECODE
</code_context>
<issue_to_address>
**Positional algorithms evade findings**
When an unsafe algorithm list is passed as the third positional argument to `decode()` or `decode_complete()`, the `pattern-sinks` pattern matches only `algorithms=$ALGORITHMS`, so the positional value reaches PyJWT without a finding and the caller receives no security warning.
Extend the sink patterns to match the algorithms argument when it is passed positionally to `decode()` and `decode_complete()`.
</issue_to_address>
### Comment 2
<location path=".semgrep/pro-wes.yml" line_range="75" />
<code_context>
+ options:
+ # Values looked up with a tainted key (e.g., `jwks[header["kid"]]`)
+ # come from the server, not from the token
+ taint_assume_safe_indexes: true
+ pattern-sources:
+ # The unverified header, and anything taken from it
</code_context>
<issue_to_address>
**Indexed algorithms evade findings**
When a header-derived value indexes a mapping whose returned algorithm value is attacker-controlled, `taint_assume_safe_indexes: true` treats indexed lookups as untainted, so the returned algorithm does not reach the sink and Semgrep reports no finding.
Limit the safe-index assumption to server-provided JWKS lookups while preserving taint for lookups keyed by unverified header values.
</issue_to_address>
### Comment 3
<location path=".semgrep/pro-wes.yml" line_range="78" />
<code_context>
+ taint_assume_safe_indexes: true
+ pattern-sources:
+ # The unverified header, and anything taken from it
+ - pattern: jwt.get_unverified_header(...)
+ # Any string literal `none`, in any case, also where it is removed from
+ # a set (e.g., `algorithms - {"none"}`, reported too): an exception
</code_context>
<issue_to_address>
**Instance headers evade findings**
When a caller reads the unverified header through `jwt.PyJWS().get_unverified_header()` or `jwt.PyJWT().get_unverified_header()`, the source pattern matches only `jwt.get_unverified_header(...)`, so an algorithm read from either instance method does not reach the sink as tainted and the unsafe decode receives no finding.
Add taint sources for the `PyJWS` and `PyJWT` instance methods that return unverified headers.
</issue_to_address>
### Comment 4
<location path=".semgrep/pro-wes.yml" line_range="68" />
<code_context>
- regex: ^decode(_complete)?$
+ # Taint mode, so that values reach `algorithms` also through variables
+ # (e.g., `alg = header["alg"]`) and collections. Taint is tracked within
+ # a function only (not into helpers, nor from module constants into
+ # functions). An `alg` from the header that is checked against an
+ # allowlist is still reported: pass the allowlist itself instead
</code_context>
<issue_to_address>
**Helper-returned algorithms evade findings**
When a helper extracts an algorithm from an unverified header and another function passes it to `decode()`, the taint analysis does not carry the header-derived value into the caller, so the algorithm does not reach the sink and the unsafe decode receives no finding.
Make the rule detect header-derived algorithms passed through helper return values to decode calls.
</issue_to_address>
### Comment 5
<location path=".semgrep/pro-wes.yml" line_range="54" />
<code_context>
+ - pattern: "0.0"
+ - pattern: '""'
+ - pattern: "[]"
+ - pattern: "{}"
# `decode()` and `decode_complete()` of `jwt` and `jwt.PyJWT()` (also
# imported under other names)
</code_context>
<issue_to_address>
**Empty verification values evade findings**
When a verification setting uses an unlisted falsy literal such as `b""` or `()`, the JWT and TLS patterns match only enumerated falsy forms, so PyJWT or Requests can disable signature or certificate verification without a finding.
Extend the verification patterns to match the additional falsy literals that disable verification, including empty bytes and empty tuples.
Also at `.semgrep/pro-wes.yml:122-132`, `.semgrep/pro-wes.yml:143-151`.
</issue_to_address>
### Comment 6
<location path=".semgrep/pro-wes.yml" line_range="137" />
<code_context>
+ # Any function of the requests module
+ - pattern: requests.$FUNCTION(..., verify=$OFF, ...)
+ - patterns:
+ - pattern: $X.$METHOD(..., verify=$OFF, ...)
+ # The request methods of requests' sessions (and of the
+ # module, and of similar clients)
</code_context>
<issue_to_address>
**Unpacked TLS bypass evades rule**
When an application developer calls a requests session method with `request_options = {"verify": False}` and passes `**request_options`, the request accepts the mapping through `**request_options` and disables certificate verification, but this rule only matches an explicit `verify=$OFF` argument. The insecure request therefore runs without a Semgrep finding.
Add rule coverage for disabled verification supplied through unpacked keyword mappings, with a regression fixture.
</issue_to_address>
### Comment 7
<location path=".semgrep/pro-wes.yml" line_range="142" />
<code_context>
+ # module, and of similar clients)
+ - metavariable-regex:
+ metavariable: $METHOD
+ regex: ^(get|post|put|patch|delete|head|options|request|send)$
+ - metavariable-pattern:
+ metavariable: $OFF
</code_context>
<issue_to_address>
**Explicit None disables TLS checks**
When a Requests session calls `send(..., verify=None)`, `Session.send` preserves an explicitly supplied `verify=None` rather than substituting the session default, and Requests treats that falsy value as disabling certificate verification; the rule omits `None` from its call-value patterns, so this unsafe call receives no finding.
Treat `verify=None` as disabled for `send` calls, or handle `send` separately from request methods where `None` inherits the session setting.
</issue_to_address>
### Comment 8
<location path=".semgrep/pro-wes.yml" line_range="137" />
<code_context>
+ # Any function of the requests module
+ - pattern: requests.$FUNCTION(..., verify=$OFF, ...)
+ - patterns:
+ - pattern: $X.$METHOD(..., verify=$OFF, ...)
+ # The request methods of requests' sessions (and of the
+ # module, and of similar clients)
</code_context>
<issue_to_address>
**Unrelated methods get flagged**
When code calls an unrelated object's listed method with a falsy parameter named `verify`, the TLS rule matches any receiver's `get`, `post`, `put`, and other listed methods with a falsy `verify` argument, regardless of the receiver's type or whether `verify` controls TLS; unrelated APIs therefore produce security findings.
Restrict the receiver pattern to known TLS clients or add an explicit allowlist of supported client APIs.
</issue_to_address>
### Comment 9
<location path=".semgrep/pro-wes.yml" line_range="147-151" />
<code_context>
+ pattern-either:
+ - pattern: "False"
+ - pattern: "None"
+ - pattern: "0"
+ - pattern: "0.0"
+ - pattern: '""'
+ - pattern: "[]"
+ - pattern: "{}"
# `decode()` and `decode_complete()` of `jwt` and `jwt.PyJWT()` (also
# imported under other names)
</code_context>
<issue_to_address>
**Safe TLS settings are rejected**
When a caller passes a falsy value other than `False`, or assigns `None` to a session’s `verify` setting, the rule reports non-`False` falsy values as disabled verification, but Requests disables certificate checks only when `verify is False`; values such as `0`, `[]`, and `{}` still use the default CA verification, while `verify=None` inherits the session default. Since CI runs Semgrep with `--error`, those safe settings produce a failing check for callers.
Restrict the rule to values Requests actually treats as disabling verification, accounting for `None` as the session default.
Also at `.semgrep/pro-wes.yml:126-131`.
</issue_to_address>
### Comment 10
<location path=".semgrep/pro-wes.py" line_range="76" />
<code_context>
jwt.decode(TOKEN, options={"verify_signature": VERIFY})
</code_context>
<issue_to_address>
**security (python.jwt.security.unverified-jwt-decode):** Detected JWT token decoded with 'verify=False'. This bypasses any integrity checks for the token which means the token could be tampered with by malicious actors. Ensure that the JWT token is verified.
```suggestion
jwt.decode(TOKEN, options={"verify_signature": True})
```
*Source: opengrep*
</issue_to_address>Sourcery assessment
Approval pending. 10 findings to address first.
Blocking findings: .semgrep/pro-wes.yml:86, .semgrep/pro-wes.yml:75, .semgrep/pro-wes.yml:78, .semgrep/pro-wes.yml:68, .semgrep/pro-wes.yml:54, and 5 more
Summary
Takes over FOCA's shared semgrep rules as widened in elixir-cloud-aai/foca#784, identical apart from
paths, with their tests:tls-verification-disabled: also calls on sessions (session.get(url, verify=False)), which ruff's S501 misses, and falsy constantsjwt-decode-without-verification: any falsy value (alsoNone), also through constantsjwt-decode-unsafe-algorithms: taint mode, so that the token header'salgornoneis also found when it reachesalgorithmsthrough variablesThe rules find nothing in the code.
Closes #150
Part of elixir-cloud-aai/foca#783
Verification
cd .semgrep && semgrep --test .: 5/5 passed.ast-grep test --skip-snapshot-tests: 8/8 passed;ast-grep scan: no findings.pre-commit run --all-files: ruff, the rule tests and the file checks pass; thesemgrephook can fail locally only because semgrep-core can't allocate io_uring queues when pre-commit runs it in parallel (the same scan run serially passes), and actionlint lacks a local shellcheck. CI covers both.paths, tests equal FOCA's apart from the file name and import order.CI passedincluded.Sourcery
Sourcery's check may fail on the deliberately insecure rule-test fixtures in
.semgrep/(accepted before for trs-filer #149 and proWES #147);.sourcery.yamlalready ignores them.🤖 Generated with Claude Code
Summary by Sourcery
Strengthen Semgrep security checks for TLS certificate verification and JWT validation bypasses.
Bug Fixes:
Enhancements:
Tests: