Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(skills): check skill bundle paths per segment so nested files publish on Windows (#5776)

garrytan8c83350a2d7d
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index d14913a558..6f1359f186 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -138,7 +138,7 @@ jobs:
bun-version: ${{ matrix.bun }}
- run: bun install --frozen-lockfile --ignore-scripts
- name: Boundary and transport regressions
- run: bun test --timeout 60000 test/frontmatter-security.test.ts test/data-frontmatter.test.ts test/authorization-boundaries.test.ts test/url-safety.test.ts test/guarded-http.test.ts
+ run: bun test --timeout 60000 test/frontmatter-security.test.ts test/data-frontmatter.test.ts test/authorization-boundaries.test.ts test/url-safety.test.ts test/guarded-http.test.ts test/bundle-files-segments.test.ts
- name: Isolated TLS and proxy regressions
run: bun test --timeout 60000 test/guarded-http-tls.serial.test.ts
diff --git a/src/core/persistence/bundle-files.ts b/src/core/persistence/bundle-files.ts
index 8728d5b571..d190139926 100644
--- a/src/core/persistence/bundle-files.ts
+++ b/src/core/persistence/bundle-files.ts
@@ -18,12 +18,22 @@ function unsafe(): OperationError {
return new OperationError('storage_error', 'Skill publication requires bounded regular files without aliases, links, or special files.');
}
+/**
+ * #5776: check each path segment, not the joined relative path. On Windows the
+ * separator is a backslash, so testing the whole relative path for one
+ * rejected every nested bundle file there.
+ */
+export function bundleRelativePathIsSafe(rel: string, pathSep: string = sep): boolean {
+ const parts = rel.split(pathSep);
+ return parts.length <= BUNDLE_FILE_LIMITS.depth && parts.every(part => part !== '' && !/[\\/\x00-\x1f:]/.test(part));
+}
+
export function readBundleFile(path: string, root: string): { bytes: Buffer; mode: number } | null {
if (!path.isWellFormed() || path !== path.normalize('NFC')) throw unsafe();
const rel = relative(root, path);
if (!isAbsolute(path) || path !== resolve(path) || !rel || isAbsolute(rel) || rel.startsWith(`..${sep}`) || rel === '..'
|| !rel.isWellFormed() || rel !== rel.normalize('NFC')
- || rel.split(sep).length > BUNDLE_FILE_LIMITS.depth || /[\\\x00-\x1f:]/.test(rel)) throw unsafe();
+ || !bundleRelativePathIsSafe(rel)) throw unsafe();
if (realpathSync(root) !== root || !lstatSync(root).isDirectory()) throw unsafe();
const parts = rel.split(sep);
let current = root;
diff --git a/test/bundle-files-segments.test.ts b/test/bundle-files-segments.test.ts
new file mode 100644
index 0000000000..470d1a024c
--- /dev/null
+++ b/test/bundle-files-segments.test.ts
@@ -0,0 +1,50 @@
+/**
+ * #5776: skill publication's bundle reader checked the whole relative path
+ * for a backslash, which is the separator on Windows, so every nested bundle
+ * file was rejected there and the shared-skills projection never completed.
+ * The check is per path segment now. The win32 cases run on every OS through
+ * path.win32; the readBundleFile case runs natively in the Windows
+ * security-regressions job.
+ */
+
+import { describe, test, expect } from 'bun:test';
+import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'fs';
+import { join, posix, win32 } from 'path';
+import { tmpdir } from 'os';
+
+import { bundleRelativePathIsSafe, readBundleFile } from '../src/core/persistence/bundle-files.ts';
+
+describe('#5776 bundle paths are checked per segment', () => {
+ test('a nested Windows path is safe', () => {
+ const rel = win32.relative('C:\\brain\\skills', 'C:\\brain\\skills\\sub\\nested.md');
+ expect(rel).toBe('sub\\nested.md');
+ expect(bundleRelativePathIsSafe(rel, win32.sep)).toBe(true);
+ });
+
+ test('unsafe segments are still refused on Windows', () => {
+ expect(bundleRelativePathIsSafe('sub\\a:b.md', win32.sep)).toBe(false);
+ expect(bundleRelativePathIsSafe('sub\\a\u0001b.md', win32.sep)).toBe(false);
+ expect(bundleRelativePathIsSafe('sub\\a/b.md', win32.sep)).toBe(false);
+ expect(bundleRelativePathIsSafe('sub\\\\b.md', win32.sep)).toBe(false);
+ });
+
+ test('POSIX keeps refusing a backslash inside a file name, and the depth bound holds', () => {
+ expect(bundleRelativePathIsSafe('sub/nested.md', posix.sep)).toBe(true);
+ expect(bundleRelativePathIsSafe('sub/a\\b.md', posix.sep)).toBe(false);
+ expect(bundleRelativePathIsSafe('sub/a:b.md', posix.sep)).toBe(false);
+ expect(bundleRelativePathIsSafe(Array.from({ length: 16 }, () => 'd').join('/'), posix.sep)).toBe(true);
+ expect(bundleRelativePathIsSafe(Array.from({ length: 17 }, () => 'd').join('/'), posix.sep)).toBe(false);
+ });
+
+ test('readBundleFile reads a nested file on this platform', () => {
+ const root = realpathSync(mkdtempSync(join(tmpdir(), 'gbrain-5776-')));
+ try {
+ mkdirSync(join(root, 'sub'), { recursive: true });
+ writeFileSync(join(root, 'sub', 'nested.md'), 'nested body');
+ const read = readBundleFile(join(root, 'sub', 'nested.md'), root);
+ expect(read?.bytes.toString('utf-8')).toBe('nested body');
+ } finally {
+ rmSync(root, { recursive: true, force: true });
+ }
+ });
+});