Repository navigation
fix: catch disabled TLS checks in session calls - #784
Conversation
Widen the shared semgrep rules: - tls-verification-disabled: verify=False or verify=0 in calls of get, post, put, patch, delete, head, options, request and send on any object, e.g. a requests session (ruff's S501 only checks calls of the requests module) - jwt-decode-without-verification: any falsy literal (False, None, 0, 0.0, empty string) for verify_signature or verify, inline and in assigned options - jwt-decode-unsafe-algorithms: taint mode, so that the algorithm `none` and values from jwt.get_unverified_header() are also found when they reach `algorithms` through variables or collections Each change comes with ruleid/ok test cases in .semgrep/foca.py. Closes #783 Co-Authored-By: Claude Opus 5.5 <[email protected]>
- jwt-decode-without-verification, tls-verification-disabled: match the falsy values with metavariable-pattern rather than a regex, so that constants assigned before (VERIFY = False) are found again; the TLS rule also covers verify="" and session.verify = <falsy constant> - jwt-decode-unsafe-algorithms: values looked up with a key from the header (jwks[header["kid"]]) are no longer tainted, and removing "none" from a set sanitizes it; the comment names the limits (intra-procedural, allowlist checks still reported) - More test cases: further falsy values, constants, ok cases, and flows inside functions - ast-grep: take over string-status-code and root-logger-call as widened in proWES (#147), so that the rules stay identical with the apps, as the contributing guide says Co-Authored-By: Claude Opus 5.5 <[email protected]>
A sanitizer for `$X - {"none"}` also cleared the taint of an algorithm
from the token's header ({header["alg"]} - {"none"}). Exclude `none`
literals inside a set difference from the sources instead, and add the
header case as a test.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideConfigurationWiden the shared Semgrep rules to detect falsy JWT verification settings, unsafe JWT algorithm flows, and disabled TLS verification in request and session calls.
Expand the ast-grep rules for root-logger calls and string status codes so they match the shared FOCA-app conventions.
DocumentationUpdate the contributing guide to identify the shared FOCA-app rules and tests and explain their suppression conventions.
TestsAdd Semgrep fixtures for falsy JWT verification options, unsafe algorithm propagation, and disabled TLS checks, including session calls and constant values.
Add ast-grep rule tests for named/root loggers and parenthesized or prefixed string status codes.
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 #784 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 75 75
Lines 10025 10025
=========================================
Hits 10025 10025
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Linked issuesChecked at commit f523006 #783 fix: catch disabled TLS checks in session calls · 2 of 4 met
|
There was a problem hiding this comment.
Hey - I've found 1 security issue, and 7 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/foca.yml" line_range="84" />
<code_context>
+ # header's taint)
+ - patterns:
+ - pattern: '"=~/^[nN][oO][nN][eE]$/"'
+ - pattern-not-inside: "$ALGORITHMS - {...}"
+ pattern-sinks:
+ - patterns:
</code_context>
<issue_to_address>
**Unsafe decoder goes unreported**
When an algorithms set contains `"none"` and a set difference does not remove it, `pattern-not-inside` suppresses the `"none"` source anywhere inside the difference, so the unsafe algorithm list reaches the decode sink without a finding and CI accepts a decoder that can authenticate unsigned tokens.
Change the exclusion so it does not suppress a `"none"` source when the set difference leaves that value in the result.
</issue_to_address>
### Comment 2
<location path=".semgrep/foca.yml" line_range="50-54" />
<code_context>
+ - metavariable-pattern:
+ metavariable: $OFF
+ pattern-either:
+ - pattern: "False"
+ - pattern: "None"
+ - pattern: "0"
+ - pattern: "0.0"
+ - pattern: '""'
# `decode()` and `decode_complete()` of `jwt` and `jwt.PyJWT()` (also
# imported under other names)
</code_context>
<issue_to_address>
**Verification bypass goes unreported**
When `verify` or `verify_signature` is supplied as an empty list or dictionary, `$OFF` does not match empty containers, so Requests certificate checks or PyJWT signature validation are disabled without a finding and the insecure call passes unreported.
Extend the falsey-value patterns to match empty lists and dictionaries for the Requests and PyJWT rules.
Also at `.semgrep/foca.yml:135-137`.
</issue_to_address>
### Comment 3
<location path=".semgrep/foca.yml" line_range="122" />
<code_context>
+ # means the session's default
+ patterns:
+ - pattern-either:
+ - pattern: $SESSION.verify = $OFF
+ # Any function of the requests module
+ - pattern: requests.$FUNCTION(..., verify=$OFF, ...)
</code_context>
<issue_to_address>
**A `None` session setting disables TLS**
When a caller sets `session.verify = None`, when a developer assigns `None` to a requests session's `verify` attribute, requests passes that value to its adapter, which treats it as false and disables certificate verification; the assignment does not match `$OFF`, so Semgrep reports no finding.
Flag `None` for session `.verify` assignments while continuing to treat per-call `verify=None` as the session default.
</issue_to_address>
### Comment 4
<location path=".semgrep/foca.yml" line_range="132-137" />
<code_context>
- pattern: $JWT.$DECODE(..., options=$OPTIONS, ...)
+ # Falsy values, which PyJWT treats alike; patterns rather than a regex,
+ # so that constants assigned before (e.g., `VERIFY = False`) match too
+ - metavariable-pattern:
+ metavariable: $OFF
+ pattern-either:
+ - pattern: "False"
+ - pattern: "None"
+ - pattern: "0"
+ - pattern: "0.0"
+ - pattern: '""'
# `decode()` and `decode_complete()` of `jwt` and `jwt.PyJWT()` (also
# imported under other names)
</code_context>
<issue_to_address>
**Falsy TLS values evade rule**
When a caller passes `verify=0.0` to a requests call or assigns that value to a session's `verify` setting, the `$OFF` patterns omit values such as `0.0`, which requests treats as false when choosing whether to verify certificates; the call therefore disables TLS verification without a Semgrep finding.
Extend the TLS rule to recognize all statically falsy non-`None` values that requests interprets as disabling verification.
</issue_to_address>
### Comment 5
<location path=".ast-grep/rules/root-logger-call.yml" line_range="19" />
<code_context>
constraints:
NAME:
- regex: ^(None|""|'')$
+ regex: ^(None|""|''|"root"|'root')$
METHOD:
regex: ^(debug|info|warning|warn|error|exception|critical|fatal|log)$
</code_context>
<issue_to_address>
**Valid logger calls fail lint**
When code intentionally logs through a logger named `root`, the `NAME` constraint treats `logging.getLogger("root")` as the root logger, so the rule reports valid named-logger calls and can block callers.
Remove the `"root"` and `'root'` alternatives from the `NAME` regex.
</issue_to_address>
### Comment 6
<location path=".semgrep/foca.yml" line_range="126" />
<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>
**Mirror rules in apps**
The change adds the widened Semgrep behavior in FOCA, but does not mirror the rules and tests in proTES, proWES, trs-filer, or Cloud Registry. The PR defers those updates to follow-up PRs.
</issue_to_address>
### Comment 7
<location path=".ast-grep/rules/string-status-code.yml" line_range="13-14" />
<code_context>
any:
- pattern: return $BODY, $CODE
- pattern: return $BODY, $CODE, $HEADERS
+ - pattern: return ($BODY, $CODE)
+ - pattern: return ($BODY, $CODE, $HEADERS)
constraints:
CODE:
</code_context>
<issue_to_address>
**Port rules to apps**
The change widens the ast-grep rule in FOCA, but does not port the widened `string-status-code` and `root-logger-call` rules to trs-filer, proTES, or Cloud Registry. The PR defers those updates to follow-up PRs.
</issue_to_address>
### Comment 8
<location path=".semgrep/foca.py" line_range="72" />
<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
Needs a human reviewer. 8 findings to address first, and this changes static checks for disabled TLS verification and unsafe JWT algorithm handling, not the application’s runtime security behavior. A missed pattern could let a future insecure change pass CI; reverting restores the previous checks, and any code that passed would need to be reviewed and fixed separately.
Blocking findings: .semgrep/foca.yml:84, .semgrep/foca.yml:54, .semgrep/foca.yml:122, .semgrep/foca.yml:137, .ast-grep/rules/root-logger-call.yml:19, and 3 more
From the Sourcery review:
- tls-verification-disabled: also 0.0, [] and {} in calls, and None
as the session's setting (requests then skips the check; None in a
call still means the session's default)
- jwt-decode-without-verification: also [] and {}
- jwt-decode-unsafe-algorithms: report `none` also inside a set
difference; the exclusion hid `{"RS256", "none"} - {"NONE"}`, since
it can't tell which operand holds the literal. Subtracting `none`
from a set is reported, too (use a fixed list instead)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
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]>
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]>
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]>
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]>
* fix: catch disabled TLS checks in session calls 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]> * fix: close falsy and set difference gaps 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]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
Summary
Widens the shared semgrep rules in
.semgrep/foca.yml, with test cases in.semgrep/foca.py:tls-verification-disabled:verify=False,0or""(also as a constant assigned before) in calls ofget,post,put,patch,delete,head,options,requestandsendon any object, e.g.session.get(url, verify=False); ruff's S501 only checks calls of therequestsmodule.verify=None(the session default) is not flagged.jwt-decode-without-verification: any falsy value (False,None,0,0.0,"") forverify_signatureorverify, inline, in assigned options and via constants (matched withmetavariable-pattern, so semgrep's constant propagation applies).jwt-decode-unsafe-algorithms: now a taint rule. Sources arejwt.get_unverified_header(...)and any string literalnone(not where it is subtracted from a set); the sink isalgorithms=ofdecode()/decode_complete(). This catchesalg = header["alg"]thenalgorithms=[alg], lists built from the header, and functions.taint_assume_safe_indexeskeeps values looked up bykid(jwks[header["kid"]].algorithm_name) clean. Limits (in the rule comment): taint is tracked within a function only, and analgfrom the header checked against an allowlist is still reported (pass the allowlist itself).The rules and their tests are shared with proTES, proWES, trs-filer and Cloud Registry (follow-up PRs there, see below), so the header comment and the contributing guide now say so. To keep them identical with the apps,
string-status-codeandroot-logger-callin.ast-grep/take over the versions widened in elixir-cloud-aai/prowes#147 (returned tuples in parentheses, prefixed strings;logging.root,getLogger(name=None),getLogger("root")), which find nothing in FOCA.Closes #783
Verification
cd .semgrep && semgrep --test .(semgrep 1.183.0, as pinned): 5/5 passed.ast-grep test --skip-snapshot-tests: 9/9 passed;ast-grep scan --error=flask-import: no findings.pre-commit run --all-files: all hooks pass exceptsemgrep, which fails locally only because semgrep-core can't allocate io_uring queues when pre-commit runs it in parallel; the same scan run serially passes (CI runs it serially).Review
Adversarial review in two rounds plus a quality review. Fixed: falsy constants missed by a regex (regression), false positives for
kidlookups and- {"none"}, a sanitizer that also cleared the header's taint, more ok/ruleid cases and function-level tests.Declined review findings
jwt.decode(..., verify=None)is flagged although it is PyJWT 2's default: PyJWT 2 ignoresverifyanyway, and PyJWT 1 treatedNoneas "don't verify"; harmless.verify=0.0in session calls is not caught by the TLS rule: nobody writes that.requests.$FUNCTION(..., verify=$OFF)overlaps with the method pattern: it also covers other functions of the module; kept, with a comment.Sourcery
Sourcery's check may fail on the deliberately insecure rule-test fixtures in
.semgrep/foca.py(accepted before for trs-filer #149 and proWES #147).🤖 Generated with Claude Code
Summary by Sourcery
Strengthen shared security checks for disabled TLS verification and unsafe JWT decoding while aligning related static-analysis rules across FOCA applications.
New Features:
nonealgorithm through taint analysis.Bug Fixes:
Enhancements:
Documentation:
Tests: