fix(#2198): remove dead scan exports, correct injection-scan docs

scanEntropyAnomalies + shannonEntropy were dead exports with zero
production callers — the live hooks (gsd-prompt-guard.js,
gsd-read-injection-scanner.js) inline their own pattern subsets for
hook independence and never called these functions.

Changes:
- Remove scanEntropyAnomalies + shannonEntropy from src/security.cts
- Remove scanEntropyAnomalies test block from tests/security.test.cjs
- Correct REQ-SCAN-INJ-02/-03 in FEATURES.md (EN/zh-CN/ja-JP) to
  describe what actually runs live (injection patterns, invisible
  Unicode) vs CI-only (base64-decode, codebase scan)
- Correct docs/security/baseline.md §2.4 to clarify live hooks inline
  patterns, not import from security.cts
- Add regression test asserting the corrected contract
- scanForInjection retained: it serves as the CI codebase-scanner engine
This commit is contained in:
Tom Boucher
2026-07-11 23:48:05 -04:00
parent 96b632d4eb
commit 8022c5c864
9 changed files with 132 additions and 134 deletions

View File

@@ -0,0 +1,90 @@
/**
* Regression test for #2198 — advertised base64/entropy/homoglyph scanning
* never runs live. `scanEntropyAnomalies` + `shannonEntropy` were dead exports
* (zero callers outside their own unit tests). The live hooks inline their
* own pattern subsets "for hook independence" and never call these functions.
*
* This test asserts the chosen contract: the dead export was removed and the
* docs no longer over-claim entropy analysis as a live MUST.
*
* Contract: `scanForInjection` is retained — it serves as the CI codebase
* scanner engine (tests/prompt-injection-scan.security.test.cjs). It is NOT
* called from live hooks; hooks inline their own patterns.
*/
'use strict';
const { describe, test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('fs');
const path = require('path');
const PROJECT_ROOT = path.join(__dirname, '..');
describe('#2198 regression: dead scan exports removed, docs corrected', () => {
test('scanEntropyAnomalies is no longer exported from security.cjs', () => {
const security = require('../gsd-core/bin/lib/security.cjs');
assert.equal(
security.scanEntropyAnomalies,
undefined,
'scanEntropyAnomalies should have been removed as a dead export (#2198)'
);
});
test('shannonEntropy is not accessible from the security module', () => {
const security = require('../gsd-core/bin/lib/security.cjs');
assert.equal(
security.shannonEntropy,
undefined,
'shannonEntropy was the private helper for the removed scanEntropyAnomalies'
);
});
test('scanForInjection is retained (CI codebase scanner uses it)', () => {
const security = require('../gsd-core/bin/lib/security.cjs');
assert.equal(
typeof security.scanForInjection,
'function',
'scanForInjection is retained: it serves as the CI codebase scanner engine'
);
});
test('FEATURES.md does not over-claim entropy analysis as a live MUST', () => {
const features = fs.readFileSync(
path.join(PROJECT_ROOT, 'docs', 'FEATURES.md'),
'utf-8'
);
assert.ok(
!features.includes('REQ-SCAN-INJ-03: Scanner MUST apply entropy analysis'),
'REQ-SCAN-INJ-03 should not claim entropy analysis runs as a live MUST — ' +
'the implementation was dead code (#2198)'
);
});
test('FEATURES.md documents that base64-decode is CI-only, not live', () => {
const features = fs.readFileSync(
path.join(PROJECT_ROOT, 'docs', 'FEATURES.md'),
'utf-8'
);
assert.ok(
features.includes('CI-time control'),
'FEATURES.md should note base64-decode is a CI-time control, not a live hook (#2198)'
);
});
test('live hooks inline patterns independently (do not import security.cjs)', () => {
const hookFiles = [
'hooks/gsd-prompt-guard.js',
'hooks/gsd-read-injection-scanner.js',
];
for (const relPath of hookFiles) {
const fullPath = path.join(PROJECT_ROOT, relPath);
const source = fs.readFileSync(fullPath, 'utf-8');
assert.ok(
!source.match(/require\s*\(\s*['"][^'"]*security\.(cjs|js)['"]\s*\)/) &&
!source.match(/import\s+.*from\s+['"][^'"]*security\.(cjs|js)['"]\s*;?/),
`${relPath} must not require/import security.cjs — hooks inline patterns for independence`
);
}
});
});

View File

@@ -21,7 +21,6 @@ const {
validateFieldName,
validateShellArg,
validatePromptStructure,
scanEntropyAnomalies,
} = require('../gsd-core/bin/lib/security.cjs');
// ─── Path Traversal Prevention ──────────────────────────────────────────────
@@ -765,79 +764,8 @@ describe('validatePromptStructure', () => {
});
});
// ─── Layer 4: Paragraph-Level Entropy Anomaly Detection ─────────────────────
describe('scanEntropyAnomalies', () => {
test('is exported from security.cjs', () => {
assert.equal(typeof scanEntropyAnomalies, 'function');
});
test('returns { clean, findings } shape', () => {
const result = scanEntropyAnomalies('Normal text here.');
assert.ok(typeof result.clean === 'boolean');
assert.ok(Array.isArray(result.findings));
});
test('clean natural language text passes', () => {
const text = [
'Build an authentication system with JWT tokens.',
'',
'The system should support login, logout, and token refresh.',
].join('\n');
const result = scanEntropyAnomalies(text);
assert.ok(result.clean, `Expected clean but got: ${result.findings.join(', ')}`);
});
test('detects high-entropy paragraph (random-character content)', () => {
// A string cycling through 90 distinct chars has entropy ~6.4 bits/char, well above 5.5 threshold
const highEntropyPara = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=!@#$%^&*()_-[]{}|;:,.<>?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr';
const result = scanEntropyAnomalies(highEntropyPara);
assert.ok(!result.clean, 'should detect high-entropy paragraph');
assert.ok(
result.findings.some(f => f.includes('High-entropy paragraph')),
'finding should mention high-entropy paragraph'
);
});
test('finding includes entropy value in bits/char', () => {
const highEntropyPara = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=!@#$%^&*()_-[]{}|;:,.<>?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr';
const result = scanEntropyAnomalies(highEntropyPara);
assert.ok(result.findings.some(f => f.includes('bits/char')));
});
test('skips paragraphs shorter than or equal to 50 chars', () => {
// Even a high-entropy short paragraph should not be flagged
const shortPara = 'SGVsbG8gV29ybGQ='; // 16 chars — under 50
const result = scanEntropyAnomalies(shortPara);
assert.ok(result.clean, 'short paragraphs should be skipped');
});
test('handles empty text gracefully', () => {
const result = scanEntropyAnomalies('');
assert.ok(result.clean);
assert.equal(result.findings.length, 0);
});
test('handles null gracefully', () => {
const result = scanEntropyAnomalies(null);
assert.ok(result.clean);
assert.equal(result.findings.length, 0);
});
test('multiple paragraphs — flags only high-entropy ones', () => {
const highEntropyPara = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=!@#$%^&*()_-[]{}|;:,.<>?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr';
const text = [
'This is a perfectly normal English sentence describing a feature.',
'',
highEntropyPara,
'',
'Another clean sentence about the authentication requirements.',
].join('\n');
const result = scanEntropyAnomalies(text);
assert.ok(!result.clean);
assert.equal(result.findings.length, 1, 'only 1 high-entropy paragraph should be flagged');
});
});
// NOTE (#2198): scanEntropyAnomalies test block removed — the function was a
// dead export (zero production callers) and has been deleted from security.cts.
// ────────────────────────────────────────────────────────────────────────