Skip to content

fix(notion-app): percent-encode reflected uid in OAuth links and redirects - #20400

Closed
kaleidoscopeAI wants to merge 2 commits into
BasedHardware:mainfrom
kaleidoscopeAI:fix/notion-app-uid-encoding-v2
Closed

kaleidoscopeAI wants to merge 2 commits into
BasedHardware:mainfrom
kaleidoscopeAI:fix/notion-app-uid-encoding-v2

Conversation

@kaleidoscopeAI

@kaleidoscopeAI kaleidoscopeAI commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #15011

Fresh PR off current main — supersedes #15013, which has drifted and no longer merges cleanly.

The error-parameter half of #15011 already landed via #14503 (the callback error is HTML-escaped on main). This PR completes the remaining half: percent-encode the reflected uid at every outbound-URL interpolation in the Notion app, per FC-uid-reflected-unencoded-in-remaining-apps.

Changes (plugins/omi-notion-app/main.py):

  • Root handler: quote(clean_uid, safe='') for the connect link, HTML-escaped once into safe_auth_url
  • Connected handler: quote(clean_uid, safe='') for the disconnect link, HTML-escaped once into safe_disconnect_url
  • OAuth callback success page: safe_continue_url = html.escape(f"/?uid={quote(uid, safe='')}", quote=True) — closes the attribute-breakout vector (/?uid={uid} was interpolated raw)
  • /disconnect redirect: quote(clean_uid, safe='') in the Location target
  • Callback params typed Optional[str] with isinstance guards so a hostile non-string query value fails closed instead of rendering

Tests (plugins/omi-notion-app/test_notion_security.py):
Hermetic suite — framework doubles injected via patch.dict(sys.modules, ...), so fastapi/requests/dotenv are not required and it runs in the Hygiene lane:

  • uid sanitization accepts valid uids, rejects injection payloads
  • /auth/notion, /setup/notion, /disconnect reject malformed uids
  • root page renders a safe connect link and returns metadata JSON for hostile uids
  • callback error parameter is HTML-escaped (reflected XSS)
  • callback success link percent-encodes a hostile uid (attacker" onfocus="alert(1) renders as attacker%22%20onfocus%3D%22alert%281%29), keeping the HTML attribute intact

Registered in .github/checks-manifest.yaml as notion-app-security-tests (triggers on plugins/omi-notion-app/** and .github/checks-manifest.yaml).

Failure-Class: FC-uid-reflected-unencoded-in-remaining-apps

Review in cubic

@cubic-dev-ai cubic-dev-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.

1 issue found across 3 files

Confidence score: 4/5

  • In plugins/omi-notion-app/main.py, a truthy non-string error is ignored, so a call with valid code and state can still proceed to token exchange instead of failing closed. Reject non-string errors before handling string errors.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="plugins/omi-notion-app/main.py">

<violation number="1" location="plugins/omi-notion-app/main.py:1179">
P2: A truthy non-string `error` is silently ignored, so a direct call with valid `code` and `state` continues into token exchange instead of failing closed. Reject non-string errors before handling a string error.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread plugins/omi-notion-app/main.py Outdated
):
"""Handle Notion OAuth2 callback."""
if error:
if error and isinstance(error, str):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2: A truthy non-string error is silently ignored, so a direct call with valid code and state continues into token exchange instead of failing closed. Reject non-string errors before handling a string error.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At plugins/omi-notion-app/main.py, line 1179:

<comment>A truthy non-string `error` is silently ignored, so a direct call with valid `code` and `state` continues into token exchange instead of failing closed. Reject non-string errors before handling a string error.</comment>

<file context>
@@ -1170,27 +1171,28 @@ async def notion_auth(uid: str = Query(...)):
 ):
     """Handle Notion OAuth2 callback."""
-    if error:
+    if error and isinstance(error, str):
+        safe_error = html.escape(error, quote=True)
         return HTMLResponse(content=f"""
</file context>
Suggested change
if error and isinstance(error, str):
if error is not None and not isinstance(error, str):
return HTMLResponse(content="Invalid error parameter", status_code=400)
if error:

Comment thread .github/checks-manifest.yaml Resolved
@kaleidoscopeAI

Copy link
Copy Markdown
Contributor Author

CI is green on this head — ready to merge.

Head: 40a7b7bd77065f7ad064c6d93f59a13828729c07
Hygiene: success

Fix (plugins/omi-notion-app/main.py): percent-encode the reflected uid at every outbound-URL interpolation — connect link (quote(clean_uid, safe='') → safe_auth_url), disconnect link (safe_disconnect_url), OAuth callback success page (safe_continue_url), and the /disconnect redirect target — and fail closed on non-string callback params (Optional[str] + isinstance guards). The error-parameter XSS half of #15011 already landed via #14503.

Tests (plugins/omi-notion-app/test_notion_security.py): 8 hermetic tests (framework doubles injected via patch.dict(sys.modules, ...), no fastapi/requests/dotenv needed): uid sanitization valid/invalid, malformed-uid rejection on /auth/notion, /setup/notion, /disconnect, root-page uid escaping + XSS neutralization, callback error-param XSS escaping, and the uid attribute-breakout payload (attacker" onfocus="alert(1) renders as attacker%22%20onfocus%3D%22alert%281%29).

Registered in .github/checks-manifest.yaml as notion-app-security-tests (triggers on plugins/omi-notion-app/** and .github/checks-manifest.yaml).

5 bounty for #15011.

@Git-on-my-level

Copy link
Copy Markdown
Member

Reviewed all three changed files at head 40a7b7b, and ran the test suite locally — 8/8 pass hermetically (stdlib-only framework doubles, no third-party deps, no network). Thanks for the clean, well-scoped hardening pass.

plugins/omi-notion-app/main.py — The encode-then-escape ordering is correct and applied consistently at every interpolation site: quote(clean_uid, safe='') builds the URL component, then html.escape(..., quote=True) for the HTML attribute (safe_auth_url, safe_disconnect_url, safe_continue_url), plus quote() in the /disconnect redirect Location. The Optional[str] + isinstance guards on the callback params are fail-closed and harmless (real FastAPI query params are always strings, so they only matter for direct invocation). One scope note: since _sanitize_uid() enforces ^[a-zA-Z0-9_\-]{1,128}$ and the callback uid must reproduce a server-stored state built from a sanitized uid, quote() is a no-op on currently reachable paths — this lands as defense-in-depth rather than closing a reachable attribute-breakout, so the PR description slightly overstates reachability. Still worth having; layered encoding is the right pattern if the sanitizer ever loosens.

plugins/omi-notion-app/test_notion_security.py — Genuinely hermetic rewrite; the attribute-breakout assertion (attacker%22%20onfocus%3D%22alert%281%29) is a nice exact-match check. Two minor non-blocking nits: (1) no test exercises workspace_name escaping with a hostile value (the callback test only uses Test Workspace); (2) the file is missing a trailing newline.

.github/checks-manifest.yaml — notion-app-security-tests registration follows the existing per-plugin pattern and the trigger set is appropriately scoped.

Since this modifies the OAuth callback and redirect surface, it needs maintainer sign-off before merge — leaving for human maintainer review rather than an automated approval.


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 security-review Touches auth, provider routing, secrets, or security-sensitive surfaces positive-signal Automation verified a genuine fix/quality contribution python labels Oct 3, 2026
@Git-on-my-level

Copy link
Copy Markdown
Member

Thanks @kaleidoscopeAI — clean, well-scoped hardening follow-up to #15011, and the fresh rebase off main is appreciated. I verified the diff file by file and ran the new suite locally (hermetic, stdlib-only): 8/8 pass.

plugins/omi-notion-app/main.py

  • The callback success page was the one genuinely raw reflection point (href="/?uid={uid}" interpolated the state-derived uid unescaped); html.escape(f"/?uid={quote(uid, safe='')}", quote=True) closes it correctly. Worth noting for the record: reaching that render also required a matching stored state, so this is defense-in-depth plus removal of raw interpolation — still the right fix.
  • Root connect/disconnect links: moving from html.escape(clean_uid) to quote(clean_uid, safe='') with a single html.escape of the full URL is the right layering (percent-encode for the URL, escape for the HTML attribute). Since _sanitize_uid already restricts uids to [a-zA-Z0-9_-]{1,128}, these are consistency/defense-in-depth — good.
  • /disconnect now percent-encodes the uid in the Location target, which also neutralizes CR/LF header injection.
  • Optional[str] + isinstance guards on the callback params fail closed — good hardening.

plugins/omi-notion-app/test_notion_security.py

  • The stdlib framework doubles via patch.dict(sys.modules, ...) match the loader pattern already used by this app's test_main.py, and the new attribute-breakout assertion (attacker%22%20onfocus%3D%22alert%281%29) plus the error-XSS test pin the exact behavior. Confirmed green with no third-party deps.

.github/checks-manifest.yaml

  • notion-app-security-tests registration looks right; triggers scoped to plugins/omi-notion-app/** and the manifest itself. One heads-up: Hygiene skipped on this PR run (fork PR gate), so the suite hasn't executed in repo CI yet — maintainers may want a ci:full run before merge to see it in the real lane.

Non-blocking nit: the test file is missing a trailing newline.

This touches the OAuth callback surface, so it stays under security-review for maintainer sign-off before merge — the code itself looks correct to me.


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

The callback previously only handled string errors (`if error and
isinstance(error, str)`), so a truthy non-string `error` was silently
ignored and the request flowed on into token exchange when code/state were
present. Treat any truthy error as terminal, escaping it when it is a string
and returning a generic 400 otherwise.

Also add regression coverage for the non-string case and for workspace-name
escaping, backing the manifest's stated coverage.
@Git-on-my-level

Copy link
Copy Markdown
Member

Follow-up on the new head 2249870: the incremental commit addresses both non-blocking nits from the earlier review and hardens one more edge.

plugins/omi-notion-app/main.py

  • The callback error path now fails closed: a truthy non-string error (the previous if error and isinstance(error, str) guard would have silently skipped the error branch and continued toward token exchange) now renders the escaped "Invalid authorization error." 400 page. The matching isinstance guards on code/state are consistent with that posture.
  • The encode-then-escape layering from the prior review is unchanged and remains correct at every uid interpolation site (safe_auth_url, safe_disconnect_url, safe_continue_url, and the /disconnect Location target).

plugins/omi-notion-app/test_notion_security.py

  • test_callback_rejects_non_string_error pins the fail-closed behavior and asserts requests.post and get_oauth_state are never reached for a hostile non-string error.
  • test_root_escapes_workspace_name covers the earlier nit: a hostile workspace name is HTML-escaped in the connected page.
  • Trailing-newline nit fixed.

Verification: I ran the suite hermetically (stdlib doubles only, no network, no third-party deps): 10/10 pass. The Hygiene lane also ran notion-app-security-tests on this head and it passed — this is the first in-repo CI execution (it was skipped on the previous head by the fork gate), so the earlier request to see it run in the real lane is satisfied.

.github/checks-manifest.yaml is unchanged since the earlier review; the registration remains correctly scoped.

Still an OAuth-callback security change, so security-review stays on for maintainer sign-off before merge — the code itself looks correct to me.


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

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

Approved 2249870a71c59cb59024714ebc23b97e4a0e8c68. plugins/omi-notion-app/main.py on main still interpolates a raw uid into /?uid=. This head encodes it and Hygiene succeeded.

@aryanorastar

Copy link
Copy Markdown
Collaborator

This is a later copy of #13658, which already percent-encodes the Notion uid links and the disconnect redirect. This head only edits that same plugins/omi-notion-app/main.py plus tests and the checks manifest. Closing it.

@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Hey @kaleidoscopeAI 👋

Thank you so much for taking the time to contribute to Omi! We truly appreciate you putting in the effort to submit this pull request.

After careful review, we've decided not to merge this particular PR. Please don't take this personally — we genuinely try to merge as many contributions as possible, but sometimes we have to make tough calls based on:

  • Project standards — Ensuring consistency across the codebase
  • User needs — Making sure changes align with what our users need
  • Code best practices — Maintaining code quality and maintainability
  • Project direction — Keeping aligned with our product principles and locked invariants

Before your next PR, please skim:

  • PRODUCT.md — product north star
  • Product invariants — locked rules (shared chat, memory tiers, agent control plane, integrations, brand)

If this was declined for direction or taste, maintainers should cite an invariant ID or open a proposed one — ask if that citation is missing.

Your contribution is still valuable to us, and we'd love to see you contribute again in the future! If you'd like feedback on how to improve this PR or want to discuss alternative approaches, please don't hesitate to reach out.

Thank you for being part of the Omi community!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

positive-signal Automation verified a genuine fix/quality contribution python security-review Touches auth, provider routing, secrets, or security-sensitive surfaces

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bounty proposal] security(notion-app): reflected XSS in OAuth callback error parameter and attribute injection in success link (5 proposed)

3 participants