Skip to content

fix: catch disabled TLS checks in session calls - #784

Merged
uniqueg merged 4 commits into
devfrom
fix/783-tls-session-calls
Nov 17, 2026
Merged

uniqueg merged 4 commits into
devfrom
fix/783-tls-session-calls

Conversation

@uniqueg

@uniqueg uniqueg commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Summary

Widens the shared semgrep rules in .semgrep/foca.yml, with test cases in .semgrep/foca.py:

  • tls-verification-disabled: verify=False, 0 or "" (also as a constant assigned before) in calls of get, post, put, patch, delete, head, options, request and send on any object, e.g. session.get(url, verify=False); ruff's S501 only checks calls of the requests module. verify=None (the session default) is not flagged.
  • jwt-decode-without-verification: any falsy value (False, None, 0, 0.0, "") for verify_signature or verify, inline, in assigned options and via constants (matched with metavariable-pattern, so semgrep's constant propagation applies).
  • jwt-decode-unsafe-algorithms: now a taint rule. Sources are jwt.get_unverified_header(...) and any string literal none (not where it is subtracted from a set); the sink is algorithms= of decode()/decode_complete(). This catches alg = header["alg"] then algorithms=[alg], lists built from the header, and functions. taint_assume_safe_indexes keeps values looked up by kid (jwks[header["kid"]].algorithm_name) clean. Limits (in the rule comment): taint is tracked within a function only, and an alg from 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-code and root-logger-call in .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.
  • semgrep with the new rules on all tracked files (as the hook): 0 findings; also 0 findings in proTES, proWES, trs-filer and Cloud Registry.
  • 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 except semgrep, 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).
  • Old vs. new rules were compared on extra cases by the reviewer subagent: every origin/dev finding is still reported.
  • No package code changes, so pytest was not rerun.
  • CI: all checks passed (run 44078382266).

Review

Adversarial review in two rounds plus a quality review. Fixed: falsy constants missed by a regex (regression), false positives for kid lookups 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 ignores verify anyway, and PyJWT 1 treated None as "don't verify"; harmless.
  • verify=0.0 in 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.
  • Commit subject names only the TLS rule: it is the issue's title.

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:

  • Expand security rules to detect disabled TLS verification across session and request methods, and to identify broader unsafe JWT verification configurations.
  • Detect JWT algorithms derived from unverified headers or containing the none algorithm through taint analysis.

Bug Fixes:

  • Catch falsy JWT verification settings and TLS verification values, including values assigned through constants, while preserving accepted session defaults and safe key lookups.

Enhancements:

  • Synchronize shared Semgrep rules and tests across FOCA applications.
  • Broaden ast-grep checks for root logger usage and string-valued HTTP status codes.

Documentation:

  • Update contribution guidance and rule comments to document shared security rules and tests.

Tests:

  • Add coverage for falsy JWT options, unsafe algorithm propagation, session TLS calls, and expanded ast-grep patterns.

uniqueg and others added 3 commits November 17, 2026 12:18
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]>
@uniqueg uniqueg self-assigned this Nov 17, 2026
@sourcery-ai

sourcery-ai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Configuration

Widen the shared Semgrep rules to detect falsy JWT verification settings, unsafe JWT algorithm flows, and disabled TLS verification in request and session calls.

  • .semgrep/foca.yml L33-56, L68-92, L117-153 (+2 small edits)

Expand the ast-grep rules for root-logger calls and string status codes so they match the shared FOCA-app conventions.

  • .ast-grep/rules/root-logger-call.yml L7-19
  • .ast-grep/rules/string-status-code.yml L13-18

Documentation

Update the contributing guide to identify the shared FOCA-app rules and tests and explain their suppression conventions.

  • docs/contributing/index.md L443-447

Tests

Add Semgrep fixtures for falsy JWT verification options, unsafe algorithm propagation, and disabled TLS checks, including session calls and constant values.

  • .semgrep/foca.py L50-84, L110-163, L205-239

Add ast-grep rule tests for named/root loggers and parenthesized or prefixed string status codes.

  • .ast-grep/rule-tests/root-logger-call-test.yml L6, L13-15
  • .ast-grep/rule-tests/string-status-code-test.yml L7-13

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (7bc6d9c) to head (f523006).

Additional details and impacted files
@@            Coverage Diff            @@
##               dev      #784   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           75        75           
  Lines        10025     10025           
=========================================
  Hits         10025     10025           
Flag Coverage Δ
test_unit 100.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 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.

@uniqueg
uniqueg marked this pull request as ready for review November 17, 2026 12:12
@sourcery-ai

sourcery-ai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Linked issues

Checked at commit f523006

#783 fix: catch disabled TLS checks in session calls · 2 of 4 met

# Requirement Status Notes
1 Flag disabled TLS verification in the specified request methods on any object, with ruleid/ok tests. ✅ Met The rule covers the named methods and falsy values, and the tests include session calls and a verify=None ok case.
2 Close the JWT gaps for falsy verification options and algorithms derived from an unverified header, with tests. ✅ Met The rule changes and .semgrep/foca.py cases cover falsy verification values, tainted header-derived algorithms, and safe cases.
3 Mirror the Semgrep rules and tests in proTES, proWES, trs-filer and Cloud Registry. 🟡 Partly met The FOCA rules and tests are updated, but the other repositories are not included; the PR says these are follow-up PRs.
4 Port the widened string-status-code and root-logger-call ast-grep rules and tests to trs-filer, proTES and Cloud Registry. ❌ Not addressed The changes update FOCA's ast-grep rules and tests only; the PR says the other repositories will be handled in follow-up PRs.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread .semgrep/foca.yml Resolved
Comment thread .semgrep/foca.yml Resolved
Comment thread .semgrep/foca.yml Resolved
Comment thread .semgrep/foca.yml Resolved
Comment thread .ast-grep/rules/root-logger-call.yml Resolved
Comment thread .semgrep/foca.yml Resolved
Comment thread .ast-grep/rules/string-status-code.yml Resolved
Comment thread .semgrep/foca.py Resolved
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]>
uniqueg added a commit to elixir-cloud-aai/protes that referenced this pull request Nov 17, 2026
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]>
uniqueg added a commit to elixir-cloud-aai/trs-filer that referenced this pull request Nov 17, 2026
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]>
uniqueg added a commit to elixir-cloud-aai/cloud-registry that referenced this pull request Nov 17, 2026
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]>
uniqueg added a commit to elixir-cloud-aai/prowes that referenced this pull request Nov 17, 2026
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]>
@uniqueg
uniqueg merged commit 14afdad into dev Nov 17, 2026
57 of 58 checks passed
@uniqueg
uniqueg deleted the fix/783-tls-session-calls branch November 17, 2026 16:08
uniqueg added a commit to elixir-cloud-aai/prowes that referenced this pull request Nov 17, 2026
* 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]>
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.

fix: catch disabled TLS checks in session calls

1 participant