Skip to content

armgit / ECC

Public Imported from GitHub
0

The agent harness performance optimization system. Skills, instincts, memory, security, and research-first development for Claude Code, Codex, Opencode, Cursor and beyond.

fix(skills): reject Windows-unsafe components in workspace_path example

vamc196dc7d5112
diff --git a/skills/agent-security-hardening/SKILL.md b/skills/agent-security-hardening/SKILL.md
index 54a272080..d392c8823 100644
--- a/skills/agent-security-hardening/SKILL.md
+++ b/skills/agent-security-hardening/SKILL.md
@@ -85,15 +85,24 @@ This step is complete when tests reject empty, oversized, malformed, traversal-s
### 3. Contain Filesystem Access
-Reject absolute user-controlled paths and traversal components before joining. Resolve both the workspace and candidate path, then prove containment:
+Reject absolute user-controlled paths and traversal components before joining. Reject Windows-unsafe components on every platform so behavior stays portable: reserved device names (`CON`, `NUL`, `COM1`, `LPT1`, including with an extension such as `nul.txt`), names ending in a dot or space (Win32 strips them), and `:` (drive-relative or NTFS alternate data stream syntax such as `report.txt:ads`). Resolve both the workspace and candidate path, then prove containment:
```python
+import re
from pathlib import Path
+WINDOWS_RESERVED_NAME = re.compile(
+ r"(CON|PRN|AUX|NUL|COM[1-9¹²³]|LPT[1-9¹²³])(\..*)?",
+ re.IGNORECASE,
+)
+
def workspace_path(workspace: Path, requested: str) -> Path:
relative = Path(requested)
if relative.is_absolute() or ".." in relative.parts:
raise ValueError("path must be workspace-relative")
+ for part in relative.parts:
+ if ":" in part or part.endswith((".", " ")) or WINDOWS_RESERVED_NAME.fullmatch(part):
+ raise ValueError("path component is not portable")
root = workspace.resolve(strict=True)
candidate = (root / relative).resolve(strict=False)
@@ -104,7 +113,7 @@ def workspace_path(workspace: Path, requested: str) -> Path:
Containment checks do not eliminate symlink races. For sensitive writes, open relative to a trusted directory handle where the platform supports it, reject symlink targets, create files exclusively, and verify ownership and permissions after opening.
-This step is complete when tests cover sibling-prefix paths, nested `..`, absolute paths, symlink escapes, and a valid nested workspace path.
+This step is complete when tests cover sibling-prefix paths, nested `..`, absolute paths, symlink escapes, reserved device names, trailing dots or spaces, alternate data stream syntax, and a valid nested workspace path.
### 4. Protect Temporary Data and Credentials
diff --git a/tests/ci/agent-security-hardening-skill.test.js b/tests/ci/agent-security-hardening-skill.test.js
new file mode 100644
index 000000000..5cc662046
--- /dev/null
+++ b/tests/ci/agent-security-hardening-skill.test.js
@@ -0,0 +1,136 @@
+/**
+ * Executes the workspace_path() example from the agent-security-hardening
+ * skill so the documented guard is checked as code, not only as prose.
+ */
+
+"use strict";
+
+const assert = require("assert");
+const fs = require("fs");
+const os = require("os");
+const path = require("path");
+const { spawnSync } = require("child_process");
+
+const REPO_ROOT = path.join(__dirname, "..", "..");
+const SKILL_PATH = path.join(REPO_ROOT, "skills", "agent-security-hardening", "SKILL.md");
+
+function findPython() {
+ for (const candidate of ["python3", "python"]) {
+ const probe = spawnSync(candidate, ["-c", "import sys; print(sys.version_info >= (3, 9))"], {
+ encoding: "utf8",
+ });
+ if (probe.status === 0 && probe.stdout.trim() === "True") return candidate;
+ }
+ return null;
+}
+
+function workspacePathSnippet() {
+ const skill = fs.readFileSync(SKILL_PATH, "utf8");
+ const blocks = [...skill.matchAll(/```python\n([\s\S]*?)```/g)].map(match => match[1]);
+ const snippet = blocks.find(block => block.includes("def workspace_path("));
+ assert.ok(snippet, "workspace_path() python example missing from SKILL.md");
+ return snippet;
+}
+
+const HARNESS = `
+import json, sys
+from pathlib import Path
+namespace = {}
+exec(sys.stdin.read(), namespace)
+workspace_path = namespace["workspace_path"]
+workspace = Path(sys.argv[1])
+results = {}
+for requested in json.loads(sys.argv[2]):
+ try:
+ workspace_path(workspace, requested)
+ results[requested] = "accepted"
+ except ValueError:
+ results[requested] = "rejected"
+print(json.dumps(results))
+`;
+
+function classify(python, requests) {
+ const workspace = fs.mkdtempSync(path.join(os.tmpdir(), "ecc-workspace-path-"));
+ try {
+ const result = spawnSync(python, ["-c", HARNESS, workspace, JSON.stringify(requests)], {
+ input: workspacePathSnippet(),
+ encoding: "utf8",
+ });
+ assert.strictEqual(result.status, 0, `python harness failed:\n${result.stderr}`);
+ return JSON.parse(result.stdout);
+ } finally {
+ fs.rmSync(workspace, { recursive: true, force: true });
+ }
+}
+
+const tests = [];
+function test(name, fn) { tests.push([name, fn]); }
+
+const python = findPython();
+
+test("accepts ordinary nested and look-alike workspace paths", () => {
+ if (!python) return { skipped: "python >= 3.9 not available" };
+ const requests = ["reports/q1.txt", "console.txt", "CONFIG", "com10.txt", "nullable.md", ".env"];
+ const results = classify(python, requests);
+ for (const requested of requests) {
+ assert.strictEqual(results[requested], "accepted", `${requested} should be accepted`);
+ }
+});
+
+test("rejects traversal and absolute paths", () => {
+ if (!python) return { skipped: "python >= 3.9 not available" };
+ const requests = ["../outside.txt", "nested/../../outside.txt", path.resolve(os.tmpdir(), "abs.txt")];
+ const results = classify(python, requests);
+ for (const requested of requests) {
+ assert.strictEqual(results[requested], "rejected", `${requested} should be rejected`);
+ }
+});
+
+test("rejects Windows reserved device names, trailing dots or spaces, and stream syntax", () => {
+ if (!python) return { skipped: "python >= 3.9 not available" };
+ const requests = [
+ "CON",
+ "nul",
+ "nul.txt",
+ "NUL.tar.gz",
+ "aux",
+ "prn.log",
+ "com1",
+ "COM9.txt",
+ "lpt1",
+ "LPT9.log",
+ "COM¹",
+ "lpt³.txt",
+ "sub/CON/file.txt",
+ "notes.",
+ "notes ",
+ "dir./file.txt",
+ "report.txt:ads",
+ "report.txt::$DATA",
+ ];
+ const results = classify(python, requests);
+ for (const requested of requests) {
+ assert.strictEqual(results[requested], "rejected", `${JSON.stringify(requested)} should be rejected`);
+ }
+});
+
+let failed = 0;
+let skipped = 0;
+console.log("\n=== Testing agent-security-hardening skill ===\n");
+for (const [name, fn] of tests) {
+ try {
+ const result = fn();
+ if (result?.skipped) {
+ skipped += 1;
+ console.log(` - SKIP ${name}: ${result.skipped}`);
+ continue;
+ }
+ console.log(` ✓ ${name}`);
+ } catch (error) {
+ failed += 1;
+ console.log(` ✗ ${name}`);
+ console.error(` ${error.message}`);
+ }
+}
+if (failed) process.exit(1);
+console.log(`\n${tests.length - failed - skipped}/${tests.length} passed, ${skipped} skipped`);