gsd-code-first 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.ja-JP.md +834 -0
- package/README.ko-KR.md +823 -0
- package/README.md +937 -0
- package/README.pt-BR.md +452 -0
- package/README.zh-CN.md +800 -0
- package/agents/gsd-advisor-researcher.md +104 -0
- package/agents/gsd-annotator.md +148 -0
- package/agents/gsd-arc-executor.md +537 -0
- package/agents/gsd-arc-planner.md +374 -0
- package/agents/gsd-assumptions-analyzer.md +105 -0
- package/agents/gsd-code-planner.md +155 -0
- package/agents/gsd-codebase-mapper.md +770 -0
- package/agents/gsd-debugger.md +1373 -0
- package/agents/gsd-executor.md +509 -0
- package/agents/gsd-integration-checker.md +443 -0
- package/agents/gsd-nyquist-auditor.md +176 -0
- package/agents/gsd-phase-researcher.md +698 -0
- package/agents/gsd-plan-checker.md +773 -0
- package/agents/gsd-planner.md +1354 -0
- package/agents/gsd-project-researcher.md +654 -0
- package/agents/gsd-prototyper.md +161 -0
- package/agents/gsd-research-synthesizer.md +247 -0
- package/agents/gsd-roadmapper.md +679 -0
- package/agents/gsd-ui-auditor.md +439 -0
- package/agents/gsd-ui-checker.md +300 -0
- package/agents/gsd-ui-researcher.md +357 -0
- package/agents/gsd-user-profiler.md +171 -0
- package/agents/gsd-verifier.md +700 -0
- package/bin/install.js +5009 -0
- package/commands/gsd/add-backlog.md +76 -0
- package/commands/gsd/add-phase.md +43 -0
- package/commands/gsd/add-tests.md +41 -0
- package/commands/gsd/add-todo.md +47 -0
- package/commands/gsd/annotate.md +54 -0
- package/commands/gsd/audit-milestone.md +36 -0
- package/commands/gsd/audit-uat.md +24 -0
- package/commands/gsd/autonomous.md +41 -0
- package/commands/gsd/check-todos.md +45 -0
- package/commands/gsd/cleanup.md +18 -0
- package/commands/gsd/complete-milestone.md +136 -0
- package/commands/gsd/debug.md +173 -0
- package/commands/gsd/deep-plan.md +52 -0
- package/commands/gsd/discuss-phase.md +64 -0
- package/commands/gsd/do.md +30 -0
- package/commands/gsd/execute-phase.md +59 -0
- package/commands/gsd/extract-plan.md +35 -0
- package/commands/gsd/fast.md +30 -0
- package/commands/gsd/forensics.md +56 -0
- package/commands/gsd/health.md +22 -0
- package/commands/gsd/help.md +22 -0
- package/commands/gsd/insert-phase.md +32 -0
- package/commands/gsd/iterate.md +124 -0
- package/commands/gsd/join-discord.md +18 -0
- package/commands/gsd/list-phase-assumptions.md +46 -0
- package/commands/gsd/list-workspaces.md +19 -0
- package/commands/gsd/manager.md +39 -0
- package/commands/gsd/map-codebase.md +71 -0
- package/commands/gsd/milestone-summary.md +51 -0
- package/commands/gsd/new-milestone.md +44 -0
- package/commands/gsd/new-project.md +42 -0
- package/commands/gsd/new-workspace.md +44 -0
- package/commands/gsd/next.md +24 -0
- package/commands/gsd/note.md +34 -0
- package/commands/gsd/pause-work.md +38 -0
- package/commands/gsd/plan-milestone-gaps.md +34 -0
- package/commands/gsd/plan-phase.md +47 -0
- package/commands/gsd/plant-seed.md +28 -0
- package/commands/gsd/pr-branch.md +25 -0
- package/commands/gsd/profile-user.md +46 -0
- package/commands/gsd/progress.md +24 -0
- package/commands/gsd/prototype.md +56 -0
- package/commands/gsd/quick.md +47 -0
- package/commands/gsd/reapply-patches.md +123 -0
- package/commands/gsd/remove-phase.md +31 -0
- package/commands/gsd/remove-workspace.md +26 -0
- package/commands/gsd/research-phase.md +195 -0
- package/commands/gsd/resume-work.md +40 -0
- package/commands/gsd/review-backlog.md +61 -0
- package/commands/gsd/review.md +37 -0
- package/commands/gsd/session-report.md +19 -0
- package/commands/gsd/set-mode.md +41 -0
- package/commands/gsd/set-profile.md +12 -0
- package/commands/gsd/settings.md +36 -0
- package/commands/gsd/ship.md +23 -0
- package/commands/gsd/stats.md +18 -0
- package/commands/gsd/thread.md +127 -0
- package/commands/gsd/ui-phase.md +34 -0
- package/commands/gsd/ui-review.md +32 -0
- package/commands/gsd/update.md +37 -0
- package/commands/gsd/validate-phase.md +35 -0
- package/commands/gsd/verify-work.md +38 -0
- package/commands/gsd/workstreams.md +63 -0
- package/get-shit-done/bin/gsd-tools.cjs +946 -0
- package/get-shit-done/bin/lib/arc-scanner.cjs +341 -0
- package/get-shit-done/bin/lib/commands.cjs +959 -0
- package/get-shit-done/bin/lib/config.cjs +466 -0
- package/get-shit-done/bin/lib/core.cjs +1230 -0
- package/get-shit-done/bin/lib/frontmatter.cjs +336 -0
- package/get-shit-done/bin/lib/init.cjs +1442 -0
- package/get-shit-done/bin/lib/milestone.cjs +252 -0
- package/get-shit-done/bin/lib/model-profiles.cjs +68 -0
- package/get-shit-done/bin/lib/phase.cjs +888 -0
- package/get-shit-done/bin/lib/profile-output.cjs +952 -0
- package/get-shit-done/bin/lib/profile-pipeline.cjs +539 -0
- package/get-shit-done/bin/lib/roadmap.cjs +329 -0
- package/get-shit-done/bin/lib/security.cjs +382 -0
- package/get-shit-done/bin/lib/state.cjs +1031 -0
- package/get-shit-done/bin/lib/template.cjs +222 -0
- package/get-shit-done/bin/lib/uat.cjs +282 -0
- package/get-shit-done/bin/lib/verify.cjs +888 -0
- package/get-shit-done/bin/lib/workstream.cjs +491 -0
- package/get-shit-done/commands/gsd/workstreams.md +63 -0
- package/get-shit-done/references/arc-standard.md +315 -0
- package/get-shit-done/references/checkpoints.md +778 -0
- package/get-shit-done/references/continuation-format.md +249 -0
- package/get-shit-done/references/decimal-phase-calculation.md +64 -0
- package/get-shit-done/references/git-integration.md +295 -0
- package/get-shit-done/references/git-planning-commit.md +38 -0
- package/get-shit-done/references/model-profile-resolution.md +36 -0
- package/get-shit-done/references/model-profiles.md +139 -0
- package/get-shit-done/references/phase-argument-parsing.md +61 -0
- package/get-shit-done/references/planning-config.md +202 -0
- package/get-shit-done/references/questioning.md +162 -0
- package/get-shit-done/references/tdd.md +263 -0
- package/get-shit-done/references/ui-brand.md +160 -0
- package/get-shit-done/references/user-profiling.md +681 -0
- package/get-shit-done/references/verification-patterns.md +612 -0
- package/get-shit-done/references/workstream-flag.md +58 -0
- package/get-shit-done/templates/DEBUG.md +164 -0
- package/get-shit-done/templates/UAT.md +265 -0
- package/get-shit-done/templates/UI-SPEC.md +100 -0
- package/get-shit-done/templates/VALIDATION.md +76 -0
- package/get-shit-done/templates/claude-md.md +122 -0
- package/get-shit-done/templates/codebase/architecture.md +255 -0
- package/get-shit-done/templates/codebase/concerns.md +310 -0
- package/get-shit-done/templates/codebase/conventions.md +307 -0
- package/get-shit-done/templates/codebase/integrations.md +280 -0
- package/get-shit-done/templates/codebase/stack.md +186 -0
- package/get-shit-done/templates/codebase/structure.md +285 -0
- package/get-shit-done/templates/codebase/testing.md +480 -0
- package/get-shit-done/templates/config.json +44 -0
- package/get-shit-done/templates/context.md +352 -0
- package/get-shit-done/templates/continue-here.md +78 -0
- package/get-shit-done/templates/copilot-instructions.md +7 -0
- package/get-shit-done/templates/debug-subagent-prompt.md +91 -0
- package/get-shit-done/templates/dev-preferences.md +21 -0
- package/get-shit-done/templates/discovery.md +146 -0
- package/get-shit-done/templates/discussion-log.md +63 -0
- package/get-shit-done/templates/milestone-archive.md +123 -0
- package/get-shit-done/templates/milestone.md +115 -0
- package/get-shit-done/templates/phase-prompt.md +610 -0
- package/get-shit-done/templates/planner-subagent-prompt.md +117 -0
- package/get-shit-done/templates/project.md +186 -0
- package/get-shit-done/templates/requirements.md +231 -0
- package/get-shit-done/templates/research-project/ARCHITECTURE.md +204 -0
- package/get-shit-done/templates/research-project/FEATURES.md +147 -0
- package/get-shit-done/templates/research-project/PITFALLS.md +200 -0
- package/get-shit-done/templates/research-project/STACK.md +120 -0
- package/get-shit-done/templates/research-project/SUMMARY.md +170 -0
- package/get-shit-done/templates/research.md +552 -0
- package/get-shit-done/templates/retrospective.md +54 -0
- package/get-shit-done/templates/roadmap.md +202 -0
- package/get-shit-done/templates/state.md +176 -0
- package/get-shit-done/templates/summary-complex.md +59 -0
- package/get-shit-done/templates/summary-minimal.md +41 -0
- package/get-shit-done/templates/summary-standard.md +48 -0
- package/get-shit-done/templates/summary.md +248 -0
- package/get-shit-done/templates/user-profile.md +146 -0
- package/get-shit-done/templates/user-setup.md +311 -0
- package/get-shit-done/templates/verification-report.md +322 -0
- package/get-shit-done/workflows/add-phase.md +112 -0
- package/get-shit-done/workflows/add-tests.md +351 -0
- package/get-shit-done/workflows/add-todo.md +158 -0
- package/get-shit-done/workflows/audit-milestone.md +340 -0
- package/get-shit-done/workflows/audit-uat.md +109 -0
- package/get-shit-done/workflows/autonomous.md +891 -0
- package/get-shit-done/workflows/check-todos.md +177 -0
- package/get-shit-done/workflows/cleanup.md +152 -0
- package/get-shit-done/workflows/complete-milestone.md +767 -0
- package/get-shit-done/workflows/diagnose-issues.md +231 -0
- package/get-shit-done/workflows/discovery-phase.md +289 -0
- package/get-shit-done/workflows/discuss-phase-assumptions.md +653 -0
- package/get-shit-done/workflows/discuss-phase.md +1049 -0
- package/get-shit-done/workflows/do.md +104 -0
- package/get-shit-done/workflows/execute-phase.md +846 -0
- package/get-shit-done/workflows/execute-plan.md +514 -0
- package/get-shit-done/workflows/fast.md +105 -0
- package/get-shit-done/workflows/forensics.md +265 -0
- package/get-shit-done/workflows/health.md +181 -0
- package/get-shit-done/workflows/help.md +634 -0
- package/get-shit-done/workflows/insert-phase.md +130 -0
- package/get-shit-done/workflows/list-phase-assumptions.md +178 -0
- package/get-shit-done/workflows/list-workspaces.md +56 -0
- package/get-shit-done/workflows/manager.md +362 -0
- package/get-shit-done/workflows/map-codebase.md +377 -0
- package/get-shit-done/workflows/milestone-summary.md +223 -0
- package/get-shit-done/workflows/new-milestone.md +486 -0
- package/get-shit-done/workflows/new-project.md +1250 -0
- package/get-shit-done/workflows/new-workspace.md +237 -0
- package/get-shit-done/workflows/next.md +97 -0
- package/get-shit-done/workflows/node-repair.md +92 -0
- package/get-shit-done/workflows/note.md +156 -0
- package/get-shit-done/workflows/pause-work.md +176 -0
- package/get-shit-done/workflows/plan-milestone-gaps.md +273 -0
- package/get-shit-done/workflows/plan-phase.md +859 -0
- package/get-shit-done/workflows/plant-seed.md +169 -0
- package/get-shit-done/workflows/pr-branch.md +129 -0
- package/get-shit-done/workflows/profile-user.md +450 -0
- package/get-shit-done/workflows/progress.md +507 -0
- package/get-shit-done/workflows/quick.md +757 -0
- package/get-shit-done/workflows/remove-phase.md +155 -0
- package/get-shit-done/workflows/remove-workspace.md +90 -0
- package/get-shit-done/workflows/research-phase.md +82 -0
- package/get-shit-done/workflows/resume-project.md +326 -0
- package/get-shit-done/workflows/review.md +228 -0
- package/get-shit-done/workflows/session-report.md +146 -0
- package/get-shit-done/workflows/settings.md +283 -0
- package/get-shit-done/workflows/ship.md +228 -0
- package/get-shit-done/workflows/stats.md +60 -0
- package/get-shit-done/workflows/transition.md +671 -0
- package/get-shit-done/workflows/ui-phase.md +302 -0
- package/get-shit-done/workflows/ui-review.md +165 -0
- package/get-shit-done/workflows/update.md +323 -0
- package/get-shit-done/workflows/validate-phase.md +174 -0
- package/get-shit-done/workflows/verify-phase.md +254 -0
- package/get-shit-done/workflows/verify-work.md +637 -0
- package/hooks/dist/gsd-check-update.js +114 -0
- package/hooks/dist/gsd-context-monitor.js +156 -0
- package/hooks/dist/gsd-prompt-guard.js +96 -0
- package/hooks/dist/gsd-statusline.js +119 -0
- package/hooks/dist/gsd-workflow-guard.js +94 -0
- package/package.json +52 -0
- package/scripts/base64-scan.sh +262 -0
- package/scripts/build-hooks.js +82 -0
- package/scripts/prompt-injection-scan.sh +198 -0
- package/scripts/run-tests.cjs +29 -0
- package/scripts/secret-scan.sh +227 -0
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Security — Input validation, path traversal prevention, and prompt injection guards
|
|
3
|
+
*
|
|
4
|
+
* This module centralizes security checks for GSD tooling. Because GSD generates
|
|
5
|
+
* markdown files that become LLM system prompts (agent instructions, workflow state,
|
|
6
|
+
* phase plans), any user-controlled text that flows into these files is a potential
|
|
7
|
+
* indirect prompt injection vector.
|
|
8
|
+
*
|
|
9
|
+
* Threat model:
|
|
10
|
+
* 1. Path traversal: user-supplied file paths escape the project directory
|
|
11
|
+
* 2. Prompt injection: malicious text in arguments/PRDs embeds LLM instructions
|
|
12
|
+
* 3. Shell metacharacter injection: user text interpreted by shell
|
|
13
|
+
* 4. JSON injection: malformed JSON crashes or corrupts state
|
|
14
|
+
* 5. Regex DoS: crafted input causes catastrophic backtracking
|
|
15
|
+
*/
|
|
16
|
+
'use strict';
|
|
17
|
+
|
|
18
|
+
const fs = require('fs');
|
|
19
|
+
const path = require('path');
|
|
20
|
+
|
|
21
|
+
// ─── Path Traversal Prevention ──────────────────────────────────────────────
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Validate that a file path resolves within an allowed base directory.
|
|
25
|
+
* Prevents path traversal attacks via ../ sequences, symlinks, or absolute paths.
|
|
26
|
+
*
|
|
27
|
+
* @param {string} filePath - The user-supplied file path
|
|
28
|
+
* @param {string} baseDir - The allowed base directory (e.g., project root)
|
|
29
|
+
* @param {object} [opts] - Options
|
|
30
|
+
* @param {boolean} [opts.allowAbsolute=false] - Allow absolute paths (still must be within baseDir)
|
|
31
|
+
* @returns {{ safe: boolean, resolved: string, error?: string }}
|
|
32
|
+
*/
|
|
33
|
+
function validatePath(filePath, baseDir, opts = {}) {
|
|
34
|
+
if (!filePath || typeof filePath !== 'string') {
|
|
35
|
+
return { safe: false, resolved: '', error: 'Empty or invalid file path' };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (!baseDir || typeof baseDir !== 'string') {
|
|
39
|
+
return { safe: false, resolved: '', error: 'Empty or invalid base directory' };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Reject null bytes (can bypass path checks in some environments)
|
|
43
|
+
if (filePath.includes('\0')) {
|
|
44
|
+
return { safe: false, resolved: '', error: 'Path contains null bytes' };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Resolve symlinks in base directory to handle macOS /var -> /private/var
|
|
48
|
+
// and similar platform-specific symlink chains
|
|
49
|
+
let resolvedBase;
|
|
50
|
+
try {
|
|
51
|
+
resolvedBase = fs.realpathSync(path.resolve(baseDir));
|
|
52
|
+
} catch {
|
|
53
|
+
resolvedBase = path.resolve(baseDir);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let resolvedPath;
|
|
57
|
+
|
|
58
|
+
if (path.isAbsolute(filePath)) {
|
|
59
|
+
if (!opts.allowAbsolute) {
|
|
60
|
+
return { safe: false, resolved: '', error: 'Absolute paths not allowed' };
|
|
61
|
+
}
|
|
62
|
+
resolvedPath = path.resolve(filePath);
|
|
63
|
+
} else {
|
|
64
|
+
resolvedPath = path.resolve(baseDir, filePath);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Resolve symlinks in the target path too
|
|
68
|
+
try {
|
|
69
|
+
resolvedPath = fs.realpathSync(resolvedPath);
|
|
70
|
+
} catch {
|
|
71
|
+
// File may not exist yet (e.g., about to be created) — use logical resolution
|
|
72
|
+
// but still resolve the parent directory if it exists
|
|
73
|
+
const parentDir = path.dirname(resolvedPath);
|
|
74
|
+
try {
|
|
75
|
+
const realParent = fs.realpathSync(parentDir);
|
|
76
|
+
resolvedPath = path.join(realParent, path.basename(resolvedPath));
|
|
77
|
+
} catch {
|
|
78
|
+
// Parent doesn't exist either — keep the resolved path as-is
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Normalize both paths and check containment
|
|
83
|
+
const normalizedBase = resolvedBase + path.sep;
|
|
84
|
+
const normalizedPath = resolvedPath + path.sep;
|
|
85
|
+
|
|
86
|
+
// The resolved path must start with the base directory
|
|
87
|
+
// (or be exactly the base directory)
|
|
88
|
+
if (resolvedPath !== resolvedBase && !normalizedPath.startsWith(normalizedBase)) {
|
|
89
|
+
return {
|
|
90
|
+
safe: false,
|
|
91
|
+
resolved: resolvedPath,
|
|
92
|
+
error: `Path escapes allowed directory: ${resolvedPath} is outside ${resolvedBase}`,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return { safe: true, resolved: resolvedPath };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Validate a file path and throw on traversal attempt.
|
|
101
|
+
* Convenience wrapper around validatePath for use in CLI commands.
|
|
102
|
+
*/
|
|
103
|
+
function requireSafePath(filePath, baseDir, label, opts = {}) {
|
|
104
|
+
const result = validatePath(filePath, baseDir, opts);
|
|
105
|
+
if (!result.safe) {
|
|
106
|
+
throw new Error(`${label || 'Path'} validation failed: ${result.error}`);
|
|
107
|
+
}
|
|
108
|
+
return result.resolved;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// ─── Prompt Injection Detection ─────────────────────────────────────────────
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Patterns that indicate prompt injection attempts in user-supplied text.
|
|
115
|
+
* These patterns catch common indirect prompt injection techniques where
|
|
116
|
+
* an attacker embeds LLM instructions in text that will be read by an agent.
|
|
117
|
+
*
|
|
118
|
+
* Note: This is defense-in-depth — not a complete solution. The primary defense
|
|
119
|
+
* is proper input/output boundaries in agent prompts.
|
|
120
|
+
*/
|
|
121
|
+
const INJECTION_PATTERNS = [
|
|
122
|
+
// Direct instruction override attempts
|
|
123
|
+
/ignore\s+(all\s+)?previous\s+instructions/i,
|
|
124
|
+
/ignore\s+(all\s+)?above\s+instructions/i,
|
|
125
|
+
/disregard\s+(all\s+)?previous/i,
|
|
126
|
+
/forget\s+(all\s+)?(your\s+)?instructions/i,
|
|
127
|
+
/override\s+(system|previous)\s+(prompt|instructions)/i,
|
|
128
|
+
|
|
129
|
+
// Role/identity manipulation
|
|
130
|
+
/you\s+are\s+now\s+(?:a|an|the)\s+/i,
|
|
131
|
+
/act\s+as\s+(?:a|an|the)\s+(?!plan|phase|wave)/i, // allow "act as a plan"
|
|
132
|
+
/pretend\s+(?:you(?:'re| are)\s+|to\s+be\s+)/i,
|
|
133
|
+
/from\s+now\s+on,?\s+you\s+(?:are|will|should|must)/i,
|
|
134
|
+
|
|
135
|
+
// System prompt extraction
|
|
136
|
+
/(?:print|output|reveal|show|display|repeat)\s+(?:your\s+)?(?:system\s+)?(?:prompt|instructions)/i,
|
|
137
|
+
/what\s+(?:are|is)\s+your\s+(?:system\s+)?(?:prompt|instructions)/i,
|
|
138
|
+
|
|
139
|
+
// Hidden instruction markers (XML/HTML tags that mimic system messages)
|
|
140
|
+
// Note: <instructions> is excluded — GSD uses it as legitimate prompt structure
|
|
141
|
+
// Requires > to close the tag (not just whitespace) to avoid matching generic types like Promise<User | null>
|
|
142
|
+
/<\/?(?:system|assistant|human)>/i,
|
|
143
|
+
/\[SYSTEM\]/i,
|
|
144
|
+
/\[INST\]/i,
|
|
145
|
+
/<<\s*SYS\s*>>/i,
|
|
146
|
+
|
|
147
|
+
// Exfiltration attempts
|
|
148
|
+
/(?:send|post|fetch|curl|wget)\s+(?:to|from)\s+https?:\/\//i,
|
|
149
|
+
/(?:base64|btoa|encode)\s+(?:and\s+)?(?:send|exfiltrate|output)/i,
|
|
150
|
+
|
|
151
|
+
// Tool manipulation
|
|
152
|
+
/(?:run|execute|call|invoke)\s+(?:the\s+)?(?:bash|shell|exec|spawn)\s+(?:tool|command)/i,
|
|
153
|
+
];
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Scan text for potential prompt injection patterns.
|
|
157
|
+
* Returns an array of findings (empty = clean).
|
|
158
|
+
*
|
|
159
|
+
* @param {string} text - The text to scan
|
|
160
|
+
* @param {object} [opts] - Options
|
|
161
|
+
* @param {boolean} [opts.strict=false] - Enable stricter matching (more false positives)
|
|
162
|
+
* @returns {{ clean: boolean, findings: string[] }}
|
|
163
|
+
*/
|
|
164
|
+
function scanForInjection(text, opts = {}) {
|
|
165
|
+
if (!text || typeof text !== 'string') {
|
|
166
|
+
return { clean: true, findings: [] };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const findings = [];
|
|
170
|
+
|
|
171
|
+
for (const pattern of INJECTION_PATTERNS) {
|
|
172
|
+
if (pattern.test(text)) {
|
|
173
|
+
findings.push(`Matched injection pattern: ${pattern.source}`);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (opts.strict) {
|
|
178
|
+
// Check for suspicious Unicode that could hide instructions
|
|
179
|
+
// (zero-width chars, RTL override, homoglyph attacks)
|
|
180
|
+
if (/[\u200B-\u200F\u2028-\u202F\uFEFF\u00AD]/.test(text)) {
|
|
181
|
+
findings.push('Contains suspicious zero-width or invisible Unicode characters');
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Check for extremely long strings that could be prompt stuffing
|
|
185
|
+
if (text.length > 50000) {
|
|
186
|
+
findings.push(`Suspicious text length: ${text.length} chars (potential prompt stuffing)`);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return { clean: findings.length === 0, findings };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Sanitize text that will be embedded in agent prompts or planning documents.
|
|
195
|
+
* Strips known injection markers while preserving legitimate content.
|
|
196
|
+
*
|
|
197
|
+
* This does NOT alter user intent — it neutralizes control characters and
|
|
198
|
+
* instruction-mimicking patterns that could hijack agent behavior.
|
|
199
|
+
*
|
|
200
|
+
* @param {string} text - Text to sanitize
|
|
201
|
+
* @returns {string} Sanitized text
|
|
202
|
+
*/
|
|
203
|
+
function sanitizeForPrompt(text) {
|
|
204
|
+
if (!text || typeof text !== 'string') return text;
|
|
205
|
+
|
|
206
|
+
let sanitized = text;
|
|
207
|
+
|
|
208
|
+
// Strip zero-width characters that could hide instructions
|
|
209
|
+
sanitized = sanitized.replace(/[\u200B-\u200F\u2028-\u202F\uFEFF\u00AD]/g, '');
|
|
210
|
+
|
|
211
|
+
// Neutralize XML/HTML tags that mimic system boundaries
|
|
212
|
+
// Replace < > with full-width equivalents to prevent tag interpretation
|
|
213
|
+
// Note: <instructions> is excluded — GSD uses it as legitimate prompt structure
|
|
214
|
+
sanitized = sanitized.replace(/<(\/?)(?:system|assistant|human)>/gi,
|
|
215
|
+
(_, slash) => `<${slash || ''}system-text>`);
|
|
216
|
+
|
|
217
|
+
// Neutralize [SYSTEM] / [INST] markers
|
|
218
|
+
sanitized = sanitized.replace(/\[(SYSTEM|INST)\]/gi, '[$1-TEXT]');
|
|
219
|
+
|
|
220
|
+
// Neutralize <<SYS>> markers
|
|
221
|
+
sanitized = sanitized.replace(/<<\s*SYS\s*>>/gi, '«SYS-TEXT»');
|
|
222
|
+
|
|
223
|
+
return sanitized;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Sanitize text that will be displayed back to the user.
|
|
228
|
+
* Removes protocol-like leak markers that should never surface in checkpoints.
|
|
229
|
+
*
|
|
230
|
+
* @param {string} text - Text to sanitize
|
|
231
|
+
* @returns {string} Sanitized text
|
|
232
|
+
*/
|
|
233
|
+
function sanitizeForDisplay(text) {
|
|
234
|
+
if (!text || typeof text !== 'string') return text;
|
|
235
|
+
|
|
236
|
+
let sanitized = sanitizeForPrompt(text);
|
|
237
|
+
|
|
238
|
+
const protocolLeakPatterns = [
|
|
239
|
+
/^\s*(?:assistant|user|system)\s+to=[^:\s]+:[^\n]+$/i,
|
|
240
|
+
/^\s*<\|(?:assistant|user|system)[^|]*\|>\s*$/i,
|
|
241
|
+
];
|
|
242
|
+
|
|
243
|
+
sanitized = sanitized
|
|
244
|
+
.split('\n')
|
|
245
|
+
.filter(line => !protocolLeakPatterns.some(pattern => pattern.test(line)))
|
|
246
|
+
.join('\n');
|
|
247
|
+
|
|
248
|
+
return sanitized;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// ─── Shell Safety ───────────────────────────────────────────────────────────
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Validate that a string is safe to use as a shell argument when quoted.
|
|
255
|
+
* This is a defense-in-depth check — callers should always use array-based
|
|
256
|
+
* exec (spawnSync) where possible.
|
|
257
|
+
*
|
|
258
|
+
* @param {string} value - The value to check
|
|
259
|
+
* @param {string} label - Description for error messages
|
|
260
|
+
* @returns {string} The validated value
|
|
261
|
+
*/
|
|
262
|
+
function validateShellArg(value, label) {
|
|
263
|
+
if (!value || typeof value !== 'string') {
|
|
264
|
+
throw new Error(`${label || 'Argument'}: empty or invalid value`);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// Reject null bytes
|
|
268
|
+
if (value.includes('\0')) {
|
|
269
|
+
throw new Error(`${label || 'Argument'}: contains null bytes`);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// Reject command substitution attempts
|
|
273
|
+
if (/[$`]/.test(value) && /\$\(|`/.test(value)) {
|
|
274
|
+
throw new Error(`${label || 'Argument'}: contains potential command substitution`);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
return value;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// ─── JSON Safety ────────────────────────────────────────────────────────────
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Safely parse JSON with error handling and optional size limits.
|
|
284
|
+
* Wraps JSON.parse to prevent uncaught exceptions from malformed input.
|
|
285
|
+
*
|
|
286
|
+
* @param {string} text - JSON string to parse
|
|
287
|
+
* @param {object} [opts] - Options
|
|
288
|
+
* @param {number} [opts.maxLength=1048576] - Maximum input length (1MB default)
|
|
289
|
+
* @param {string} [opts.label='JSON'] - Description for error messages
|
|
290
|
+
* @returns {{ ok: boolean, value?: any, error?: string }}
|
|
291
|
+
*/
|
|
292
|
+
function safeJsonParse(text, opts = {}) {
|
|
293
|
+
const maxLength = opts.maxLength || 1048576;
|
|
294
|
+
const label = opts.label || 'JSON';
|
|
295
|
+
|
|
296
|
+
if (!text || typeof text !== 'string') {
|
|
297
|
+
return { ok: false, error: `${label}: empty or invalid input` };
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (text.length > maxLength) {
|
|
301
|
+
return { ok: false, error: `${label}: input exceeds ${maxLength} byte limit (got ${text.length})` };
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
try {
|
|
305
|
+
const value = JSON.parse(text);
|
|
306
|
+
return { ok: true, value };
|
|
307
|
+
} catch (err) {
|
|
308
|
+
return { ok: false, error: `${label}: parse error — ${err.message}` };
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// ─── Phase/Argument Validation ──────────────────────────────────────────────
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Validate a phase number argument.
|
|
316
|
+
* Phase numbers must match: integer, decimal (2.1), or letter suffix (12A).
|
|
317
|
+
* Rejects arbitrary strings that could be used for injection.
|
|
318
|
+
*
|
|
319
|
+
* @param {string} phase - The phase number to validate
|
|
320
|
+
* @returns {{ valid: boolean, normalized?: string, error?: string }}
|
|
321
|
+
*/
|
|
322
|
+
function validatePhaseNumber(phase) {
|
|
323
|
+
if (!phase || typeof phase !== 'string') {
|
|
324
|
+
return { valid: false, error: 'Phase number is required' };
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const trimmed = phase.trim();
|
|
328
|
+
|
|
329
|
+
// Standard numeric: 1, 01, 12A, 12.1, 12A.1.2
|
|
330
|
+
if (/^\d{1,4}[A-Z]?(?:\.\d{1,3})*$/i.test(trimmed)) {
|
|
331
|
+
return { valid: true, normalized: trimmed };
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Custom project IDs: PROJ-42, AUTH-101 (uppercase alphanumeric with hyphens)
|
|
335
|
+
if (/^[A-Z][A-Z0-9]*(?:-[A-Z0-9]+){1,4}$/i.test(trimmed) && trimmed.length <= 30) {
|
|
336
|
+
return { valid: true, normalized: trimmed };
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return { valid: false, error: `Invalid phase number format: "${trimmed}"` };
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Validate a STATE.md field name to prevent injection into regex patterns.
|
|
344
|
+
* Field names must be alphanumeric with spaces, hyphens, underscores, or dots.
|
|
345
|
+
*
|
|
346
|
+
* @param {string} field - The field name to validate
|
|
347
|
+
* @returns {{ valid: boolean, error?: string }}
|
|
348
|
+
*/
|
|
349
|
+
function validateFieldName(field) {
|
|
350
|
+
if (!field || typeof field !== 'string') {
|
|
351
|
+
return { valid: false, error: 'Field name is required' };
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// Allow typical field names: "Current Phase", "active_plan", "Phase 1.2"
|
|
355
|
+
if (/^[A-Za-z][A-Za-z0-9 _.\-/]{0,60}$/.test(field)) {
|
|
356
|
+
return { valid: true };
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
return { valid: false, error: `Invalid field name: "${field}"` };
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
module.exports = {
|
|
363
|
+
// Path safety
|
|
364
|
+
validatePath,
|
|
365
|
+
requireSafePath,
|
|
366
|
+
|
|
367
|
+
// Prompt injection
|
|
368
|
+
INJECTION_PATTERNS,
|
|
369
|
+
scanForInjection,
|
|
370
|
+
sanitizeForPrompt,
|
|
371
|
+
sanitizeForDisplay,
|
|
372
|
+
|
|
373
|
+
// Shell safety
|
|
374
|
+
validateShellArg,
|
|
375
|
+
|
|
376
|
+
// JSON safety
|
|
377
|
+
safeJsonParse,
|
|
378
|
+
|
|
379
|
+
// Input validation
|
|
380
|
+
validatePhaseNumber,
|
|
381
|
+
validateFieldName,
|
|
382
|
+
};
|