fix(ENG-12332): export ToolSanitizationRule and add tier2Fields option#16
Merged
Conversation
Allows consumers to properly type custom toolRules passed via PromptDefenseOptions.config without importing from internal modules. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…c fields When tier2Fields is set, extractStrings only collects text from matching field keys (descending into non-matching keys to find them deeper). This lets callers focus Tier 2 on user-controlled fields (e.g. subject, body) and skip encoded or non-text fields, reducing both latency and false-negative risk from base64-encoded content. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Changes enableTier2 default from false to true so callers don't need to explicitly opt in. Also updates DEFAULT_TIER2_CONFIG.enabled to reflect the actual behaviour. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR expands the public API and adds an option to scope Tier 2 classification to specific tool-result fields, aiming to improve consumer ergonomics and reduce Tier 2 latency/noise.
Changes:
- Export
ToolSanitizationRulefrom the package entrypoint for external typing. - Add
tier2Fields?: string[]to configuration/types andPromptDefenseOptions. - Update Tier 2 text extraction to optionally collect strings only under matching field keys.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/types.ts | Extends PromptDefenseConfig.tier2 with tier2Fields and documents the behavior. |
| src/index.ts | Exposes ToolSanitizationRule via the public entrypoint type exports. |
| src/core/prompt-defense.ts | Implements filtered string extraction for Tier 2 and wires tier2Fields into defendToolResult. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Contributor
There was a problem hiding this comment.
1 issue found across 2 files (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="src/core/prompt-defense.ts">
<violation number="1" location="src/core/prompt-defense.ts:169">
P2: This makes Tier 2 classification run by default when `enableTier2` is omitted, which conflicts with the documented default of `false` and can add unexpected model initialization/latency for existing consumers. Keep the default off unless explicitly enabled.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
- Remove explicit enableTier2: true from all examples (now the default) - Update API reference: default true, set false to disable - Add tier2Fields to API reference Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Normalize empty fields array to collect-all (avoids silent Tier 2 bypass) - Fall back to config.tier2.tier2Fields when options.tier2Fields is unset - Use Set for O(1) key lookups during traversal Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ToolSanitizationRulefrom the public API so consumers can type customtoolRuleswithout reaching into internal modulestier2Fields?: string[]toPromptDefenseOptionsandPromptDefenseConfig.tier2— when set, Tier 2 only classifies strings extracted from matching field keys (while still descending into non-matching keys to find them deeper). This lets callers focus Tier 2 on user-controlled fields (e.g.subject,body) and skip encoded or structural fields, reducing latency and false-negative risk.Test plan
import type { ToolSanitizationRule } from '@stackone/defender'createPromptDefense({ enableTier2: true, tier2Fields: ['subject', 'body'] })only runs Tier 2 on those fields🤖 Generated with Claude Code
Summary by cubic
Exported
ToolSanitizationRule, addedtier2Fieldsto scope Tier 2 to specific fields, enabled Tier 2 by default, and surfaced a skip reason when Tier 2 is skipped. Addresses ENG-12332.New Features
ToolSanitizationRulefrom@stackone/defenderfor typing customtoolRules.tier2Fields?: string[]toPromptDefenseOptionsandPromptDefenseConfig.tier2; when set, Tier 2 only classifies strings under those keys; when unset, all strings are scanned.tier2Fields.Bug Fixes
extractStringstreats emptytier2Fieldsas scan-all, falls back toconfig.tier2.tier2Fields, and uses aSetfor faster lookups.tier2Fieldsis set so they aren’t dropped during extraction.tier2SkipReasontoDefenseResultand set it when Tier 2 is skipped (e.g., no strings intier2Fieldsor classifier skip).enabledfromPromptDefenseConfig.tier2and defaults.Written for commit 82c9e81. Summary will update on new commits.