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): preserve documents with AppleDouble-style names (#2312)

Michael D'Angelo424c564bd1a3
diff --git a/sdk/typescript/src/knowledge-base.ts b/sdk/typescript/src/knowledge-base.ts
index 878d0a066..7015fdbcf 100644
--- a/sdk/typescript/src/knowledge-base.ts
+++ b/sdk/typescript/src/knowledge-base.ts
@@ -2,6 +2,7 @@ import { constants } from "node:fs";
import {
lstat,
mkdtemp,
+ open,
readFile,
readdir,
realpath,
@@ -228,8 +229,7 @@ async function discover(
} else if (entry.isFile()) {
// Office keeps a "~$" owner file next to each open document.
if (entry.name.startsWith("~$")) continue;
- // macOS writes "._" metadata files when copying to other filesystems.
- if (entry.name.startsWith("._")) continue;
+ if (entry.name.startsWith("._") && (await isAppleDouble(path))) continue;
// LibreOffice keeps a ".~lock.<name>#" file next to each open document.
if (/^\.~lock\..+#$/s.test(entry.name)) continue;
if (!DOCUMENT_EXTENSIONS.has(extname(path).toLowerCase())) {
@@ -249,6 +249,21 @@ async function discover(
return documents;
}
+// AppleDouble sidecars and ordinary documents can share the same "._" prefix.
+async function isAppleDouble(path: string): Promise<boolean> {
+ const file = await open(
+ path,
+ constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0),
+ );
+ try {
+ const header = Buffer.alloc(4);
+ const { bytesRead } = await file.read(header, 0, header.length, 0);
+ return bytesRead === header.length && header.readUInt32BE() === 0x00051607;
+ } finally {
+ await file.close();
+ }
+}
+
function decodeText(path: string, bytes: Uint8Array): string {
if (bytes.includes(0)) {
throw new Error(`Knowledge base document contains binary data: ${path}`);
diff --git a/sdk/typescript/tests-ts/knowledge-base.test.ts b/sdk/typescript/tests-ts/knowledge-base.test.ts
index 393fac1b9..e9e5d3008 100644
--- a/sdk/typescript/tests-ts/knowledge-base.test.ts
+++ b/sdk/typescript/tests-ts/knowledge-base.test.ts
@@ -887,6 +887,7 @@ describe("scan knowledge bases", () => {
const root = await temporaryDirectory();
await writeFile(join(root, "guide.md"), "Rotate admin keys monthly.");
await writeFile(join(root, "policy.pdf"), pdf("Access policy."));
+ await writeFile(join(root, "._policy.md"), "Keep audit records.");
const appleDouble = Buffer.alloc(82);
appleDouble.write("\x00\x05\x16\x07\x00\x02\x00\x00Mac OS X", "latin1");
await writeFile(join(root, "._guide.md"), appleDouble);
@@ -897,6 +898,7 @@ describe("scan knowledge bases", () => {
expect((await extractedDocuments(knowledgeBase.path)).sort()).toEqual([
"Access policy.",
+ "Keep audit records.",
"Rotate admin keys monthly.",
]);
});