-
-
Notifications
You must be signed in to change notification settings - Fork 1
Cross-language parity: unified case-insensitive pattern superset, TS harmful-content check, strict parse (#30) #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
073a845
f0e99cd
abf7930
77e2d6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,15 +53,25 @@ export class ToolGuard extends BaseGuard { | |
| 'send_email', 'transfer_money', 'make_payment', | ||
| ]); | ||
|
|
||
| // Unified cross-language superset — every pattern case-insensitive. | ||
| // Mirrors Python DEFAULT_DANGEROUS_PATTERNS exactly; the previously | ||
| // missing del/format/sudo/chmod/subprocess/os.system entries are the | ||
| // #30 divergences (Python blocked them, npm passed them). | ||
| private static DEFAULT_DANGEROUS_PATTERNS = [ | ||
| /DROP\s+TABLE/i, | ||
| /DELETE\s+FROM/i, | ||
| /TRUNCATE\s+TABLE/i, | ||
| /rm\s+-rf/i, | ||
| /rmdir\s+\/s/i, | ||
| /del\s+\/f/i, | ||
| /format\s+c:/i, | ||
| /sudo\s+/i, | ||
| /chmod\s+777/i, | ||
| /eval\s*\(/i, | ||
| /exec\s*\(/i, | ||
| /__import__/i, | ||
| /subprocess/i, | ||
| /os\.system/i, | ||
| ]; | ||
|
|
||
| constructor(options: { | ||
|
|
@@ -594,28 +604,53 @@ export class SafetyGuard extends BaseGuard { | |
| name = 'SafetyGuard'; | ||
| description = 'Comprehensive safety checks'; | ||
|
|
||
| private checkPii: boolean; | ||
| private checkInjection: boolean; | ||
|
|
||
| private static PII_PATTERNS = { | ||
| email: /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/, | ||
| phone: /\b\d{3}[-.]?\d{3}[-.]?\d{4}\b/, | ||
| ssn: /\b\d{3}-\d{2}-\d{4}\b/, | ||
| creditCard: /\b\d{4}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}\b/, | ||
| // #30 parity: Python detects IPs, npm silently passed them. | ||
| ipAddress: /\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/, | ||
| }; | ||
|
|
||
| // #30 parity: mirrors Python INJECTION_PATTERNS — the missing five | ||
| // patterns let injection payloads pass on npm while Python blocked them. | ||
| private static INJECTION_PATTERNS = [ | ||
| /ignore\s+(previous|all|above)\s+(instructions?|prompts?)/i, | ||
| /disregard\s+(previous|all|above)/i, | ||
| /forget\s+(everything|all|your\s+instructions)/i, | ||
| /you\s+are\s+now\s+/i, | ||
| /act\s+as\s+if\s+you\s+are/i, | ||
| /pretend\s+(you|to\s+be)/i, | ||
| /new\s+instructions?\s*:/i, | ||
| /system\s*:\s*/i, | ||
| /<\|.*?\|>/, | ||
| /\[\[.*?\]\]/, | ||
| ]; | ||
|
|
||
| // #30 parity: Python's harmful-content check had no npm counterpart at | ||
| // all — "api_key=sk-12345" was BLOCKED on Python and passed on npm. | ||
| private static HARMFUL_PATTERNS = [ | ||
| /password\s*[=:]\s*\S+/i, | ||
| /api[_-]?key\s*[=:]\s*\S+/i, | ||
|
greptile-apps[bot] marked this conversation as resolved.
Outdated
|
||
| /secret\s*[=:]\s*\S+/i, | ||
| /private[_-]?key/i, | ||
|
greptile-apps[bot] marked this conversation as resolved.
Outdated
|
||
| /BEGIN\s+(RSA|DSA|EC)\s+PRIVATE\s+KEY/, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: This private-key pattern is case-sensitive, so lowercase or mixed-case PEM headers pass npm while Python blocks them. [api mismatch] Assessment: 🟠 Prompt for AI Agent 🤖This is a comment left during a code review.
**Path:** npm/src/guards.ts
**Line:** 638:638
**Comment:**
*Api Mismatch: This private-key pattern is case-sensitive, so lowercase or mixed-case PEM headers pass npm while Python blocks them.
Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix |
||
| ]; | ||
|
|
||
| constructor(options: { checkPii?: boolean; checkInjection?: boolean } = {}) { | ||
| private checkPii: boolean; | ||
| private checkInjection: boolean; | ||
| private checkHarmful: boolean; | ||
|
|
||
| constructor(options: { | ||
| checkPii?: boolean; | ||
| checkInjection?: boolean; | ||
| checkHarmful?: boolean; | ||
| } = {}) { | ||
| super(); | ||
| this.checkPii = options.checkPii ?? true; | ||
| this.checkInjection = options.checkInjection ?? true; | ||
| this.checkHarmful = options.checkHarmful ?? true; | ||
| } | ||
|
|
||
| check(response: ParsedResponse, context?: Record<string, any>): GuardResult { | ||
|
|
@@ -649,6 +684,19 @@ export class SafetyGuard extends BaseGuard { | |
| } | ||
| } | ||
|
|
||
| if (this.checkHarmful) { | ||
| // Mirrors Python: harmful content is an ERROR-severity issue | ||
| // (fails the guard), unlike PII which is only a warning (#30). | ||
| for (const pattern of SafetyGuard.HARMFUL_PATTERNS) { | ||
| if (pattern.test(content)) { | ||
| return this.failResult( | ||
| 'Safety check failed: 1 critical issue(s)', | ||
| { issues: [{ type: 'harmful', severity: 'error', details: [pattern.source] }] }, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (issues.length > 0) { | ||
|
sentry[bot] marked this conversation as resolved.
|
||
| return this.failResult(`Safety issues detected: ${issues.join(', ')}`, { issues }, 'warning'); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -110,7 +110,10 @@ export class ResponseVerifier { | |
| } | ||
|
|
||
| private parseResponse(response: any): ParsedResponse { | ||
| if (typeof response === 'object' && response !== null) { | ||
| // Parse strictness mirrors Python _parse_response (#30): Python | ||
| // raises ValueError for non-dict/scalar inputs — npm must reject | ||
| // them too, not wrap them as {type:'unknown'} and verify them. | ||
| if (response !== null && typeof response === 'object' && !Array.isArray(response)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔒 Security & Privacy | 🟠 Major | ⚡ Quick win 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -eu
printf '%s\n' '--- npm/src/verifier.ts ---'
sed -n '1,155p' npm/src/verifier.ts
printf '%s\n' '--- npm/src/guards.ts relevant symbols ---'
rg -n -A45 -B10 'extractContent|class SafetyGuard|api_key|secret|harmful' npm/src/guards.ts npm/src
printf '%s\n' '--- TypeScript verifier tests and package scripts ---'
rg -n -A12 -B8 'ResponseVerifier|parseResponse|JSON.parse|strictMode' npm test* npm 2>/dev/null | head -240Repository: QWED-AI/qwed-open-responses Length of output: 39033 Sensitive Data Exposure (CWE-200): Exposure of Sensitive Information to an Unauthorized Actor Reachability: External · Exploitability: Trivial Validate the result of When 🤖 Prompt for AI Agents |
||
| return response; | ||
| } | ||
|
|
||
|
|
@@ -122,6 +125,9 @@ export class ResponseVerifier { | |
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: A JSON string representing an array can bypass verification checks in Suggested FixAfter calling Prompt for AI Agent |
||
| } | ||
|
|
||
| return { type: 'unknown', raw: String(response) }; | ||
| const typeName = response === null ? 'null' : Array.isArray(response) ? 'list' : typeof response; | ||
| throw new Error( | ||
| `Cannot parse response of type ${typeName}. Expected object, string, or JSON.` | ||
| ); | ||
|
Comment on lines
+128
to
+131
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: The Suggested FixWrap the call to Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. |
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.