Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

fix(knowledge-base): support long filenames when staging documents (#1016)

wj270ab44c276f
diff --git a/sdk/typescript/src/knowledge-base.ts b/sdk/typescript/src/knowledge-base.ts
index 425fe64cf..79b0c8ca9 100644
--- a/sdk/typescript/src/knowledge-base.ts
+++ b/sdk/typescript/src/knowledge-base.ts
@@ -92,15 +92,15 @@ export async function prepareKnowledgeBase(
`Knowledge base document contains no extractable text: ${document}`,
);
}
- await writeFile(
- join(path, `${index++}-${basename(document)}.txt`),
- text,
- {
- encoding: "utf8",
- mode: 0o600,
- signal,
- },
- );
+ const name = `${index}-${basename(document)}.txt`;
+ // The prefix and suffix can exceed the filesystem's 255-byte name limit.
+ const filename = Buffer.byteLength(name) > 255 ? `${index}.txt` : name;
+ await writeFile(join(path, filename), text, {
+ encoding: "utf8",
+ mode: 0o600,
+ signal,
+ });
+ index++;
}
} catch (error) {
await rm(path, { recursive: true, force: true });
diff --git a/sdk/typescript/tests-ts/knowledge-base.test.ts b/sdk/typescript/tests-ts/knowledge-base.test.ts
index 4fb21cac0..f850b4ddc 100644
--- a/sdk/typescript/tests-ts/knowledge-base.test.ts
+++ b/sdk/typescript/tests-ts/knowledge-base.test.ts
@@ -130,6 +130,48 @@ describe("scan knowledge bases", () => {
}
});
+ test.each([
+ ["ASCII at the staging boundary", `${"a".repeat(246)}.md`],
+ ["ASCII beyond the staging boundary", `${"a".repeat(247)}.md`],
+ ["ASCII at the source boundary", `${"a".repeat(252)}.md`],
+ ["multibyte UTF-8", `${"文".repeat(83)}.md`],
+ ])("stages long filenames: %s", async (_description, name) => {
+ const root = await temporaryDirectory();
+ const paths: string[] = [];
+ const contents: string[] = [];
+ for (let index = 0; index < 11; index++) {
+ const directory = join(root, String(index));
+ await mkdir(directory);
+ const source = join(directory, name);
+ const text = `Document ${index}.`;
+ await writeFile(source, text);
+ paths.push(source);
+ contents.push(text);
+ }
+ const scope = join(root, "scope.md");
+ await writeFile(scope, "Review application boundaries.");
+ paths.push(scope);
+ contents.push("Review application boundaries.");
+
+ const knowledgeBase = await prepareKnowledgeBase(paths);
+ temporaryDirectories.push(knowledgeBase.path);
+
+ expect(knowledgeBase.sources).toEqual(paths);
+ expect((await extractedDocuments(knowledgeBase.path)).sort()).toEqual(
+ [...contents].sort(),
+ );
+ expect(
+ await readFile(join(knowledgeBase.path, "11-scope.md.txt"), "utf8"),
+ ).toBe("Review application boundaries.");
+ await knowledgeBase.cleanup();
+ await expect(stat(knowledgeBase.path)).rejects.toMatchObject({
+ code: "ENOENT",
+ });
+ expect(
+ await Promise.all(paths.map((path) => readFile(path, "utf8"))),
+ ).toEqual(contents);
+ });
+
test("cancels recursive discovery before staging knowledge-base documents", async () => {
const root = await temporaryDirectory();
const nested = join(root, "nested", "deeper");