Skip to content

fix(artifacts): screen note content for prompt injection before generating - #735

Merged
jovandyaz merged 1 commit into
mainfrom
fix/artifact-injection-guard
Nov 20, 2026
Merged

jovandyaz merged 1 commit into
mainfrom
fix/artifact-injection-guard

Conversation

@jovandyaz

@jovandyaz jovandyaz commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Closes #383.

Why

POST /artifacts/generate sends a note of up to 48,000 characters to the model, and nothing scans it for prompt injection. The note can be shared or co-edited, so it can carry a collaborator's text. The artifacts pipeline bypasses AICompletionPipeline, the only pipeline that calls the heuristic guard. A spec even locked in the unguarded behavior.

Policy

The heuristic can't tell an attack from a note that discusses one: a security note quoting "ignore all previous instructions" scores 0.9, the same as a real attack. So every hit has to be cleared by the model classifier (AI_GUARD_CLASSIFIER_MODEL), and the generation is refused when the classifier flags a hit or can't decide.

What changed

  • NoteContentGuard (artifacts/application/services/note-content-guard.ts):
    • It runs detectAiInput and locateInjectionSpans from llm-router over two views: the plain text, which joins a phrase that inline markup splits, and the raw HTML the model receives, which keeps attributes and elements such as <style>.
    • Each hit becomes an excerpt for the classifier, with whitespace collapsed so padding can't push the phrase out of the 4,000-character input. An excerpt carries 600 characters of context on each side, and nearby hits share one. Excerpts are classified in parallel.
    • It refuses when:
      • the classifier flags an excerpt or can't decide;
      • a view fails but the hit can't be located, or one hit is too long for an excerpt;
      • there are more than 8 excerpts.
  • AIGenerationPipeline:
    • It accepts a screen callback that runs after the budget check, so a caller over budget doesn't pay for classifier calls.
    • A refusal or a throw releases the reservation.
  • Errors: PROMPT_INJECTION_DETECTED returns 422. Refusals log artifacts.generate.injection_blocked with reason; cleared notes log artifacts.generate.injection_cleared.
  • Docs: a new "Artifact note screening" paragraph in docs/AI.md. The pipeline order is corrected too: model selection comes before the rate-limit check.

POST /artifacts/learn isn't screened: its 200-character topic is the caller's own input.

Trade-offs

  • A passage visible in both views costs 2 classifier calls. A note with 5 or more scattered mentions is refused unjudged (too_many_hits).
  • A view the heuristic passes, including gray-zone scores between 0.3 and 0.6, sends nothing to the classifier.

Review

An independent review ran three rounds. Fixed along the way:

  • whitespace padding that hid a phrase from every window;
  • a cross-view dedupe that let text in <style> or an attribute through unjudged;
  • classifier calls running before the budget check;
  • a reservation leaking when screening throws.

Verification

  • New specs for the guard and the handler cover:
    • an attack deep in a long note;
    • whitespace padding;
    • a phrase split by markup;
    • hits in an attribute, in an attribute containing >, and in <style>;
    • flag and degraded verdicts;
    • the cap;
    • reservation release on refusal and on a throw;
    • no classifier call when over budget.
  • Artifacts specs: 147 passed. nx run-many -t test lint -p api, api:typecheck and Prettier: clean.
  • Not exercised against a live classifier model.

Summary by CodeRabbit

  • New Features
    • Notes are screened for prompt-injection-like content before artifact generation. Unsafe content, or content the screening system cannot safely assess, is blocked with an explanatory error.
    • Notes that pass screening continue through artifact generation.
  • Documentation
    • Updated AI documentation to describe note screening and its place in the artifact generation process.

@jovandyaz

Copy link
Copy Markdown
Owner Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Artifact generation now screens sanitized note content before generation. The guard checks plain-text and raw-HTML views, classifies excerpts for detected hits, and returns a refusal for unsafe or unclassifiable content. The pipeline releases its reservation when screening refuses or throws.

Changes

Artifact note screening

Layer / File(s) Summary
Detect and classify note excerpts
apps/api/src/modules/ai/application/services/injection-classifier.service.ts, apps/api/src/modules/artifacts/application/services/note-content-guard.ts, apps/api/src/modules/artifacts/application/services/note-content-guard.spec.ts
NoteContentGuard checks plain-text and raw-HTML views, groups detected hits into bounded excerpts, and classifies them. It blocks unlocated hits, excessive excerpts, unsafe verdicts, and degraded verdicts. Tests cover detection, excerpt construction, and refusal cases.
Screen notes in artifact generation
apps/api/src/modules/artifacts/application/services/ai-generation.pipeline.ts, apps/api/src/modules/artifacts/application/commands/generate-artifact.handler.ts, apps/api/src/modules/artifacts/application/commands/generate-artifact.handler.spec.ts, apps/api/src/modules/artifacts/domain/errors/artifact.errors.ts, apps/api/src/modules/artifacts/artifact-error-status.map.ts, apps/api/src/modules/artifacts/artifacts.module.ts, docs/AI.md
The handler passes a screening callback to the pipeline. The pipeline runs it after reservation and releases the reservation on refusal or exception. A blocked result maps to PROMPT_INJECTION_DETECTED and HTTP 422. Tests and documentation cover the flow.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant GenerateArtifactHandler
  participant AIGenerationPipeline
  participant NoteContentGuard
  participant InjectionClassifierService
  GenerateArtifactHandler->>AIGenerationPipeline: Submit generation request with screening callback
  AIGenerationPipeline->>NoteContentGuard: Screen sanitized note after reservation
  NoteContentGuard->>InjectionClassifierService: Classify excerpts for heuristic hits
  InjectionClassifierService-->>NoteContentGuard: Return classifier verdicts
  NoteContentGuard-->>AIGenerationPipeline: Return safe verdict or screening error
  AIGenerationPipeline->>AIGenerationPipeline: Release reservation on error or exception
Loading
Merge Risk: Minimal · up to afae9

Artifact generation now screens note content for prompt-injection text before calling the model. Unsafe or undecidable notes get a 422 refusal, and the budget reservation is released. No concrete defect was found, so the change looks safe to merge. The classifier was not exercised live.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the primary change: screening note content for prompt injection before artifact generation. The changeset adds NoteContentGuard, wires it into the generation pipeline, returns PROMPT_I…
Linked Issues check Passed Directly linked issue #383 asks that note content in POST /artifacts/generate be scanned before it reaches the model, and that the test codifying unguarded behavior be replaced. The PR meets both re…
Out of Scope Changes check Passed The changes support the screening objective:
- CLASSIFIER_INPUT_MAX_CHARS is exported so the guard can size excerpts.
- The handler spec and note-content-guard.spec.ts cover the new behavior.
- Th…
Full details: Title check

Explanation

The title names the primary change: screening note content for prompt injection before artifact generation. The changeset adds NoteContentGuard, wires it into the generation pipeline, returns PROMPT_INJECTION_DETECTED, and updates docs, which matches the title. The title uses the conventional fix(artifacts) prefix and is specific. At 74 characters it is within a reasonable length.

Full details: Linked Issues check

Explanation

Directly linked issue #383 asks that note content in POST /artifacts/generate be scanned before it reaches the model, and that the test codifying unguarded behavior be replaced. The PR meets both requirements:

  • NoteContentGuard runs detectAiInput and locateInjectionSpans over plain-text and raw-HTML views. It runs before generation, and GenerateArtifactHandler supplies it as the screen callback in AIGenerationPipeline.
  • The previous spec that asserted successful generation from injection-like phrasing was replaced. New specs assert refusal (PROMPT_INJECTION_DETECTED, 422) for flagged or degraded verdicts, and clearance for benign hits.

The PR differs from the issue's Proposal in one respect. The issue suggests that a heuristic score of 0.6 or higher blocks. The PR instead sends every heuristic hit to the classifier and refuses only when the classifier flags or cannot decide. The issue states this as a proposal, and the PR states its reason (the heuristic also scores quoted attack text at 0.9). The feature-flag rollout is optional in the issue, and the PR does not add it. The issue leaves /artifacts/learn as a decision, and the PR excludes it with a stated reason. Evidence used: the PR summary, the changed-file summaries, and the docs change. No focused code inspection was run.

Full details: Out of Scope Changes check

Explanation

The changes support the screening objective:

  • CLASSIFIER_INPUT_MAX_CHARS is exported so the guard can size excerpts.
  • The handler spec and note-content-guard.spec.ts cover the new behavior.
  • The generate-artifact.handler.ts, ai-generation.pipeline.ts, artifacts.module.ts, artifact.errors.ts, and artifact-error-status.map.ts changes wire the guard, the callback, the reservation release, and the 422 error.
  • The docs/AI.md change documents screening. Its pipeline-order correction is small and matches the new screen step.

No change was found that is unrelated to #383. The PR does not change /artifacts/learn behavior. Evidence: the file-level summaries and the PR description. No focused code inspection was run.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install timed out. The project may have too many dependencies for the sandbox.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 7fd495f and afae996.

Files selected for processing: 10

  • apps/api/src/modules/ai/application/services/injection-classifier.service.ts
  • apps/api/src/modules/artifacts/application/commands/generate-artifact.handler.spec.ts
  • apps/api/src/modules/artifacts/application/commands/generate-artifact.handler.ts
  • apps/api/src/modules/artifacts/application/services/ai-generation.pipeline.ts
  • apps/api/src/modules/artifacts/application/services/note-content-guard.spec.ts
  • apps/api/src/modules/artifacts/application/services/note-content-guard.ts
  • apps/api/src/modules/artifacts/artifact-error-status.map.ts
  • apps/api/src/modules/artifacts/artifacts.module.ts
  • apps/api/src/modules/artifacts/domain/errors/artifact.errors.ts
  • docs/AI.md

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Action not completed

Review rate limited.


Your included review limit is currently reached under our Fair Usage Limits Policy. This review may still proceed through usage-based billing if eligible. Your next included review will be available in 43 minutes.

@jovandyaz

Copy link
Copy Markdown
Owner Author

@coderabbitai full review

@jovandyaz
jovandyaz force-pushed the fix/artifact-injection-guard branch from d05a320 to afae996 Compare November 20, 2026 21:37
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Action performed

Full review finished.

@jovandyaz
jovandyaz enabled auto-merge (squash) November 20, 2026 21:49
@jovandyaz
jovandyaz merged commit 23e2c89 into main Nov 20, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(artifacts): scan note content with the injection guard before it reaches the model

1 participant