fix(session-recovery): audit raw prompt aliases
This commit is contained in:
@@ -21,6 +21,17 @@ interface PromptWithToolResultInput {
|
|||||||
body: { parts: ToolResultPart[] }
|
body: { parts: ToolResultPart[] }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ClientWithPromptAsync = Client & {
|
||||||
|
session: Client["session"] & {
|
||||||
|
promptAsync: (input: PromptWithToolResultInput) => Promise<unknown>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function hasPromptAsync(client: Client): client is ClientWithPromptAsync {
|
||||||
|
const promptAsync = (client.session as { promptAsync?: unknown }).promptAsync
|
||||||
|
return typeof promptAsync === "function"
|
||||||
|
}
|
||||||
|
|
||||||
interface ToolUsePart {
|
interface ToolUsePart {
|
||||||
type: "tool_use"
|
type: "tool_use"
|
||||||
id: string
|
id: string
|
||||||
@@ -104,17 +115,12 @@ export async function recoverUnavailableTool(
|
|||||||
path: { id: sessionID },
|
path: { id: sessionID },
|
||||||
body: { parts: toolResultParts },
|
body: { parts: toolResultParts },
|
||||||
}
|
}
|
||||||
const promptAsync = client.session.promptAsync as (...args: never[]) => unknown
|
if (!hasPromptAsync(client)) {
|
||||||
const promptClient = {
|
return false
|
||||||
session: {
|
|
||||||
status: client.session.status,
|
|
||||||
promptAsync: (input: PromptWithToolResultInput) => (
|
|
||||||
Reflect.apply(promptAsync, client.session, [input]) as Promise<unknown>
|
|
||||||
),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const promptResult = await promptAsyncAfterSessionIdle<PromptWithToolResultInput>({
|
const promptResult = await promptAsyncAfterSessionIdle<PromptWithToolResultInput>({
|
||||||
client: promptClient,
|
client,
|
||||||
sessionID,
|
sessionID,
|
||||||
source: "session-recovery-unavailable-tool",
|
source: "session-recovery-unavailable-tool",
|
||||||
input: promptInput,
|
input: promptInput,
|
||||||
|
|||||||
@@ -42,6 +42,14 @@ describe("production prompt injection routes", () => {
|
|||||||
// given
|
// given
|
||||||
const files = await listSourceFiles(SOURCE_ROOT)
|
const files = await listSourceFiles(SOURCE_ROOT)
|
||||||
const offenders: string[] = []
|
const offenders: string[] = []
|
||||||
|
const rawPromptPatterns = [
|
||||||
|
/\bsession\.promptAsync\s*\(/,
|
||||||
|
/\bsession\.prompt\s*\(/,
|
||||||
|
/\bReflect\.apply\s*\(\s*\w*promptAsync\b/,
|
||||||
|
/\bReflect\.apply\s*\(\s*\w*prompt\b/,
|
||||||
|
/\b(?:const|let|var)\s+\w*promptAsync\w*\s*=\s*[\w.]+\.session\.promptAsync\b/,
|
||||||
|
/\b(?:const|let|var)\s+\w*prompt\w*\s*=\s*[\w.]+\.session\.prompt\b/,
|
||||||
|
]
|
||||||
|
|
||||||
// when
|
// when
|
||||||
for (const filePath of files) {
|
for (const filePath of files) {
|
||||||
@@ -50,7 +58,7 @@ describe("production prompt injection routes", () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const contents = uncommentedLines(await readFile(filePath, "utf8")).join("\n")
|
const contents = uncommentedLines(await readFile(filePath, "utf8")).join("\n")
|
||||||
if (/\bsession\.promptAsync\s*\(/.test(contents) || /\bsession\.prompt\s*\(/.test(contents)) {
|
if (rawPromptPatterns.some((pattern) => pattern.test(contents))) {
|
||||||
offenders.push(relativeSourcePath(filePath))
|
offenders.push(relativeSourcePath(filePath))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user