Skip to content

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

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

uniqueg merged 2 commits into
devfrom
fix/783-session-tls-rules

Conversation

@uniqueg

@uniqueg uniqueg commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

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 constants
  • jwt-decode-without-verification: any falsy value (also None), also through constants
  • jwt-decode-unsafe-algorithms: taint mode, so that the token header's alg or none is also found when it reaches algorithms through variables

The rules find nothing in the code.

Closes #150

Part of elixir-cloud-aai/foca#783

Verification

  • cd .semgrep && semgrep --test .: 5/5 passed.
  • semgrep with the new rules on all tracked files: 0 findings.
  • 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; the semgrep hook 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.
  • pytest: 63 passed.
  • Mirror review (adversarial subagent): rule files equal FOCA's apart from the header and paths, tests equal FOCA's apart from the file name and import order.
  • CI: all checks pass, CI passed included.

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.yaml already ignores them.

🤖 Generated with Claude Code

Summary by Sourcery

Strengthen Semgrep security checks for TLS certificate verification and JWT validation bypasses.

Bug Fixes:

  • Expand security rules to detect disabled TLS verification in requests session calls and additional falsy values.
  • Detect JWT signature verification bypasses and unsafe algorithms when insecure values flow through variables or collections.

Enhancements:

  • Align Semgrep rules and fixtures with the widened FOCA security checks.

Tests:

  • Extend Semgrep fixtures to cover session-based TLS bypasses, falsy verification settings, and tainted JWT algorithms.

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]>
@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

Broaden 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.

  • .semgrep/pro-wes.yml L31-54, L66-90, L115-151 (+1 small edits)

Tests

Update 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.

  • .semgrep/pro-wes.py L47-84, L110-163, L205-239 (+3 small edits)

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 81.55%. Comparing base (d7b8a59) to head (2fc7289).
⚠️ Report is 6 commits behind head on dev.

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.
📢 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.

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 marked this pull request as ready for review November 17, 2026 16:50
@sourcery-ai

sourcery-ai Bot commented Nov 17, 2026

Copy link
Copy Markdown

Linked issues

Checked at commit 2fc7289

#150 fix: catch disabled TLS checks in session calls · 2 of 3 met · 1 to check by hand

# Requirement Status Notes
1 Take over FOCA's widened Semgrep rules and tests, identical apart from paths. 👀 Check by hand (external system) Compare the rules and tests in this change with FOCA's corresponding files to confirm they differ only in repository-specific paths.
2 Flag session.get(url, verify=False) and the two JWT cases. ✅ Met The Semgrep fixtures mark session.get with verify=False, verify_signature=None, and a header-derived algorithm passed through a variable as findings.
3 Fix or suppress, with a reason, any findings in the code. ✅ Met The PR reports zero findings in tracked files; the added insecure examples are rule-test fixtures annotated as expected findings.

@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 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


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

Comment thread .semgrep/pro-wes.yml Resolved
Comment thread .semgrep/pro-wes.yml Resolved
Comment thread .semgrep/pro-wes.yml Resolved
Comment thread .semgrep/pro-wes.yml Resolved
Comment thread .semgrep/pro-wes.yml Resolved
Comment thread .semgrep/pro-wes.yml Resolved
Comment thread .semgrep/pro-wes.yml Resolved
Comment thread .semgrep/pro-wes.yml Resolved
Comment thread .semgrep/pro-wes.yml Resolved
Comment thread .semgrep/pro-wes.py Resolved
@uniqueg
uniqueg merged commit e4618e2 into dev Nov 17, 2026
26 of 27 checks passed
@uniqueg
uniqueg deleted the fix/783-session-tls-rules branch November 17, 2026 18:32
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