Skip to content

security: timing-safe HMAC and CORS wildcard fix - #617

Merged
T3ST3ST3R0N merged 3 commits into
PasarGuard:devfrom
wahh3b-lgtm:pr-1-security-v2
Oct 11, 2026
Merged

T3ST3ST3R0N merged 3 commits into
PasarGuard:devfrom
wahh3b-lgtm:pr-1-security-v2

Conversation

@wahh3b-lgtm

@wahh3b-lgtm wahh3b-lgtm commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

Changes

  • JWT timing-safe verification (app/utils/jwt.py): Replaced in membership check with hmac.compare_digest using bitwise OR to ensure both comparisons always execute, preventing timing side-channels
  • CORS wildcard fix (app/middlewares/__init__.py): When allow_credentials=True, any wildcard origin (*) in the allowed origins list now disables credentials with a warning
  • Python 2 syntax fix (app/utils/jwt.py): Fixed except TypeError, ValueError: to except (TypeError, ValueError):

What was removed (per project owner feedback)

  • Rate limiting: Should be handled at infrastructure level (nginx, cloudflare, haproxy)
  • TTL on get_secret_key: The JWT secret key is permanent and doesn't need a TTL
  • invalidate_secret_key_cache: Unused function removed

Files Changed

  • app/utils/jwt.py
  • app/middlewares/__init__.py

Verification

  • Ruff lint + format pass
  • 13/13 tests pass

Summary by CodeRabbit

  • Bug Fixes
    • Cross-origin requests now include credential permission only when allowed origins are explicitly configured. When the wildcard origin is used, credential permission is omitted.
    • Legacy subscription tokens are accepted when their signatures match either supported format. Tokens with altered signatures, including non-ASCII characters, are rejected.

- Use hmac.compare_digest with bitwise OR for constant-time JWT signature verification
- Reject wildcard CORS origins when allow_credentials=True
- Fix Python 2 exception syntax (except TypeError, ValueError -> except (TypeError, ValueError))
@coderabbitai

coderabbitai Bot commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 03059053-52b6-4a7d-b3a6-5cfa249491be

📥 Commits

Reviewing files that changed from the base of the PR and between 2c474eb and c8a87c9.


📒 Files selected for processing (3)
  • app/middlewares/__init__.py
  • app/utils/jwt.py
  • tests/test_security_hardening.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.



Walkthrough

The pull request changes legacy subscription-token signature validation to use constant-time comparisons. It also disables CORS credentials when the allowed origins include *. Tests cover both changes.

Changes

Legacy token validation

Layer / File(s) Summary
Legacy signature validation and tests
app/utils/jwt.py, tests/test_security_hardening.py
Legacy signature validation uses constant-time comparisons against two supported encodings. Tests cover valid and altered signatures.

CORS credential handling

Layer / File(s) Summary
CORS origin configuration and tests
app/middlewares/__init__.py, tests/test_security_hardening.py
CORS setup disables credentials for wildcard origins and enables them for explicit origins. Tests check the credentials and allow-origin headers.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: m03ed

Merge Risk: ⚪ Minimal · up to c8a87

The change hardens legacy token signature comparison and stops sending CORS credentials for wildcard origins. No actionable merge-blocking risk is evident.

Pre-merge checks | Passed 4 | Failed 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes both main changes: timing-safe HMAC validation and the CORS wildcard credential fix.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit checks each token’s mark,
And guards the origins after dark.
Two signatures meet a careful test,
While wildcard stars receive no crest.
The burrow’s headers settle right,
Then carrots crunch beneath moonlight.

Comment @coderabbitai help to get the list of available commands.

dev restructured app/utils/jwt.py (new HMAC token format, Python 3.14
except syntax), so take dev's version here; the constant-time compare is
re-applied to the legacy branch in the next commit.
…e legacy signatures in constant time

ALLOWED_ORIGINS defaults to "*". With allow_credentials on, Starlette
then echoes every cross-origin request's Origin back together with
Access-Control-Allow-Credentials, so any site could make credentialed
requests and read the responses. Credentials are now only allowed when
the origins are listed explicitly. The panel authenticates with bearer
tokens, so the dashboard is unaffected; there is no startup warning,
since the wildcard is the default.

The legacy subscription token branch still matched its signature with
`in`. It now uses hmac.compare_digest on bytes for both encodings (str
input must be ASCII, and the signature comes from the URL).
@T3ST3ST3R0N

Copy link
Copy Markdown
Collaborator

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@T3ST3ST3R0N
T3ST3ST3R0N merged commit ef84f21 into PasarGuard:dev Oct 11, 2026
9 checks passed
Free-Guy-IR added a commit to Free-Guy-IR/panel that referenced this pull request Oct 11, 2026
…am PR PasarGuard#617)

With the default ALLOWED_ORIGINS=* and allow_credentials=True, Starlette
answers every foreign origin with that origin echoed back and
Access-Control-Allow-Credentials: true. The panel authenticates with a bearer
token and sets no cookie, so no credential was exposed, but the configuration
is the one upstream PR PasarGuard#617 flags. Credentials are now allowed only when the
origins are listed explicitly; a wildcard keeps serving plain cross-origin
reads. PasarGuard#617's other two changes, the constant-time subscription signature
check and the except-clause syntax, are already in the fork's
app/utils/jwt.py. Test: tests/test_cors_credentials.py, RED 2 of 3 before and
GREEN 3 of 3 after.
Free-Guy-IR added a commit to Free-Guy-IR/panel that referenced this pull request Oct 11, 2026
 hardening, PasarGuard#617 CORS, PasarGuard#879 loopback Xray client inbounds, Fork Tests, and the parity-gate follow-ups

Migrations: P13's fork migration bfd88026565b (after fd33c6013baa) was a
second head. The integration's own merge revision 8158387867c9 now joins
0374ac83604b with bfd88026565b instead of fd33c6013baa (the approved
re-seat; no upstream-authored migration is edited). One head. SQLite:
upgrade from d8b2c4f60a17, alembic check, downgrade to d8b2c4f60a17,
re-upgrade and alembic check all pass.

OVERRIDES.md: union. app/utils/jwt.py keeps P10's revocation-epoch clause
and adds P13's PasarGuard#756 iat/exp rule; app/notification/webhook/__init__.py takes
P13's PasarGuard#756 proxy_settings clause; the P4b user-modal.tsx entry and P13's
client-template-form.ts entry both stay; the P5 and P11 clauses on
node/__init__.py, node_checker.py and record_usages.py are kept.
Free-Guy-IR added a commit to Free-Guy-IR/panel that referenced this pull request Nov 2, 2026
The ledger parser took any line starting with # as a heading, so a
wrapped prose line such as "PasarGuard#879 and PasarGuard#617 ..." closed the Port queue and
every entry after it was ignored, and a line "#waived" inside the Port
queue moved the following entries into the Waived section, where they
counted as decided with no deadline and no security classification.
Markdown renders both lines as text, so a reader of the ledger could not
see either effect.

Headings now follow the CommonMark ATX rule: up to three spaces, one to
six hashes, then a space, a tab or the end of the line. A line that
names a ledger section without that space ("#waived", "##Port queue")
still switches the section as before but is a ledger error, so the gate
fails closed; any other hash line is text and leaves the section open.

Tests: "#waived", "##ported by design" and "  #Waived  #" inside the
Port queue fail the gate with the new error, "#Waived: moved from the
queue" leaves the next security: yes entry red, and a wrapped "PasarGuard#879 ..."
line keeps every queued entry counted. All five fail on the previous
parser; the existing "##Port queue" case still reports its missing until.
Free-Guy-IR added a commit to Free-Guy-IR/panel that referenced this pull request Nov 2, 2026
…e03754dec

Checked with git ls-remote on 2026-10-11 14:49 UTC; every other forked
repository, tag and release is unchanged since 02:55 UTC.

- PasarGuard#617 (ef84f21) and the confirmation-dialog escaping (1c25036) are
  ported by design: the fork has 24bcbba and 0e08156 for the CORS
  credentials and the constant-time signature compare, and 9c3ae14 for
  the dialogs (escapeValue on, Trans instead of raw HTML).
- The chart stylesheet escaping (a1d697b) and PasarGuard#859 (ba4a0ab, the
  device-limit race and the proxy_settings leak in /info and /raw) are
  queued security: yes; the fork has neither, so the gate is red on them
  until they are ported.
- PasarGuard#950, PasarGuard#947, PasarGuard#857, PasarGuard#903, PasarGuard#908 and PasarGuard#996 are queued security: no until
  2026-10-25, each with its reason.
- Node PasarGuard#64 (e03754dec, WireGuard peers through Xray's UserManager, Go
  1.27.1, unpinned pre-release Xray by default) is queued security: no
  until 2026-10-25; a port must pin the Xray tag.

Against live upstream the panel gate now fails only on the 19 port-queue
commits classified security: yes, and the node ledger only on
f72ac0efacdb (security: yes).
Free-Guy-IR added a commit to Free-Guy-IR/panel that referenced this pull request Nov 2, 2026
… waiver

B7-ADV F9: a bridge waiver whose reason named a CVE and an auth bypass
passed, Waived and Ported by design entries for a "fix(auth): ... (CVE-...)"
commit passed with no security note, and npm waivers had no classification
at all.

- Waived entries need exactly one security: yes|no. A waived security: yes
  entry also needs owner-decision: YYYY-MM-DD-<name>, dated no later than
  today.
- An entry classified security: no fails in every section when the
  upstream subject or the reason names a CVE or GHSA id or carries a
  (security) mark.
- A Ported by design entry needs a classification when the keyword hint
  fires on its subject or reason; Waived and Ported by design security: no
  entries with a hint get the re-review note.
- The bridge and npm waivers need security = "yes"|"no", and
  owner_decision = "YYYY-MM-DD-<name>" for yes.

The real ledgers are classified: every Waived entry is security: no (the
upstream notifier, telemt and feature commits have no counterpart in the
fork); PasarGuard#879, PasarGuard#617, the dashboard name escaping and node PasarGuard#100 are Ported by
design with security: yes. The bridge and npm waivers are security = "no";
xray-config-kit 0.3.10 to 0.4.1 only add Xray parity data and MASQUE and
XDrive support.
Free-Guy-IR added a commit to Free-Guy-IR/panel that referenced this pull request Nov 2, 2026
…e files

The real ledger's PasarGuard#617 entry cites fork commit 24bcbba for the CORS
change in app/middlewares/__init__.py; the commit's OVERRIDES.md line
was reworded later, so checking every hunk of the commit refuses a
port that is still in place. A cited fork commit is checked in the
files the reason names, and in all its files when it names none.

RED on 46e6459: the guard.py case fails.
Free-Guy-IR added a commit to Free-Guy-IR/panel that referenced this pull request Nov 2, 2026
A Ported by design security entry passes on a fork commit that is an
ancestor of HEAD and whose change is still there. When the reason
names files the fork commit touched, only those files' hunks are
checked, so a later rewording of an unrelated line in the same commit
(the OVERRIDES.md line of 24bcbba behind PasarGuard#617) no longer refuses a
port whose code is in place; a reason that names no file still needs
every hunk of the commit. A file counts as named only as a whole path,
not as the tail of a longer one.
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.

2 participants