Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/370-untrusted-firewall.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
An issue body that tells the agent to push to main is now recorded as named, blocked directives on the job page instead of only being fenced as data. The firewall matches a finite rule table and stores rule ids plus the body hash, never the text.
4 changes: 4 additions & 0 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -1629,6 +1629,10 @@ and approves the latest completed plan. The issue title and body are external, s
`plan` and `fix` build their goal with `untrustedIssueGoal()`
(`src/lib/untrusted.ts`): the text is fenced as `UNTRUSTED ISSUE TEXT`, a forged
fence is escaped, and the job audit records the label and a hash, never the body.
`scanUntrusted()` (`src/lib/firewall.ts`) then names the instruction-like
directives in that same text as rule ids; the job event `job.untrusted_input`
carries `blocked=<ids|none>` and the job page shows the count and the ids, while
the body stays out of the event, the log and the page.

Four verbs act on the job the work item already has: `/fledge retry`,
`/fledge pause`, `/fledge resume` and `/fledge cancel`. Retry goes through the
Expand Down
17 changes: 14 additions & 3 deletions docs/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,20 @@ stripped, and the payload keeps the 4,000-character cap, so a body cannot close
the block early or add a second instruction line. The fixed instruction stays
outside the block. The job audit row records `label=ISSUE TEXT`,
`sha256=<first 12 hex>` and `truncated=<bool>`; the body is never written to the
log. This is the labelling slice. The content firewall, confirmation for
high-risk actions and the bridge change are the follower half of #370, and host
isolation remains #109.
log.

The second slice adds the firewall (`src/lib/firewall.ts`). It scans exactly the
string the fence carries — after control characters are stripped, so a hidden
newline cannot split a pattern the agent still reads — against a finite, ordered
rule table (`ignore-previous-instructions`, `push-to-main`, `merge-pull-request`,
`system-prompt`, `run-command`, `reveal-secret`). A hit is recorded, never
blocked by itself: the job event `job.untrusted_input` and the audit row carry
`blocked=<rule ids>` (or `none`) beside the label, hash and truncation flag, and
the job page states the count and the ids. Matched text and the body never reach
the event, the log or the page; `parseUntrustedMeta()` refuses any record it did
not write, so a hand-edited row is not rendered as a claim. Judging whether a
flagged run may continue, confirming high-risk actions and the `bridge/` change
remain the follower half of #370, and host isolation remains #109.

### Job cost, evidence and metrics

Expand Down
24 changes: 19 additions & 5 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ import { isWorkItemState, MAINTAINER_ONLY_STATES, WORK_ITEM_TRANSITIONS, type Wo
import { parseStageMode } from './lib/stages.js'
import { issuePriority, issueState, shouldTriage } from './lib/issueIntake.js'
import { parseIssueCommand, parseMentionTrigger, type IssueCommandInput, type MentionTriggerInput } from './lib/issueCommands.js'
import { untrustedAuditDetail, untrustedIssueGoal } from './lib/untrusted.js'
import { untrustedAuditDetail, untrustedIssueGoal, untrustedMeta, type UntrustedText } from './lib/untrusted.js'
import { scanUntrusted, UNTRUSTED_EVENT_KIND } from './lib/firewall.js'
import { VERSION } from './version.js'
import { clearGauge, incCounter, observeHistogram, renderMetrics, setGauge } from './lib/metrics.js'
import { readReleaseStatus } from './lib/releaseStatus.js'
Expand Down Expand Up @@ -5899,6 +5900,19 @@ export async function buildApp(options: BuildOptions): Promise<BuiltApp> {
// event is handled. The comment id is both the claim key and the idempotency key:
// the claim absorbs a duplicate delivery, and if a delivery is retaken the
// receipt answers the replay instead of queueing a second job.
// #370 slice 2: the fence marks the text as data; this record says what the
// text tried to do. Only rule ids and the body hash are written — the job page
// and the audit log never receive the issue text itself.
const recordUntrustedInput = (outcome: CommandOutcome, untrusted: UntrustedText, payload: string, itemId: string): void => {
const firewall = scanUntrusted(payload)
const queuedJobId = commandJobId(outcome)
store.audit(repo.user_id, 'job.untrusted_input', untrustedAuditDetail(queuedJobId ?? itemId, untrusted, firewall.directives))
// The event is the job page's source; without a job there is no page to put
// it on, and an event keyed by a work item id would never be read back.
if (queuedJobId === null) return
store.recordJobEvent({ jobId: queuedJobId, repoId: repo.id, userId: repo.user_id, kind: UNTRUSTED_EVENT_KIND, meta: untrustedMeta(untrusted, firewall.directives) })
}

const runChatCreate = async (kind: JobKind, goal: string, itemId: string): Promise<CommandOutcome> => {
const actor = managementActor({ userId: repo.user_id, repo, channel: 'chat', requestId: `issue_command:${source.commentId}` })
return management.run(actor, {
Expand Down Expand Up @@ -5994,29 +6008,29 @@ export async function buildApp(options: BuildOptions): Promise<BuiltApp> {
return
}
if (command === 'plan') {
const { goal, untrusted } = untrustedIssueGoal({
const { goal, untrusted, payload } = untrustedIssueGoal({
issueUrl,
title,
detail,
instruction: 'Produce one concrete plan only; do not write code, create a branch or open a PR.',
})
const outcome = await runChatCreate('plan', goal, item.id)
if (outcome.ok) store.audit(repo.user_id, 'job.untrusted_input', untrustedAuditDetail(commandJobId(outcome) ?? item.id, untrusted))
if (outcome.ok) recordUntrustedInput(outcome, untrusted, payload, item.id)
await reply(outcome.ok ? `Plan job ${commandJobId(outcome) ?? ''} queued.` : `Could not queue a plan: ${outcome.message}`)
return
}
if (repo.plan_required === 1 && item.state !== 'approved' && item.state !== 'assigned' && item.state !== 'pr_ready') {
await reply('This repository requires an approved plan first. Run /fledge plan, then /fledge approve.')
return
}
const { goal, untrusted } = untrustedIssueGoal({
const { goal, untrusted, payload } = untrustedIssueGoal({
issueUrl,
title,
detail,
instruction: 'Implement one focused fix for this issue.',
})
const outcome = await runChatCreate('iterate', goal, item.id)
if (outcome.ok) store.audit(repo.user_id, 'job.untrusted_input', untrustedAuditDetail(commandJobId(outcome) ?? item.id, untrusted))
if (outcome.ok) recordUntrustedInput(outcome, untrusted, payload, item.id)
await reply(outcome.ok ? `Implementation job ${commandJobId(outcome) ?? ''} queued.` : `Could not queue a fix: ${outcome.message}`)
}

Expand Down
41 changes: 41 additions & 0 deletions src/lib/firewall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { stripControlCharacters } from './untrusted.js'

// #370, second slice: the first slice fences issue text so the agent reads it as
// data. Fencing alone does not tell a reader what the text tried to do, so this
// slice scans the same payload for instruction-like patterns and records only
// the rule ids. The body itself never leaves `wrapUntrusted`.
//
// The list is deliberately finite and readable: every rule answers "does this
// text try to hand the agent a new job?" and none of them decides anything on
// its own. It labels and records; it does not block the run, the merge or the
// user's right to write an angry issue.
export const UNTRUSTED_EVENT_KIND = 'job.untrusted_input'

export interface FirewallRule {
id: string
pattern: RegExp
}

export const FIREWALL_RULES: readonly FirewallRule[] = [
{ id: 'ignore-previous-instructions', pattern: /\b(?:ignore|disregard|forget)\b[^.\n]{0,60}\b(?:instructions?|prompts?|directions?|rules?)\b/i },
{ id: 'push-to-main', pattern: /\bpush\b[^.\n]{0,40}\b(?:to|into)\s+(?:main|master)\b/i },
{ id: 'merge-pull-request', pattern: /\bmerge\b[^.\n]{0,30}\b(?:pull request|pr)\b/i },
{ id: 'system-prompt', pattern: /\b(?:system prompt|you are now|new instructions?|act as (?:a|an|the) (?:system|admin|developer))\b/i },
{ id: 'run-command', pattern: /\b(?:run|execute)\b[^.\n]{0,30}\b(?:commands?|shell|bash|powershell|scripts?)\b/i },
{ id: 'reveal-secret', pattern: /\b(?:print|reveal|show|send|exfiltrate|leak|cat)\b[^.\n]{0,40}\b(?:secrets?|tokens?|passwords?|api[- ]?keys?|credentials?)\b/i },
]

export interface FirewallResult {
clean: boolean
/** Rule ids in table order. Matched text is never returned or stored. */
directives: string[]
}

// Scan exactly what the agent will read: the fence strips control characters
// before it is assembled, so scanning the raw value would let a hidden newline
// split a pattern that the agent still sees intact.
export function scanUntrusted(value: string): FirewallResult {
const text = stripControlCharacters(value)
const directives = FIREWALL_RULES.filter((rule) => rule.pattern.test(text)).map((rule) => rule.id)
return { clean: directives.length === 0, directives }
}
56 changes: 47 additions & 9 deletions src/lib/untrusted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { sha256Hex } from './secrets.js'
// #370, first slice: issue title and body are written by whoever opened the
// issue, so they are data. The goal keeps one fixed instruction line outside a
// labelled block; the external text lives only inside it. The firewall that
// blocks directives, the confirmation for high-risk actions and the bridge
// change are the follower half, not this one.
// scans that same text for instruction-like directives is slice 2
// (`src/lib/firewall.ts`); confirmation for high-risk actions and the bridge
// change are the follower half, still not here.
export const DEFAULT_UNTRUSTED_LABEL = 'ISSUE TEXT'
export const UNTRUSTED_CAP = 4000

Expand Down Expand Up @@ -33,8 +34,10 @@ function endFence(label: string): string {
// A control character can hide a newline from a reader while a parser still
// sees one. Keep the two characters that carry the document's shape. Iterating
// the code points avoids a control-character regular expression, which the lint
// baseline rejects as a new warning.
function stripControl(value: string): string {
// baseline rejects as a new warning. Exported because the firewall scans the
// same normalised text: a pattern that only matches the raw bytes is not a
// pattern the agent ever sees.
export function stripControlCharacters(value: string): string {
let clean = ''
for (const char of value) {
const code = char.codePointAt(0) ?? 0
Expand All @@ -61,7 +64,7 @@ function escapeFence(value: string): string {
export function wrapUntrusted(value: string, options: { label?: string; cap?: number } = {}): UntrustedText {
const label = fenceLabel(options.label ?? DEFAULT_UNTRUSTED_LABEL)
const cap = options.cap ?? UNTRUSTED_CAP
const clean = stripControl(value)
const clean = stripControlCharacters(value)
const truncated = clean.length > cap
const payload = escapeFence(truncated ? clean.slice(0, cap) : clean)
return {
Expand All @@ -80,17 +83,52 @@ export function untrustedIssueGoal(input: {
instruction: string
label?: string
cap?: number
}): { goal: string; untrusted: UntrustedText } {
const untrusted = wrapUntrusted(`Title: ${input.title}\n\n${input.detail}`, {
}): { goal: string; untrusted: UntrustedText; payload: string } {
const payload = `Title: ${input.title}\n\n${input.detail}`
const untrusted = wrapUntrusted(payload, {
...(input.label === undefined ? {} : { label: input.label }),
...(input.cap === undefined ? {} : { cap: input.cap }),
})
return {
goal: `Issue ${input.issueUrl}\n\n${untrusted.text}\n\n${input.instruction}`,
untrusted,
// The exact string the fence carries, so the firewall scans what the agent
// reads instead of a second, slightly different copy of it.
payload,
}
}

export function untrustedAuditDetail(jobId: string, untrusted: UntrustedText): string {
return `${jobId} label=${untrusted.label} sha256=${untrusted.hash} truncated=${untrusted.truncated ? 'true' : 'false'}`
// One record shape for the audit row and the job event, so the page reads back
// what the writer wrote. `blocked` is a list of rule ids or `none`; the body is
// never part of it.
export function untrustedMeta(untrusted: UntrustedText, directives: readonly string[] = []): string {
return `label=${untrusted.label} sha256=${untrusted.hash} truncated=${untrusted.truncated ? 'true' : 'false'} blocked=${directives.length === 0 ? 'none' : directives.join(',')}`
}

export function untrustedAuditDetail(jobId: string, untrusted: UntrustedText, directives: readonly string[] = []): string {
return `${jobId} ${untrustedMeta(untrusted, directives)}`
}

export interface UntrustedRecord {
label: string
hash: string
truncated: boolean
blocked: string[]
}

const UNTRUSTED_META = /^label=([A-Z0-9 _-]{1,40}) sha256=([0-9a-f]{12}) truncated=(true|false) blocked=(none|[a-z0-9-]{1,40}(?:,[a-z0-9-]{1,40}){0,19})$/

// The reader refuses anything it did not write: a hand-edited event, an older
// row without `blocked`, or a body smuggled into the field stays unrendered
// rather than becoming a page claim.
export function parseUntrustedMeta(meta: string | null | undefined): UntrustedRecord | null {
if (meta === null || meta === undefined) return null
const match = UNTRUSTED_META.exec(meta.trim())
if (match === null) return null
return {
label: match[1]!,
hash: match[2]!,
truncated: match[3] === 'true',
blocked: match[4] === 'none' ? [] : match[4]!.split(','),
}
}
12 changes: 12 additions & 0 deletions src/views/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import { carryOverHeaderFromGoal } from '../jobs/continuity.js'
import { parseModelRun } from '../lib/modelRun.js'
import { mergeReadiness, parsePathGuard, parseVerification } from '../lib/verification.js'
import { parseAdversarialRecord, type AdversarialRecord } from '../lib/adversarialReview.js'
import { UNTRUSTED_EVENT_KIND } from '../lib/firewall.js'
import { parseUntrustedMeta } from '../lib/untrusted.js'
import { releaseSurface, type ReleaseSurface } from '../lib/releaseSurface.js'
import { readReleaseNotesSources } from '../releases/notes.js'
import { compareRunning, type ReleaseStatusSnapshot } from '../lib/releaseStatus.js'
Expand Down Expand Up @@ -3193,6 +3195,15 @@ export function jobPage(input: {
// DESC`), so the first adversarial event is the newest verdict. Reversing the
// list would show the first tip's answer forever: a job whose later tip cleared
// the risk would keep claiming its merge was blocked.
// #370 slice 2: the job records the label, the body hash and the rule ids the
// firewall matched. The page states the count and the ids — never the text.
const untrustedInput = parseUntrustedMeta(events.find((event) => event.kind === UNTRUSTED_EVENT_KIND)?.meta)
const untrustedLine = untrustedInput === null
? ''
: `<p class="fine" data-untrusted-input><strong>${escapeHtml(t('不受信任输入', 'Untrusted input'))}:</strong> ${escapeHtml(untrustedInput.label)} · sha256 ${escapeHtml(untrustedInput.hash)} · ${escapeHtml(untrustedInput.blocked.length === 0
? t('没有指令式文本', 'no instruction-like text')
: t(`按数据处理,拦截 ${untrustedInput.blocked.length} 条指令式文本(${untrustedInput.blocked.join('、')})`, `${untrustedInput.blocked.length} instruction-like directive${untrustedInput.blocked.length === 1 ? '' : 's'} blocked and treated as data (${untrustedInput.blocked.join(', ')})`))}</p>`

const adversarial = parseAdversarialRecord(events.find((event) => event.kind === 'job.adversarial')?.meta)
const adversarialText = (record: AdversarialRecord): string => record.status === 'risk'
? t(`具体风险 ${record.target ?? ''} — 自动合并已阻止`, `concrete risk ${record.target ?? ''} — the automatic merge was blocked`)
Expand Down Expand Up @@ -3514,6 +3525,7 @@ export function jobPage(input: {
<p class="fine">${escapeHtml(t('成本', 'Cost'))} ${escapeHtml(cost)} · ${escapeHtml(t('创建', 'Created'))} ${escapeHtml(humanTimeShort(job.created_at))} · ${escapeHtml(t('结束', 'Finished'))} ${escapeHtml(humanTimeShort(job.finished_at))}</p>
${attribution === '' ? '' : `<p class="fine">${escapeHtml(attribution)}</p>`}
${modelLine}
${untrustedLine}
${providerLine}
${carryOverLine}
${job.checkpoint === null || job.checkpoint === '' ? '' : `<p class="fine"><strong>${escapeHtml(t('断点', 'Checkpoint'))}:</strong> ${escapeHtml(job.checkpoint)}</p>`}
Expand Down
Loading
Loading