[Bug]: Cross-tenant prompt injection via missing org filter when forking a prompt version
What happened?
Summary: PromptManager.createNewPromptVersion (backing POST /v1/prompt/version/{promptVersionId}/subversion and .../subversion-from-ui) resolves the parent version by id with no organization check. This lets any authenticated user fork another organization's prompt version, and the forged fork is later indistinguishable from a legitimate version of the victim's prompt on every read path that serves production prompt content — allowing a cross-tenant write of attacker-controlled content into a victim org's live prompt.
Root cause (valhalla/jawn/src/managers/prompt/PromptManager.ts, createNewPromptVersion, around line 1031):
WITH parent_prompt_version AS (
SELECT * FROM prompts_versions WHERE id = $1 -- $1 = promptVersionId, no org filter
),
bump_version AS (
SELECT major_version, minor_version
FROM prompts_versions
WHERE id = $8 -- same gap
)
INSERT INTO prompts_versions (prompt_v2, helicone_template, model, organization, ...)
SELECT
ppv.prompt_v2, -- copied from the UNSCOPED parent lookup
$2, -- attacker-supplied template
$3,
$4, -- attacker's OWN organization
...
FROM parent_prompt_version ppv
RETURNING id, ...;
promptVersionId ($1) comes straight from the URL path in controllers/public/promptController.ts (createSubversion / createSubversionFromUi) with no check that it belongs to the caller's org.
Why this is exploitable, not just a data-model quirk: the row that gets inserted has organization = <attacker's org> but prompt_v2 = <victim's real prompt id> (copied from the unscoped ppv). Every read path that serves production prompt content authorizes on prompt_v2.organization, never on prompts_versions.organization:
-- getCompiledPromptVersions, backs POST /v1/prompt/{name}/compile
-- getPormptVersionsTemplates, backs POST /v1/prompt/{name}/template
FROM prompts_versions
left join prompt_v2 on prompt_v2.id = prompts_versions.prompt_v2
WHERE prompt_v2.organization = $1 -- caller's org (the VICTIM, when they call it)
AND (prompts_versions.metadata->>'isProduction')::boolean = true
Since prompt_v2.organization is the real prompt owner (the victim), a forged row whose prompt_v2 points at the victim's prompt passes this check regardless of who owns the prompts_versions row itself.
And promotePromptVersionToProduction only checks prompts_versions.organization = caller, which the attacker trivially satisfies — it's the row they just inserted:
UPDATE prompts_versions
SET metadata = COALESCE(metadata, '{}'::jsonb) || '{"isProduction": true}'::jsonb
WHERE id = $1 AND organization = $2
Exploit chain:
- Attacker obtains a
prompts_versionsid belonging to another org (leak vector: shared links, support threads, logs — same trust assumption as the already-fixed CIRT-77/81/82 findings in this codebase, which were the same "unscoped id lookup onprompts_versions/prompt_v2" bug class in sibling methods). POST /v1/prompt/version/{victimVersionId}/subversionwith an arbitrarynewHeliconeTemplate— creates a version owned by the attacker's org but linked viaprompt_v2to the victim's real prompt.POST /v1/prompt/version/{forgedId}/promote— succeeds because the forged row'sorganizationis the attacker's own.- The victim's own next call to
/v1/prompt/{name}/compile(which they make at real request time to fetch their production prompt template) can return the attacker's forged template instead of their own, since the query never checks who authored the version row.
This is a cross-tenant write into another tenant's live prompt template, not just a read/data-exposure bug — the follow-on impact depends on what the victim's production LLM traffic does with an attacker-controlled prompt.
Suggested fix: scope both CTEs in createNewPromptVersion to organization = $4 (the caller's own org — already a bound parameter in the same query), matching the pattern every other method in this file already uses (editPromptVersionTemplate, promotePromptVersionToProduction, editPromptVersionLabel, etc). I have a minimal patch ready and will follow up with a PR.
Verification performed: static trace of every query in PromptManager.ts that reads or writes prompts_versions.organization vs. prompt_v2.organization, confirming this is the only method with the gap, and confirming the controller passes the path parameter through with no ownership check. I have not executed this against a running instance — flagging that distinction rather than claiming a live-tested PoC.
Affected file: valhalla/jawn/src/managers/prompt/PromptManager.ts, createNewPromptVersion (~line 1031 on current main).
This is already fixed on main. Commit 63879c1f0 (#5842) added AND organization = $4 (plus soft_delete = false) to both the parent-version and bump-version CTEs in PromptManager.createNewPromptVersion, so an out-of-org promptVersionId now resolves to zero rows and no forged version is inserted. Closing as resolved.