@principles/pd-cli 1.83.0 → 1.84.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/dist/commands/runtime-uat.d.ts +1 -0
- package/dist/commands/runtime-uat.d.ts.map +1 -1
- package/dist/commands/runtime-uat.guard.test.d.ts +2 -0
- package/dist/commands/runtime-uat.guard.test.d.ts.map +1 -0
- package/dist/commands/runtime-uat.guard.test.js +155 -0
- package/dist/commands/runtime-uat.guard.test.js.map +1 -0
- package/dist/commands/runtime-uat.js +29 -3
- package/dist/commands/runtime-uat.js.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/utils/production-workspace-guard.d.ts +77 -0
- package/dist/utils/production-workspace-guard.d.ts.map +1 -0
- package/dist/utils/production-workspace-guard.js +170 -0
- package/dist/utils/production-workspace-guard.js.map +1 -0
- package/dist/utils/production-workspace-guard.test.d.ts +2 -0
- package/dist/utils/production-workspace-guard.test.d.ts.map +1 -0
- package/dist/utils/production-workspace-guard.test.js +95 -0
- package/dist/utils/production-workspace-guard.test.js.map +1 -0
- package/package.json +1 -1
- package/src/commands/runtime-uat.guard.test.ts +177 -0
- package/src/commands/runtime-uat.ts +42 -4
- package/src/index.ts +2 -0
- package/src/utils/production-workspace-guard.test.ts +108 -0
- package/src/utils/production-workspace-guard.ts +219 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
import * as os from 'os';
|
|
4
|
+
import {
|
|
5
|
+
isProductionWorkspace,
|
|
6
|
+
guardUatWorkspace,
|
|
7
|
+
getSafeUatWorkspacePath,
|
|
8
|
+
formatGuardRefusal,
|
|
9
|
+
type GuardRefusal,
|
|
10
|
+
} from './production-workspace-guard.js';
|
|
11
|
+
|
|
12
|
+
const PRODUCTION_PATHS = [
|
|
13
|
+
'D:\\.openclaw\\workspace',
|
|
14
|
+
'C:\\.openclaw\\workspace',
|
|
15
|
+
'C:\\Users\\Administrator\\.openclaw\\workspace',
|
|
16
|
+
path.join(os.homedir(), '.openclaw', 'workspace'),
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
const SAFE_PATHS = [
|
|
20
|
+
'D:\\.openclaw\\workspace-test',
|
|
21
|
+
'D:\\.openclaw\\workspace-backup',
|
|
22
|
+
path.join(os.tmpdir(), 'pd-uat-workspace'),
|
|
23
|
+
path.join(os.tmpdir(), 'pd-test-any'),
|
|
24
|
+
'C:\\completely-unrelated\\work',
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
describe('isProductionWorkspace', () => {
|
|
28
|
+
it.each(PRODUCTION_PATHS)('detects production: %s', (prodPath) => {
|
|
29
|
+
expect(isProductionWorkspace(path.resolve(prodPath))).toBe(true);
|
|
30
|
+
});
|
|
31
|
+
it.each(SAFE_PATHS)('allows safe: %s', (safePath) => {
|
|
32
|
+
expect(isProductionWorkspace(path.resolve(safePath))).toBe(false);
|
|
33
|
+
});
|
|
34
|
+
it('detects descendant', () => {
|
|
35
|
+
expect(isProductionWorkspace(path.resolve('D:\\.openclaw\\workspace\\sub\\child'))).toBe(true);
|
|
36
|
+
});
|
|
37
|
+
it('rejects sibling workspace-test (ERR-030)', () => {
|
|
38
|
+
expect(isProductionWorkspace(path.resolve('D:\\.openclaw\\workspace-test'))).toBe(false);
|
|
39
|
+
});
|
|
40
|
+
it('rejects sibling workspace-backup (ERR-030)', () => {
|
|
41
|
+
expect(isProductionWorkspace(path.resolve('D:\\.openclaw\\workspace-backup'))).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe('guardUatWorkspace', () => {
|
|
46
|
+
describe('refused', () => {
|
|
47
|
+
it.each(PRODUCTION_PATHS)('refuses production: %s', (prodPath) => {
|
|
48
|
+
const r = guardUatWorkspace(prodPath, 'test');
|
|
49
|
+
expect(r.refused).toBe(true);
|
|
50
|
+
if (r.refused) {
|
|
51
|
+
expect(r.reason).toContain('UAT/runtime test commands are not allowed');
|
|
52
|
+
expect(r.nextAction).toContain('temporary workspace');
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
it('refuses descendant', () => {
|
|
56
|
+
expect(guardUatWorkspace('D:\\.openclaw\\workspace\\subdir', 'test').refused).toBe(true);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
describe('allowed', () => {
|
|
60
|
+
it.each(SAFE_PATHS)('allows safe: %s', (safePath) => {
|
|
61
|
+
const r = guardUatWorkspace(safePath, 'test');
|
|
62
|
+
expect(r.refused).toBe(false);
|
|
63
|
+
});
|
|
64
|
+
it.each(['D:\\.openclaw\\workspace-test', 'D:\\.openclaw\\workspace-backup'])(
|
|
65
|
+
'allows sibling: %s (ERR-030)', (p) => {
|
|
66
|
+
expect(guardUatWorkspace(p, 'test').refused).toBe(false);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe('JSON output (EP-04)', () => {
|
|
72
|
+
it('outputs single object with reason and nextAction', () => {
|
|
73
|
+
const r = guardUatWorkspace('D:\\.openclaw\\workspace', 'test');
|
|
74
|
+
const json = formatGuardRefusal(r as GuardRefusal, 'test', true);
|
|
75
|
+
const parsed = JSON.parse(json);
|
|
76
|
+
expect(parsed).toMatchObject({
|
|
77
|
+
status: 'refused', reason: expect.any(String), nextAction: expect.any(String),
|
|
78
|
+
workspace: expect.any(String), isProduction: true,
|
|
79
|
+
});
|
|
80
|
+
expect(Array.isArray(parsed)).toBe(false);
|
|
81
|
+
});
|
|
82
|
+
it('no console prefixes in JSON', () => {
|
|
83
|
+
const r = guardUatWorkspace('D:\\.openclaw\\workspace', 'test');
|
|
84
|
+
const json = formatGuardRefusal(r as GuardRefusal, 'test', true);
|
|
85
|
+
expect(json).not.toContain('[pd-cli]');
|
|
86
|
+
expect(json).not.toContain('ERROR:');
|
|
87
|
+
expect(json.trim().startsWith('{')).toBe(true);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe('text output (EP-03)', () => {
|
|
92
|
+
it('includes reason and nextAction', () => {
|
|
93
|
+
const r = guardUatWorkspace('D:\\.openclaw\\workspace', 'test');
|
|
94
|
+
const text = formatGuardRefusal(r as GuardRefusal, 'test', false);
|
|
95
|
+
expect(text).toContain('Reason:');
|
|
96
|
+
expect(text).toContain('Next Action:');
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe('getSafeUatWorkspacePath', () => {
|
|
101
|
+
it('returns deterministic temp path', () => {
|
|
102
|
+
const p1 = getSafeUatWorkspacePath();
|
|
103
|
+
const p2 = getSafeUatWorkspacePath();
|
|
104
|
+
expect(p1).toBe(p2);
|
|
105
|
+
expect(p1).toContain(os.tmpdir());
|
|
106
|
+
expect(p1).toContain('pd-uat-workspace');
|
|
107
|
+
});
|
|
108
|
+
});
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Production Workspace Guard
|
|
3
|
+
*
|
|
4
|
+
* Prevents UAT/runtime test commands from writing to production workspaces.
|
|
5
|
+
*
|
|
6
|
+
* Production workspaces are:
|
|
7
|
+
* - D:\.openclaw\workspace
|
|
8
|
+
* - C:\Users\Administrator\.openclaw\workspace
|
|
9
|
+
* - And the default workspace resolved by OpenClaw configuration
|
|
10
|
+
*
|
|
11
|
+
* This module follows ERR-030 (path prefix matching must use segment boundaries)
|
|
12
|
+
* and EP-03/EP-04 (fail loud with structured reason and nextAction).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import * as path from 'path';
|
|
16
|
+
import * as os from 'os';
|
|
17
|
+
import { existsSync } from 'fs';
|
|
18
|
+
|
|
19
|
+
// ── Production workspace paths ─────────────────────────────────────────────────
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* List of production workspace paths that should be protected from UAT/test writes.
|
|
23
|
+
* These are the default paths where PD is typically installed and used for real work.
|
|
24
|
+
*/
|
|
25
|
+
const PRODUCTION_WORKSPACE_PATHS = [
|
|
26
|
+
// Windows default
|
|
27
|
+
path.resolve('D:\\.openclaw\\workspace'),
|
|
28
|
+
path.resolve('C:\\.openclaw\\workspace'),
|
|
29
|
+
path.resolve('C:\\Users\\Administrator\\.openclaw\\workspace'),
|
|
30
|
+
path.resolve('C:\\Users\\Admin\\.openclaw\\workspace'),
|
|
31
|
+
// Unix-like defaults
|
|
32
|
+
path.resolve(path.join(os.homedir(), '.openclaw', 'workspace')),
|
|
33
|
+
// macOS-specific
|
|
34
|
+
path.resolve(path.join(os.homedir(), '.openclaw', 'workspace')),
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
// ── Resolution helpers ───────────────────────────────────────────────────────
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Resolve the workspace path from environment variable or current directory.
|
|
41
|
+
*/
|
|
42
|
+
export function resolveWorkspacePath(inputPath?: string): string {
|
|
43
|
+
if (!inputPath) {
|
|
44
|
+
// Default to current directory
|
|
45
|
+
return path.resolve(process.cwd());
|
|
46
|
+
}
|
|
47
|
+
return path.resolve(inputPath);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Check if a path is a production workspace.
|
|
52
|
+
*
|
|
53
|
+
* This follows ERR-030: path matching must use segment boundaries (path.sep)
|
|
54
|
+
* to avoid false positives on sibling directories like "workspace-backup".
|
|
55
|
+
*
|
|
56
|
+
* @param resolvedPath - The absolute, normalized workspace path to check
|
|
57
|
+
* @returns true if the path is a production workspace
|
|
58
|
+
*/
|
|
59
|
+
export function isProductionWorkspace(resolvedPath: string): boolean {
|
|
60
|
+
const normalized = resolvedPath.toLowerCase();
|
|
61
|
+
|
|
62
|
+
for (const prodPath of PRODUCTION_WORKSPACE_PATHS) {
|
|
63
|
+
const normalizedProd = prodPath.toLowerCase();
|
|
64
|
+
|
|
65
|
+
// Exact match
|
|
66
|
+
if (normalized === normalizedProd) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Descendant match: must have path separator after prefix
|
|
71
|
+
// ERR-030: "startsWith" without separator matches sibling directories
|
|
72
|
+
if (normalized.startsWith(normalizedProd + path.sep)) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Handle Windows path variations
|
|
77
|
+
// D:\.openclaw\workspace should not match D:\.openclaw\workspace-backup
|
|
78
|
+
// Use case-insensitive comparison (already normalized)
|
|
79
|
+
if (path.sep === '\\') {
|
|
80
|
+
// Windows: check both forward and backslash
|
|
81
|
+
if (normalized.startsWith(normalizedProd + '/')) {
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
if (normalized.startsWith(normalizedProd.replace(/\\/g, '/') + '/')) {
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
if (normalized.startsWith(normalizedProd.replace(/\\/g, '/') + '\\')) {
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ── Guard logic ──────────────────────────────────────────────────────────────
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Guard result for UAT/test commands attempting to write to production workspace.
|
|
100
|
+
*/
|
|
101
|
+
export interface GuardRefusal {
|
|
102
|
+
refused: true;
|
|
103
|
+
reason: string;
|
|
104
|
+
nextAction: string;
|
|
105
|
+
workspace: string;
|
|
106
|
+
isProduction: true;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Guard result allowing the operation.
|
|
111
|
+
*/
|
|
112
|
+
export interface GuardAllowed {
|
|
113
|
+
refused: false;
|
|
114
|
+
workspace: string;
|
|
115
|
+
isProduction: false;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export type GuardResult = GuardRefusal | GuardAllowed;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Check if a workspace is protected from UAT/test writes.
|
|
122
|
+
*
|
|
123
|
+
* This is the main guard function for UAT/runtime test commands.
|
|
124
|
+
* It returns a structured result following EP-03/EP-04 requirements.
|
|
125
|
+
*
|
|
126
|
+
* @param inputPath - The workspace path to check (optional, resolves to cwd if not provided)
|
|
127
|
+
* @param commandContext - Context string for the error message (e.g., "pd runtime uat")
|
|
128
|
+
* @returns GuardResult indicating if the operation is allowed or refused
|
|
129
|
+
*/
|
|
130
|
+
export function guardUatWorkspace(
|
|
131
|
+
inputPath: string | undefined,
|
|
132
|
+
_commandContext: string
|
|
133
|
+
): GuardResult {
|
|
134
|
+
const resolved = resolveWorkspacePath(inputPath);
|
|
135
|
+
|
|
136
|
+
// PRI-334: Guard check should happen BEFORE file existence check
|
|
137
|
+
// This prevents production workspace writes even if directory doesn't exist yet
|
|
138
|
+
if (isProductionWorkspace(resolved)) {
|
|
139
|
+
return {
|
|
140
|
+
refused: true,
|
|
141
|
+
workspace: resolved,
|
|
142
|
+
isProduction: true,
|
|
143
|
+
reason: `UAT/runtime test commands are not allowed to write to the production workspace (${resolved}). This prevents test/synthetic data from polluting your real PD state.`,
|
|
144
|
+
nextAction: `Use a temporary workspace for testing (recommended: ${os.tmpdir()}/pd-uat-workspace) or explicitly confirm you understand the risk by using --allow-production-workspace-for-uat (not recommended).`,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (!existsSync(resolved)) {
|
|
149
|
+
// Non-existent workspace is safe to use (will be created)
|
|
150
|
+
return {
|
|
151
|
+
refused: false,
|
|
152
|
+
workspace: resolved,
|
|
153
|
+
isProduction: false,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return {
|
|
158
|
+
refused: false,
|
|
159
|
+
workspace: resolved,
|
|
160
|
+
isProduction: false,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Get a safe UAT workspace path in the system temp directory.
|
|
166
|
+
*
|
|
167
|
+
* This follows the safe execution path requirement from PRI-334.
|
|
168
|
+
*/
|
|
169
|
+
export function getSafeUatWorkspacePath(): string {
|
|
170
|
+
const tempDir = os.tmpdir();
|
|
171
|
+
// Create a unique but deterministic path for UAT workspaces
|
|
172
|
+
const uatWorkspace = path.join(tempDir, 'pd-uat-workspace');
|
|
173
|
+
return uatWorkspace;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Check if --allow-production-workspace-for-uat flag is set.
|
|
178
|
+
*
|
|
179
|
+
* This is the escape hatch for cases where the operator explicitly wants to run UAT on production.
|
|
180
|
+
* The flag must be very explicit in both name and output (as required by PRI-334).
|
|
181
|
+
*/
|
|
182
|
+
export function isProductionWorkspaceAllowed(): boolean {
|
|
183
|
+
// Check if the flag was parsed and passed through opts
|
|
184
|
+
// This will be called from command handlers after Commander parses flags
|
|
185
|
+
return false; // Placeholder; actual check depends on Commander opts
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Format guard refusal for console output.
|
|
190
|
+
*
|
|
191
|
+
* Follows EP-03/EP-04: structured reason + nextAction.
|
|
192
|
+
*/
|
|
193
|
+
export function formatGuardRefusal(refusal: GuardRefusal, commandContext: string, jsonMode = false): string {
|
|
194
|
+
if (jsonMode) {
|
|
195
|
+
// JSON mode: output exactly one JSON object with reason and nextAction
|
|
196
|
+
return JSON.stringify(
|
|
197
|
+
{
|
|
198
|
+
status: 'refused',
|
|
199
|
+
reason: refusal.reason,
|
|
200
|
+
nextAction: refusal.nextAction,
|
|
201
|
+
workspace: refusal.workspace,
|
|
202
|
+
isProduction: refusal.isProduction,
|
|
203
|
+
},
|
|
204
|
+
null,
|
|
205
|
+
2
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Text mode: human-readable structured output
|
|
210
|
+
return [
|
|
211
|
+
`[pd-cli] ERROR: ${commandContext} - workspace guard triggered`,
|
|
212
|
+
'',
|
|
213
|
+
`Reason: ${refusal.reason}`,
|
|
214
|
+
`Next Action: ${refusal.nextAction}`,
|
|
215
|
+
`Workspace: ${refusal.workspace}`,
|
|
216
|
+
'',
|
|
217
|
+
'This guard prevents UAT/runtime test data from polluting your production workspace.',
|
|
218
|
+
].join('\n');
|
|
219
|
+
}
|