gsd-opencode 1.22.0 → 1.30.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/agents/gsd-advisor-researcher.md +112 -0
- package/agents/gsd-assumptions-analyzer.md +110 -0
- package/agents/gsd-codebase-mapper.md +1 -2
- package/agents/gsd-debugger.md +119 -2
- package/agents/gsd-executor.md +25 -4
- package/agents/gsd-integration-checker.md +1 -2
- package/agents/gsd-nyquist-auditor.md +1 -2
- package/agents/gsd-phase-researcher.md +151 -5
- package/agents/gsd-plan-checker.md +71 -5
- package/agents/gsd-planner.md +50 -4
- package/agents/gsd-project-researcher.md +29 -3
- package/agents/gsd-research-synthesizer.md +1 -2
- package/agents/gsd-roadmapper.md +30 -2
- package/agents/gsd-ui-auditor.md +445 -0
- package/agents/gsd-ui-checker.md +305 -0
- package/agents/gsd-ui-researcher.md +368 -0
- package/agents/gsd-user-profiler.md +173 -0
- package/agents/gsd-verifier.md +124 -4
- package/commands/gsd/gsd-add-backlog.md +76 -0
- package/commands/gsd/gsd-audit-uat.md +24 -0
- package/commands/gsd/gsd-autonomous.md +41 -0
- package/commands/gsd/gsd-debug.md +5 -0
- package/commands/gsd/gsd-discuss-phase.md +10 -36
- package/commands/gsd/gsd-do.md +30 -0
- package/commands/gsd/gsd-execute-phase.md +20 -2
- package/commands/gsd/gsd-fast.md +30 -0
- package/commands/gsd/gsd-forensics.md +56 -0
- package/commands/gsd/gsd-list-workspaces.md +19 -0
- package/commands/gsd/gsd-manager.md +39 -0
- package/commands/gsd/gsd-milestone-summary.md +51 -0
- package/commands/gsd/gsd-new-workspace.md +44 -0
- package/commands/gsd/gsd-next.md +24 -0
- package/commands/gsd/gsd-note.md +34 -0
- package/commands/gsd/gsd-plan-phase.md +3 -1
- package/commands/gsd/gsd-plant-seed.md +28 -0
- package/commands/gsd/gsd-pr-branch.md +25 -0
- package/commands/gsd/gsd-profile-user.md +46 -0
- package/commands/gsd/gsd-quick.md +4 -2
- package/commands/gsd/gsd-reapply-patches.md +10 -6
- package/commands/gsd/gsd-remove-workspace.md +26 -0
- package/commands/gsd/gsd-research-phase.md +5 -0
- package/commands/gsd/gsd-resume-work.md +1 -1
- package/commands/gsd/gsd-review-backlog.md +61 -0
- package/commands/gsd/gsd-review.md +37 -0
- package/commands/gsd/gsd-session-report.md +19 -0
- package/commands/gsd/gsd-set-profile.md +24 -23
- package/commands/gsd/gsd-ship.md +23 -0
- package/commands/gsd/gsd-stats.md +18 -0
- package/commands/gsd/gsd-thread.md +127 -0
- package/commands/gsd/gsd-ui-phase.md +34 -0
- package/commands/gsd/gsd-ui-review.md +32 -0
- package/commands/gsd/gsd-workstreams.md +66 -0
- package/get-shit-done/bin/gsd-tools.cjs +410 -84
- package/get-shit-done/bin/lib/commands.cjs +429 -18
- package/get-shit-done/bin/lib/config.cjs +318 -45
- package/get-shit-done/bin/lib/core.cjs +822 -84
- package/get-shit-done/bin/lib/frontmatter.cjs +78 -41
- package/get-shit-done/bin/lib/init.cjs +836 -104
- package/get-shit-done/bin/lib/milestone.cjs +44 -33
- package/get-shit-done/bin/lib/model-profiles.cjs +68 -0
- package/get-shit-done/bin/lib/phase.cjs +293 -306
- 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 +55 -24
- package/get-shit-done/bin/lib/security.cjs +382 -0
- package/get-shit-done/bin/lib/state.cjs +363 -53
- package/get-shit-done/bin/lib/template.cjs +2 -2
- package/get-shit-done/bin/lib/uat.cjs +282 -0
- package/get-shit-done/bin/lib/verify.cjs +104 -36
- package/get-shit-done/bin/lib/workstream.cjs +491 -0
- package/get-shit-done/references/checkpoints.md +12 -10
- package/get-shit-done/references/decimal-phase-calculation.md +2 -3
- package/get-shit-done/references/git-integration.md +47 -0
- package/get-shit-done/references/model-profile-resolution.md +2 -0
- package/get-shit-done/references/model-profiles.md +62 -16
- package/get-shit-done/references/phase-argument-parsing.md +2 -2
- package/get-shit-done/references/planning-config.md +3 -1
- package/get-shit-done/references/user-profiling.md +681 -0
- package/get-shit-done/references/workstream-flag.md +58 -0
- package/get-shit-done/templates/UAT.md +21 -3
- package/get-shit-done/templates/UI-SPEC.md +100 -0
- package/get-shit-done/templates/claude-md.md +122 -0
- package/get-shit-done/templates/config.json +10 -3
- package/get-shit-done/templates/context.md +61 -6
- package/get-shit-done/templates/dev-preferences.md +21 -0
- package/get-shit-done/templates/discussion-log.md +63 -0
- package/get-shit-done/templates/phase-prompt.md +46 -5
- package/get-shit-done/templates/project.md +2 -0
- package/get-shit-done/templates/state.md +2 -2
- package/get-shit-done/templates/user-profile.md +146 -0
- package/get-shit-done/workflows/add-phase.md +2 -2
- package/get-shit-done/workflows/add-tests.md +4 -4
- package/get-shit-done/workflows/add-todo.md +3 -3
- package/get-shit-done/workflows/audit-milestone.md +13 -5
- 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 +2 -2
- package/get-shit-done/workflows/cleanup.md +4 -4
- package/get-shit-done/workflows/complete-milestone.md +9 -6
- package/get-shit-done/workflows/diagnose-issues.md +15 -3
- package/get-shit-done/workflows/discovery-phase.md +2 -2
- package/get-shit-done/workflows/discuss-phase-assumptions.md +653 -0
- package/get-shit-done/workflows/discuss-phase.md +411 -38
- package/get-shit-done/workflows/do.md +104 -0
- package/get-shit-done/workflows/execute-phase.md +405 -18
- package/get-shit-done/workflows/execute-plan.md +77 -12
- 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 +28 -6
- package/get-shit-done/workflows/help.md +124 -7
- package/get-shit-done/workflows/insert-phase.md +2 -2
- package/get-shit-done/workflows/list-phase-assumptions.md +2 -2
- 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 +74 -13
- package/get-shit-done/workflows/milestone-summary.md +223 -0
- package/get-shit-done/workflows/new-milestone.md +120 -18
- package/get-shit-done/workflows/new-project.md +178 -39
- 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 +62 -8
- package/get-shit-done/workflows/plan-milestone-gaps.md +4 -5
- package/get-shit-done/workflows/plan-phase.md +332 -33
- 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 +145 -20
- package/get-shit-done/workflows/quick.md +205 -49
- package/get-shit-done/workflows/remove-phase.md +2 -2
- package/get-shit-done/workflows/remove-workspace.md +90 -0
- package/get-shit-done/workflows/research-phase.md +11 -3
- package/get-shit-done/workflows/resume-project.md +35 -16
- package/get-shit-done/workflows/review.md +228 -0
- package/get-shit-done/workflows/session-report.md +146 -0
- package/get-shit-done/workflows/set-profile.md +2 -2
- package/get-shit-done/workflows/settings.md +80 -11
- 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 +147 -20
- 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 +108 -25
- package/get-shit-done/workflows/validate-phase.md +15 -8
- package/get-shit-done/workflows/verify-phase.md +16 -5
- package/get-shit-done/workflows/verify-work.md +72 -18
- package/package.json +1 -1
- package/skills/gsd-audit-milestone/SKILL.md +29 -0
- package/skills/gsd-cleanup/SKILL.md +19 -0
- package/skills/gsd-complete-milestone/SKILL.md +131 -0
- package/skills/gsd-discuss-phase/SKILL.md +54 -0
- package/skills/gsd-execute-phase/SKILL.md +49 -0
- package/skills/gsd-plan-phase/SKILL.md +37 -0
- package/skills/gsd-ui-phase/SKILL.md +24 -0
- package/skills/gsd-ui-review/SKILL.md +24 -0
- package/skills/gsd-verify-work/SKILL.md +30 -0
|
@@ -4,10 +4,10 @@
|
|
|
4
4
|
|
|
5
5
|
const fs = require('fs');
|
|
6
6
|
const path = require('path');
|
|
7
|
-
const { escapeRegex, normalizePhaseName, output, error, findPhaseInternal } = require('./core.cjs');
|
|
7
|
+
const { escapeRegex, normalizePhaseName, planningPaths, output, error, findPhaseInternal, stripShippedMilestones, extractCurrentMilestone, replaceInCurrentMilestone } = require('./core.cjs');
|
|
8
8
|
|
|
9
9
|
function cmdRoadmapGetPhase(cwd, phaseNum, raw) {
|
|
10
|
-
const roadmapPath =
|
|
10
|
+
const roadmapPath = planningPaths(cwd).roadmap;
|
|
11
11
|
|
|
12
12
|
if (!fs.existsSync(roadmapPath)) {
|
|
13
13
|
output({ found: false, error: 'ROADMAP.md not found' }, raw, '');
|
|
@@ -15,7 +15,7 @@ function cmdRoadmapGetPhase(cwd, phaseNum, raw) {
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
try {
|
|
18
|
-
const content = fs.readFileSync(roadmapPath, 'utf-8');
|
|
18
|
+
const content = extractCurrentMilestone(fs.readFileSync(roadmapPath, 'utf-8'), cwd);
|
|
19
19
|
|
|
20
20
|
// Escape special regex chars in phase number, handle decimal
|
|
21
21
|
const escapedPhase = escapeRegex(phaseNum);
|
|
@@ -63,8 +63,8 @@ function cmdRoadmapGetPhase(cwd, phaseNum, raw) {
|
|
|
63
63
|
|
|
64
64
|
const section = content.slice(headerIndex, sectionEnd).trim();
|
|
65
65
|
|
|
66
|
-
// Extract goal if present
|
|
67
|
-
const goalMatch = section.match(/\*\*Goal
|
|
66
|
+
// Extract goal if present (supports both **Goal:** and **Goal**: formats)
|
|
67
|
+
const goalMatch = section.match(/\*\*Goal(?::\*\*|\*\*:)\s*([^\n]+)/i);
|
|
68
68
|
const goal = goalMatch ? goalMatch[1].trim() : null;
|
|
69
69
|
|
|
70
70
|
// Extract success criteria as structured array
|
|
@@ -91,15 +91,16 @@ function cmdRoadmapGetPhase(cwd, phaseNum, raw) {
|
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
function cmdRoadmapAnalyze(cwd, raw) {
|
|
94
|
-
const roadmapPath =
|
|
94
|
+
const roadmapPath = planningPaths(cwd).roadmap;
|
|
95
95
|
|
|
96
96
|
if (!fs.existsSync(roadmapPath)) {
|
|
97
97
|
output({ error: 'ROADMAP.md not found', milestones: [], phases: [], current_phase: null }, raw);
|
|
98
98
|
return;
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
-
const
|
|
102
|
-
const
|
|
101
|
+
const rawContent = fs.readFileSync(roadmapPath, 'utf-8');
|
|
102
|
+
const content = extractCurrentMilestone(rawContent, cwd);
|
|
103
|
+
const phasesDir = planningPaths(cwd).phases;
|
|
103
104
|
|
|
104
105
|
// Extract all phase headings: ## Phase N: Name or ### Phase N: Name
|
|
105
106
|
const phasePattern = /#{2,4}\s*Phase\s+(\d+[A-Z]?(?:\.\d+)*)\s*:\s*([^\n]+)/gi;
|
|
@@ -117,10 +118,10 @@ function cmdRoadmapAnalyze(cwd, raw) {
|
|
|
117
118
|
const sectionEnd = nextHeader ? sectionStart + nextHeader.index : content.length;
|
|
118
119
|
const section = content.slice(sectionStart, sectionEnd);
|
|
119
120
|
|
|
120
|
-
const goalMatch = section.match(/\*\*Goal
|
|
121
|
+
const goalMatch = section.match(/\*\*Goal(?::\*\*|\*\*:)\s*([^\n]+)/i);
|
|
121
122
|
const goal = goalMatch ? goalMatch[1].trim() : null;
|
|
122
123
|
|
|
123
|
-
const dependsMatch = section.match(/\*\*Depends on
|
|
124
|
+
const dependsMatch = section.match(/\*\*Depends on(?::\*\*|\*\*:)\s*([^\n]+)/i);
|
|
124
125
|
const depends_on = dependsMatch ? dependsMatch[1].trim() : null;
|
|
125
126
|
|
|
126
127
|
// Check completion on disk
|
|
@@ -150,13 +151,20 @@ function cmdRoadmapAnalyze(cwd, raw) {
|
|
|
150
151
|
else if (hasContext) diskStatus = 'discussed';
|
|
151
152
|
else diskStatus = 'empty';
|
|
152
153
|
}
|
|
153
|
-
} catch {}
|
|
154
|
+
} catch { /* intentionally empty */ }
|
|
154
155
|
|
|
155
156
|
// Check ROADMAP checkbox status
|
|
156
|
-
const checkboxPattern = new RegExp(`-\\s*\\[(x| )\\]\\s*.*Phase\\s+${escapeRegex(phaseNum)}`, 'i');
|
|
157
|
+
const checkboxPattern = new RegExp(`-\\s*\\[(x| )\\]\\s*.*Phase\\s+${escapeRegex(phaseNum)}[:\\s]`, 'i');
|
|
157
158
|
const checkboxMatch = content.match(checkboxPattern);
|
|
158
159
|
const roadmapComplete = checkboxMatch ? checkboxMatch[1] === 'x' : false;
|
|
159
160
|
|
|
161
|
+
// If roadmap marks phase complete, trust that over disk file structure.
|
|
162
|
+
// Phases completed before GSD tracking (or via external tools) may lack
|
|
163
|
+
// the standard PLAN/SUMMARY pairs but are still done.
|
|
164
|
+
if (roadmapComplete && diskStatus !== 'complete') {
|
|
165
|
+
diskStatus = 'complete';
|
|
166
|
+
}
|
|
167
|
+
|
|
160
168
|
phases.push({
|
|
161
169
|
number: phaseNum,
|
|
162
170
|
name: phaseName,
|
|
@@ -173,7 +181,7 @@ function cmdRoadmapAnalyze(cwd, raw) {
|
|
|
173
181
|
|
|
174
182
|
// Extract milestone info
|
|
175
183
|
const milestones = [];
|
|
176
|
-
const milestonePattern = /##\s*(.*v(\d
|
|
184
|
+
const milestonePattern = /##\s*(.*v(\d+(?:\.\d+)+)[^(\n]*)/gi;
|
|
177
185
|
let mMatch;
|
|
178
186
|
while ((mMatch = milestonePattern.exec(content)) !== null) {
|
|
179
187
|
milestones.push({
|
|
@@ -222,7 +230,7 @@ function cmdRoadmapUpdatePlanProgress(cwd, phaseNum, raw) {
|
|
|
222
230
|
error('phase number required for roadmap update-plan-progress');
|
|
223
231
|
}
|
|
224
232
|
|
|
225
|
-
const roadmapPath =
|
|
233
|
+
const roadmapPath = planningPaths(cwd).roadmap;
|
|
226
234
|
|
|
227
235
|
const phaseInfo = findPhaseInternal(cwd, phaseNum);
|
|
228
236
|
if (!phaseInfo) {
|
|
@@ -249,16 +257,27 @@ function cmdRoadmapUpdatePlanProgress(cwd, phaseNum, raw) {
|
|
|
249
257
|
let roadmapContent = fs.readFileSync(roadmapPath, 'utf-8');
|
|
250
258
|
const phaseEscaped = escapeRegex(phaseNum);
|
|
251
259
|
|
|
252
|
-
// Progress table row: update Plans
|
|
253
|
-
const
|
|
254
|
-
|
|
255
|
-
'
|
|
260
|
+
// Progress table row: update Plans/Status/Date columns (handles 4 or 5 column tables)
|
|
261
|
+
const tableRowPattern = new RegExp(
|
|
262
|
+
`^(\\|\\s*${phaseEscaped}\\.?\\s[^|]*(?:\\|[^\\n]*))$`,
|
|
263
|
+
'im'
|
|
256
264
|
);
|
|
257
265
|
const dateField = isComplete ? ` ${today} ` : ' ';
|
|
258
|
-
roadmapContent = roadmapContent.replace(
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
266
|
+
roadmapContent = roadmapContent.replace(tableRowPattern, (fullRow) => {
|
|
267
|
+
const cells = fullRow.split('|').slice(1, -1); // drop leading/trailing empty from split
|
|
268
|
+
if (cells.length === 5) {
|
|
269
|
+
// 5-col: Phase | Milestone | Plans | Status | Completed
|
|
270
|
+
cells[2] = ` ${summaryCount}/${planCount} `;
|
|
271
|
+
cells[3] = ` ${status.padEnd(11)}`;
|
|
272
|
+
cells[4] = dateField;
|
|
273
|
+
} else if (cells.length === 4) {
|
|
274
|
+
// 4-col: Phase | Plans | Status | Completed
|
|
275
|
+
cells[1] = ` ${summaryCount}/${planCount} `;
|
|
276
|
+
cells[2] = ` ${status.padEnd(11)}`;
|
|
277
|
+
cells[3] = dateField;
|
|
278
|
+
}
|
|
279
|
+
return '|' + cells.join('|') + '|';
|
|
280
|
+
});
|
|
262
281
|
|
|
263
282
|
// Update plan count in phase detail section
|
|
264
283
|
const planCountPattern = new RegExp(
|
|
@@ -268,7 +287,7 @@ function cmdRoadmapUpdatePlanProgress(cwd, phaseNum, raw) {
|
|
|
268
287
|
const planCountText = isComplete
|
|
269
288
|
? `${summaryCount}/${planCount} plans complete`
|
|
270
289
|
: `${summaryCount}/${planCount} plans executed`;
|
|
271
|
-
roadmapContent = roadmapContent
|
|
290
|
+
roadmapContent = replaceInCurrentMilestone(roadmapContent, planCountPattern, `$1${planCountText}`);
|
|
272
291
|
|
|
273
292
|
// If complete: check checkbox
|
|
274
293
|
if (isComplete) {
|
|
@@ -276,7 +295,19 @@ function cmdRoadmapUpdatePlanProgress(cwd, phaseNum, raw) {
|
|
|
276
295
|
`(-\\s*\\[)[ ](\\]\\s*.*Phase\\s+${phaseEscaped}[:\\s][^\\n]*)`,
|
|
277
296
|
'i'
|
|
278
297
|
);
|
|
279
|
-
roadmapContent = roadmapContent
|
|
298
|
+
roadmapContent = replaceInCurrentMilestone(roadmapContent, checkboxPattern, `$1x$2 (completed ${today})`);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Mark completed plan checkboxes (e.g. "- [ ] 50-01-PLAN.md" or "- [ ] 50-01:")
|
|
302
|
+
for (const summaryFile of phaseInfo.summaries) {
|
|
303
|
+
const planId = summaryFile.replace('-SUMMARY.md', '').replace('SUMMARY.md', '');
|
|
304
|
+
if (!planId) continue;
|
|
305
|
+
const planEscaped = escapeRegex(planId);
|
|
306
|
+
const planCheckboxPattern = new RegExp(
|
|
307
|
+
`(-\\s*\\[) (\\]\\s*${planEscaped})`,
|
|
308
|
+
'i'
|
|
309
|
+
);
|
|
310
|
+
roadmapContent = roadmapContent.replace(planCheckboxPattern, '$1x$2');
|
|
280
311
|
}
|
|
281
312
|
|
|
282
313
|
fs.writeFileSync(roadmapPath, roadmapContent, 'utf-8');
|
|
@@ -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
|
+
};
|