Skip to content

fix(rag-knowledge-agent): clear critical deps, fix typecheck, enforce ownership on account and document routes - #63

Open
glebedel wants to merge 5 commits into
mainfrom
fix/security-critical-pass-rag
Open

glebedel wants to merge 5 commits into
mainfrom
fix/security-critical-pass-rag

Conversation

@glebedel

@glebedel glebedel commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Security and critical-error pass on apps/rag-knowledge-agent. Two commits: dependencies + typecheck, then the ownership fixes from a Codex security review. Supersedes #58 (and the lockfile changes in #55 and #60).

Dependencies

Advisory (Aikido / Dependabot) Severity Fix
langsmith 0.3.87: CVE-2026-40190, CVE-2026-45134 (+3 medium) critical, high @langchain/core 0.3 → 1.5.0, @langchain/textsplitters 0.1 → 1.0.2, which brings langsmith 0.13.1. Same bump as #58, regenerated on current main
js-yaml 4.1.1 (Dependabot #133, #146, #174, #108) high 4.3.2 via an override scoped to @mdxeditor/editor, which pins 4.1.1 exactly in every 3.x release
browserslist 4.28.1 (#162) high 4.29.6 (in range)
uuid 10.0.0 nested under langchain (#93) medium removed by the langchain bump
pg, pg-connection-string, jose, react-hook-form, picomatch, @babel/core, tar-fs, ajv, highlight.js, enhanced-resolve, @ungap/structured-clone medium/low in-range updates

#58: its Aikido check showed one new critical, lodash 4.17.23. That came from the PR's stale base (it branched before lodash 4.18.1 landed on main), not from the bump itself. Rebuilt on main, a local Aikido dependency scan of this lockfile finds no critical issues.

Every version was resolved with npm --before=2026-11-21, to stay clear of the safe-chain quarantine on fresh releases.

The Aikido issue list for this repo still shows Next.js 16.1.6. That data is stale: main has had 16.4.6 since #61.

Broken on main

tsc --noEmit failed with 13 errors, which typescript.ignoreBuildErrors hid. src/types/jsx.d.ts redeclared the react and antd modules, and those declarations shadowed the real exports (MouseEvent, Statistic, Empty.PRESENTED_IMAGE_SIMPLE). It now maps only the global JSX namespace, which an older react-markdown inside @llamaindex/chat-ui still uses. The build type-checks again, and there is a new typecheck script.

Vercel did not install from the lockfile. Vercel runs the install inside the app directory, where npm walks up to the workspace root. The root has no lockfile, so every deploy resolved fresh versions. The app lockfile, its overrides and the security pins in it were all ignored. Main was deploying @stackone/ai 2.10.0 instead of the locked 2.3.0. 2.10.0 has no Tools.utilityTools, so getStackOneUtilityToolsForAISDK hit its catch and returned no tools, and the StackOne tools were silently off in production. The first deploy of this PR failed the type check on exactly that. vercel.json now sets "installCommand": "npm ci --prefix .", and the deploy passes.

Codex security review (25 findings)

Fixed (real):

# Sev Finding Fix
1 high delete-account deleted any StackOne account ID for any signed-in user only accounts linked to the caller
2 high chat loaded integrations by ID with no owner check, so a foreign integration UUID put on an agent lent its tools chat, /api/agents/process and PUT /api/agents/[id]/integrations resolve only the caller's integrations
3 high /api/agents/process imported files through foreign integrations same
4 high connect-session took origin_owner_id and account_id from the body owner is always the session user; account_id must be linked to the caller
5 high POST /api/integrations trusted the browser's account ID, and the upsert updated and returned another user's row checks the account's origin_owner_id with StackOne (GET /accounts/{id}); the upsert only updates the caller's row (409 otherwise)
6 high webhook skipped verification when STACKONE_WEBHOOK_SECRET was unset 503 until the secret is set; constant-time compare
9 high DELETE /api/documents/ingest deleted any document's chunks scoped to the caller
10 high compose published Postgres (password postgres) on all interfaces bound to 127.0.0.1
11 high env.example shipped a usable NEXTAUTH_SECRET empty, with a generate command
12 high schema enables no RLS while the README claims it (Supabase Data API exposure) RLS enabled on all 10 tables; the app connects as owner and is unaffected
14 med saving an agent reprocessed files of deselected integrations only selected integrations are sent
15 med IDs interpolated into StackOne URL paths encoded as one segment; ./.. rejected
16 med hub postMessage origin check used startsWith exact match
17 med login redirectTo was an open redirect sameOriginPath keeps only same-origin paths, rejecting a leading // or /\ both before and after URL parsing (closes the /.//evil.com dot-segment bypass the security review bot found in the first version)
21 med public /api/health spent an embeddings call per request requires a session
cubic #24 P2 account ID prefixes in logs removed (both sites)

Left open, with reasons:

  • 7 (high) Google sign-in links to a pre-registered password account with the same email. Closing it needs email verification or explicit linking, which is a product decision.
  • 8, 13 (high/med) Prompt injection from document text can steer tool_execute, and tools span the whole connected account. Closing it needs human approval for write actions, or per-file scoping at dispatch. That is a design change.
  • 18 (med) CSRF from sibling origins. The default deployment is on vercel.app, a public suffix, and the NextAuth cookie is SameSite=Lax, so the risk needs a custom domain with a hostile sibling subdomain. Left as is.
  • 19 (med) Webhook replay. A fix needs the signed timestamp or delivery ID format, which this code does not have.
  • 20 (med) Raw StackOne bodies and prompts in debug logs. These only print with DEBUG=1.
  • 22–24 (med) No rate or cost limits on ingest, chat or sign-in. These belong in the deployment for an example app.
  • 25 (low) Signup reveals whether an email exists. Accepted.
  • Still open from Aikido: pdfjs-dist 4.8.69 (high, fixed only in 6.x; @llamaindex/chat-ui 0.6.1, the latest, pins @llamaindex/pdf-viewer 1.3.0 → react-pdf 9 → pdfjs 4.8.69). zod 3.25.76 under @llamaindex/openai 0.1.x (no 3.x fix; needs @llamaindex/openai 0.4). minimatch 9 (dev, needs 11). katex 0.16 and lexical 0.35 (fixes are 0.x-major bumps inside chat-ui's ranges). openai 4.x (medium).

Also found: proxy.ts sits at the app root, but the app uses src/, so Next never compiles it (middleware-manifest.json is empty). Every API route checks the session itself, so nothing is exposed. Moving it to src/ would also need /api/auth added to its public paths, or sign-in breaks. Left for a separate change.

Testing (local, Node 22.22.0)

  • Clean worktree: npm ci (lockfile in sync), npm run typecheck (0 errors, was 13), npm run lint (0 errors, 4 existing warnings), npm run build (type-checking now on): all pass.
  • RecursiveCharacterTextSplitter.splitText on @langchain/textsplitters 1.0.2 returns the expected chunks.
  • next start smoke test: / and /login return 200, /api/auth/csrf 200, /api/health and /api/integrations 401 unauthenticated. Webhook with no secret: 503. With a secret: unsigned 401, bad signature 401, valid HMAC 200.
  • SQL (PGlite, schema minus pgvector): another user's upsert returns 0 rows, a foreign integration cannot be linked to an agent or loaded for chat, a foreign chunk delete removes nothing, RLS is on for 10 tables, and a non-owner role reads 0 rows while the owner still works.
  • npm test (new, node --test): sameOriginPath unit tests pass. They cover //evil.com, /\evil.com, /.//evil.com, /a/..//evil.com, /%2e//evil.com, /\t/evil.com, /%2F%2Fevil.com (kept as a local path), https://evil.com and javascript:alert(1).
  • Vercel simulation: a workspace-root npm install reproduces the failed deploy (utilityTools type errors with @stackone/ai 2.10.0). npm ci --prefix . and then npm run build passes with 2.3.0. The Vercel preview for this PR now deploys.
  • Not run: flows against a live StackOne project (connect, file import, chat with tools).

🤖 Generated with Claude Code

glebedel and others added 2 commits November 25, 2026 10:19
…nd fix the typecheck

Dependencies (lockfile regenerated with --before so no version is younger than
the Aikido safe-chain cooldown):
- @langchain/core 0.3 -> 1.5.0 and @langchain/textsplitters 0.1 -> 1.0.2. This
  is the bump from #58, redone on current main: langsmith moves 0.3.87 ->
  0.13.1 (critical CVE-2026-40190, high CVE-2026-45134) and the nested uuid 10
  and zod 3 copies under @langchain/core go away. The only API the app uses is
  RecursiveCharacterTextSplitter.splitText, which is unchanged. #58 showed a new
  critical only because its base predates the lodash 4.18.1 bump on main.
- js-yaml 4.1.1 -> 4.3.2 (three high advisories). @mdxeditor/editor 3.x pins
  4.1.1 exactly, so an override scoped to it moves it to the patched 4.x.
- In-range updates: browserslist 4.29.6 (high), @babel/core 7.29.13, pg 8.23.1
  and pg-connection-string 2.14.1, jose 6.2.18, react-hook-form 7.92.0,
  picomatch 2.3.2 / 4.0.7, tar-fs 2.1.5, ajv 6.15.0 / 8.20.0, highlight.js
  11.12.0, enhanced-resolve 5.27.0, @ungap/structured-clone 1.4.0.

Typecheck: `tsc --noEmit` failed with 13 errors on main, hidden by
`typescript.ignoreBuildErrors`. src/types/jsx.d.ts redeclared parts of the
react and antd modules, which shadowed their real exports; it now only maps
the global JSX namespace that an older react-markdown inside
@llamaindex/chat-ui still references. The build type-checks again and a
`typecheck` script is added.

Logs: account ID prefixes are no longer logged (cubic review on #24).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nd document routes

Findings from a Codex security review of the app, each reproduced in code:
- Account routes trusted IDs from the browser. delete-account and
  connect-session now only act on StackOne accounts linked to the caller,
  connect sessions always use the caller as origin owner, and registering an
  integration checks the account's origin_owner_id with StackOne first. The
  upsert no longer updates (or returns) another user's row.
- Chat, document processing and agent_integrations only resolve integrations
  the caller owns, so a foreign integration UUID cannot lend its tools or files
  to someone else's agent.
- DELETE /api/documents/ingest only removes the caller's chunks.
- The webhook rejected nothing when STACKONE_WEBHOOK_SECRET was unset; it now
  returns 503 until the secret is configured, and compares signatures in
  constant time.
- Login redirectTo is reduced to a same-origin path; the hub postMessage
  listener matches origins exactly instead of by prefix.
- StackOne IDs are encoded as single URL path segments.
- /api/health requires a session, since each call spends an embeddings request.
- Saving an agent only processes files for the integrations still selected.
- Config: compose binds Postgres to 127.0.0.1, env.example no longer ships a
  usable NEXTAUTH_SECRET, and the schema enables row-level security (the app
  connects as table owner, so only other roles such as Supabase's Data API are
  denied).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vercel
vercel Bot temporarily deployed to Preview – oauth-redirect-proxy November 25, 2026 10:21 Inactive
@vercel

vercel Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
oauth-redirect-proxy Skipped Skipped Nov 26, 2026 2:23pm UTC

Request Review

…ter dot-segment resolution

The origin check alone passed "/.//evil.com": the URL parser keeps the origin
but resolves the path to "//evil.com", which a browser then treats as another
host. sameOriginPath now rejects a leading "//" or "/\" both in the raw input
and in the resolved pathname. It moves to its own module with node:test unit
tests (`npm test`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Vercel runs the install inside the app directory, where npm resolves the
monorepo workspace root. That root has no lockfile, so every deploy resolved
fresh versions and ignored the app lockfile, its overrides and the security
pins in it. On the rag app this pulled @stackone/ai 2.10.0 instead of the
locked 2.3.0. 2.10.0 has no Tools.utilityTools, which failed the now-enabled
type check and on main silently disabled the StackOne tools at runtime.
`npm ci --prefix .` installs from the app lockfile.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…-pass-rag

apps/rag-knowledge-agent/package-lock.json: took main's lockfile, regenerated with npm install --package-lock-only, then npm update on the transitive packages this PR had bumped (ajv, pg*, picomatch, jose, tar-fs and others) so the audit result matches the PR head (0 critical, 3 high)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Guillaume Lebedel <10488548+glebedel@users.noreply.github.com>
@vercel
vercel Bot temporarily deployed to Preview – oauth-redirect-proxy November 26, 2026 14:23 Inactive
@s2d2-agent

s2d2-agent Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Merged main into this branch to clear conflicts in apps/rag-knowledge-agent/package-lock.json.

CI on the new head 12120ab: all 4 checks pass.

This branch was previously deployed

1 inactive deployment
Preview – oauth-redirect-proxy — 12120ab6 Deployed Nov 26, 2026 by vercel[bot]
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