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): ignore DOCX tab stop definitions when extracting text (#2284)

Lanre Shittu522d19eefb28
diff --git a/sdk/typescript/src/knowledge-base.ts b/sdk/typescript/src/knowledge-base.ts
index b2a27df89..756dccc69 100644
--- a/sdk/typescript/src/knowledge-base.ts
+++ b/sdk/typescript/src/knowledge-base.ts
@@ -335,6 +335,8 @@ function extractDocx(path: string, bytes: Uint8Array): string {
let documentElement = false;
let deletedDepth = 0;
let instructionText = false;
+ // Paragraph properties list tab stops as w:tab elements inside w:tabs.
+ let tabStops = false;
const append = (value: string) => {
if (deletedDepth > 0) return;
const field = fields.at(-1);
@@ -379,7 +381,8 @@ function extractDocx(path: string, bytes: Uint8Array): string {
fields.at(-1)!.instructions = false;
else if (type === "end") finishField(true);
} else if (localName === "br" || localName === "cr") append("\n");
- else if (localName === "tab") append("\t");
+ else if (localName === "tabs") tabStops = true;
+ else if (localName === "tab" && !tabStops) append("\t");
else if (localName === "noBreakHyphen") append("-");
});
parser.on("closetag", ({ name, isSelfClosing }) => {
@@ -391,6 +394,7 @@ function extractDocx(path: string, bytes: Uint8Array): string {
append(content);
}
if (localName === "instrText") instructionText = false;
+ if (localName === "tabs") tabStops = false;
if (!isSelfClosing && localName === "p") append("\n");
if (
localName === "del" ||
diff --git a/sdk/typescript/tests-ts/knowledge-base.test.ts b/sdk/typescript/tests-ts/knowledge-base.test.ts
index 9dd571abf..50325d605 100644
--- a/sdk/typescript/tests-ts/knowledge-base.test.ts
+++ b/sdk/typescript/tests-ts/knowledge-base.test.ts
@@ -507,7 +507,10 @@ describe("scan knowledge bases", () => {
);
await writeFile(
join(root, "self-closing-tab.docx"),
- docx("User", "Permission", "<w:tab/>"),
+ docxBody(
+ '<w:p><w:pPr><w:tabs><w:tab w:val="left" w:pos="1701"/></w:tabs></w:pPr>' +
+ "<w:r><w:t>User</w:t><w:tab/><w:t>Permission</w:t></w:r></w:p>",
+ ),
);
await writeFile(
join(root, "non-breaking-hyphen.docx"),