Skip to content

fix(subscription): sanitize uid and eliminate script injection in pricing template (#14477) - #14478

Merged
kodjima33 merged 2 commits into
BasedHardware:mainfrom
PINYOPATTANAWASANPORN:fix-subscription-xss-security
Sep 18, 2026
Merged

kodjima33 merged 2 commits into
BasedHardware:mainfrom
PINYOPATTANAWASANPORN:fix-subscription-xss-security

Conversation

@PINYOPATTANAWASANPORN

@PINYOPATTANAWASANPORN PINYOPATTANAWASANPORN commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary of Changes

  • Resolves [Bounty proposal] fix(subscription): sanitize uid and eliminate script injection in pricing template ($25 proposed) #14477 by sanitizing uid query parameter in subscription_page and eliminating unescaped template interpolation in inline JavaScript.
  • Enforces strict alphanumeric/underscore/hyphen character allowlist for uid up to 128 characters via _sanitize_uid().
  • Updates templates/subscription/index.html to store the sanitized uid as a data-uid attribute on the container element, removing inline Jinja2 injection from <script> blocks.
  • Adds client-side validation pattern matching for URL search parameters before appending to payment checkout links.
  • Adds comprehensive regression tests in plugins/subscription/test_subscription_security.py.

Root Cause / Technical Context

plugins/subscription/main.py accepted an unvalidated uid query parameter and passed it into templates/subscription/index.html. The template previously interpolated {{ uid }} directly into JavaScript variables within <script>, allowing attackers to execute arbitrary JavaScript (reflected XSS) via crafted query payloads (e.g. ?uid=';alert(document.domain);//).

Verification & Testing

  • Validated with unit test suite covering:
    • Valid UID formats (alphanumeric, underscores, dashes up to 128 chars).
    • Malformed and malicious payloads (<script>, SQL-like injections, backticks, quotes).
    • Clean HTML rendering without unescaped variable leaks.
  • All diffs strictly normalized to LF (\n).

Impact & Compatibility

  • Breaking changes: None.
  • External dependencies affected: None.

Review in cubic

Failure-Class: none

Note: The JS fallback parameter was aligned from uid= to client_reference_id= to match the static Stripe checkout link format client_reference_id={{ uid }}.

@Git-on-my-level

Copy link
Copy Markdown
Member

Thanks @PINYOPATTANAWASANPORN — solid fix for the reflected XSS from #14477, and the regression tests are a welcome addition.

Verified against head 60a9c21:

  • plugins/subscription/main.py — the _sanitize_uid() allowlist (re.fullmatch on ^[a-zA-Z0-9_-]{1,128}$) is the right shape for this, the route now renders safe_uid, and the new invalid-format warning avoids logging raw attacker input. Nit: move import re to the module top with the other imports.
  • plugins/templates/subscription/index.html — removing the raw {{ uid }} interpolation from the inline <script> and switching to the data-uid attribute + client-side /^[a-zA-Z0-9_-]{1,128}$/ check eliminates the ?uid=';alert(...);// vector (main interpolated the param straight into a JS string literal). The escaped output is only safe because data-uid="{{ uid }}" is a double-quoted attribute context — fine today since the server allowlist runs first, just worth remembering if that attribute ever moves. Also: real uids containing dots (Firebase-style ids) are now silently dropped — probably fine for this page, but flag it if checkout links ever carry those.
  • plugins/subscription/test_subscription_security.py — all 4 tests pass in a clean venv with the plugin-pinned deps (fastapi 0.121.0); placement matches the existing plugins//test_.py convention (e.g. plugins/oauth/test_oauth_hardening.py). Unused import re at the top.

Two CI failures need a quick follow-up before merge:

  1. Hygiene: trailing whitespace introduced at plugins/templates/subscription/index.html:794 — strip the trailing spaces on the added blank line.
  2. PR Metadata Preflight: with a fix: commit subject, the PR body needs a Failure-Class: declaration — add one line, Failure-Class: FC-<existing-slug>, Failure-Class: new, or Failure-Class: none.

One small thing worth a line in the PR description: the JS fallback append changed from uid= to client_reference_id= (~line 800). It aligns with the static Stripe links' existing client_reference_id={{ uid }} and that branch was effectively unreachable on main (the param and the template uid derive from the same query value), so it looks like a correctness alignment — just currently unexplained.

Leaving for human maintainer review: it's a security fix on the payments/checkout page, so it should get maintainer sign-off before merge.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@Git-on-my-level Git-on-my-level added positive-signal Automation verified a genuine fix/quality contribution needs-maintainer-review Needs a human maintainer to sign off before merge backend Backend Task (python) labels Sep 18, 2026
- Strips trailing whitespace in index.html line 794.
- Moves import re to module top in main.py.
- Removes unused import re in test_subscription_security.py.
@PINYOPATTANAWASANPORN

Copy link
Copy Markdown
Contributor Author

Thank you @Git-on-my-level for the thorough review and validation!

Addressed all items in commit 25d4d0c:

  1. Hygiene: Stripped all trailing whitespace in plugins/templates/subscription/index.html.
  2. Imports: Moved import re to top of module in plugins/subscription/main.py alongside other stdlib imports, and removed unused import re from test_subscription_security.py.
  3. PR Metadata Preflight: Added Failure-Class: none to the PR description.
  4. Parameter Context: Documented the alignment of the Stripe checkout link query parameter from uid= to client_reference_id= in the description.

All CI preflight and hygiene checks should now be green!

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Confirmed raw {{ uid }} interpolated into inline <script> in subscription template on main (reflected XSS), plus a real uid=/client_reference_id= param mismatch bug. Fixes #14477 with sanitization + data-attribute + tests, 5/5. Merging.

@kodjima33
kodjima33 merged commit d1bfd19 into BasedHardware:main Sep 18, 2026
30 checks passed
pedrolimaDG added a commit to pedrolimaDG/omi that referenced this pull request Sep 21, 2026
…id, add hermetic tests

- Remove genuinely unused imports: Response, HTTPException,
  RedirectResponse, JSONResponse, datetime, quote
- Preserve _sanitize_uid and all XSS protection from BasedHardware#14478
- Add hermetic test_main.py (10 tests) with self.fail() guards:
  - Router prefix/tags validation
  - Empty uid endpoint rendering
  - Valid uid sanitized passthrough
  - XSS payload rejection via _sanitize_uid
  - SQL injection rejection via _sanitize_uid
- Register subscription-plugin-tests in checks-manifest.yaml

Addresses all feedback from @Git-on-my-level review:
1. _sanitize_uid restored (never removed in this revision)
2. Tests assert sanitized output, not raw passthrough
3. self.fail() in else branches prevents silent passes
4. checks-manifest covers subscription test suite

Failure-Class: none
pedrolimaDG added a commit to pedrolimaDG/omi that referenced this pull request Sep 22, 2026
…id, add hermetic tests

- Remove genuinely unused imports: Response, HTTPException,
  RedirectResponse, JSONResponse, datetime, quote
- Preserve _sanitize_uid and all XSS protection from BasedHardware#14478
- Add hermetic test_main.py (10 tests) with self.fail() guards:
  - Router prefix/tags validation
  - Empty uid endpoint rendering
  - Valid uid sanitized passthrough
  - XSS payload rejection via _sanitize_uid
  - SQL injection rejection via _sanitize_uid
- Register subscription-plugin-tests in checks-manifest.yaml

Addresses all feedback from @Git-on-my-level review:
1. _sanitize_uid restored (never removed in this revision)
2. Tests assert sanitized output, not raw passthrough
3. self.fail() in else branches prevents silent passes
4. checks-manifest covers subscription test suite

Failure-Class: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Backend Task (python) needs-maintainer-review Needs a human maintainer to sign off before merge positive-signal Automation verified a genuine fix/quality contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bounty proposal] fix(subscription): sanitize uid and eliminate script injection in pricing template ($25 proposed)

3 participants