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): skip DOCX content control placeholder text (#2490)

Lanre Shittue3280a63a518
diff --git a/sdk/typescript/src/knowledge-base.ts b/sdk/typescript/src/knowledge-base.ts
index 7015fdbcf..2afa5e2ed 100644
--- a/sdk/typescript/src/knowledge-base.ts
+++ b/sdk/typescript/src/knowledge-base.ts
@@ -362,12 +362,23 @@ function extractDocx(path: string, bytes: Uint8Array): string {
const declarations = new Map<string, (Set<string> | undefined)[]>();
const layout: boolean[] = [];
let documentElement = false;
- let deletedDepth = 0;
+ let ignoredDepth = 0;
let instructionText = false;
// Paragraph properties list tab stops as w:tab elements inside w:tabs.
let tabStops = false;
+ // Content controls mark unfilled prompt text with w:showingPlcHdr.
+ const placeholders: boolean[] = [];
+ const wordAttribute = (
+ attributes: Record<string, string>,
+ localName: string,
+ ) =>
+ decodeXml(
+ Object.entries(attributes).find(
+ ([key]) => key.split(":").at(-1) === localName,
+ )?.[1] ?? "",
+ );
const append = (value: string) => {
- if (deletedDepth > 0) return;
+ if (ignoredDepth > 0) return;
const field = fields.at(-1);
if (field === undefined) text.push(value);
else if (!field.instructions) field.result.push(value);
@@ -394,14 +405,21 @@ function extractDocx(path: string, bytes: Uint8Array): string {
declarations.get(declaration)?.at(-1)?.has(localName) ?? false,
);
if (
+ ignoredDepth > 0 ||
localName === "del" ||
localName === "delText" ||
localName === "delInstrText" ||
- localName === "moveFrom"
+ localName === "moveFrom" ||
+ (localName === "sdtContent" && placeholders.at(-1))
) {
- deletedDepth++;
+ ignoredDepth++;
+ return;
}
- if (deletedDepth > 0) return;
+ if (localName === "sdt") placeholders.push(false);
+ else if (localName === "showingPlcHdr" && placeholders.length)
+ placeholders[placeholders.length - 1] = !["0", "false", "off"].includes(
+ wordAttribute(attributes, "val"),
+ );
if (localName === "txbxContent") {
// Text boxes have their own field boundaries.
stories.push({ text, fields });
@@ -410,11 +428,7 @@ function extractDocx(path: string, bytes: Uint8Array): string {
} else if (localName === "document") documentElement = true;
else if (localName === "instrText") instructionText = true;
else if (localName === "fldChar") {
- const type = decodeXml(
- Object.entries(attributes).find(
- ([name]) => name.split(":").at(-1) === "fldCharType",
- )?.[1] ?? "",
- );
+ const type = wordAttribute(attributes, "fldCharType");
if (type === "begin")
fields.push({ instructions: true, literal: [], result: [] });
else if (type === "separate" && fields.length > 0)
@@ -434,7 +448,12 @@ function extractDocx(path: string, bytes: Uint8Array): string {
if (!bindings.length) declarations.delete(key);
}
const localName = name.split(":").at(-1);
- if (deletedDepth === 0 && localName === "txbxContent") {
+ if (ignoredDepth > 0) {
+ ignoredDepth--;
+ return;
+ }
+ if (localName === "sdt") placeholders.pop();
+ if (localName === "txbxContent") {
while (fields.length > 0) finishField(false);
const content = text.join("");
({ text, fields } = stories.pop()!);
@@ -443,14 +462,6 @@ function extractDocx(path: string, bytes: Uint8Array): string {
if (localName === "instrText") instructionText = false;
if (localName === "tabs") tabStops = false;
if (!isSelfClosing && localName === "p") append("\n");
- if (
- localName === "del" ||
- localName === "delText" ||
- localName === "delInstrText" ||
- localName === "moveFrom"
- ) {
- deletedDepth--;
- }
});
// Word stores drawing positions and relative sizes as character data.
const appendCharacters = (value: string) => {
diff --git a/sdk/typescript/tests-ts/knowledge-base.test.ts b/sdk/typescript/tests-ts/knowledge-base.test.ts
index e9e5d3008..7497a3d84 100644
--- a/sdk/typescript/tests-ts/knowledge-base.test.ts
+++ b/sdk/typescript/tests-ts/knowledge-base.test.ts
@@ -532,6 +532,18 @@ describe("scan knowledge bases", () => {
'<w:p><w:moveTo w:id="3" w:author="Example Author"><w:r><w:t>Audit weekly.</w:t></w:r></w:moveTo></w:p>',
),
);
+ await writeFile(
+ join(root, "content-controls.docx"),
+ docxBody(
+ '<w:p><w:r><w:t xml:space="preserve">Owner: </w:t></w:r>' +
+ "<w:sdt><w:sdtPr><w:showingPlcHdr/></w:sdtPr><w:sdtContent>" +
+ "<w:r><w:t>Click here to enter text.</w:t></w:r></w:sdtContent></w:sdt></w:p>" +
+ '<w:p><w:r><w:t xml:space="preserve">Data: </w:t></w:r>' +
+ '<w:sdt><w:sdtPr><w:showingPlcHdr w:val="0"/></w:sdtPr><w:sdtContent>' +
+ "<w:r><w:t>Confidential</w:t></w:r>" +
+ "</w:sdtContent></w:sdt></w:p>",
+ ),
+ );
await writeFile(
join(root, "field-code.docx"),
docxBody(
@@ -555,6 +567,7 @@ describe("scan knowledge bases", () => {
expect(documents).toContain("Admin API is internal only\n");
expect(documents).toContain("Rotate keys. \nAudit weekly.\n");
expect(documents).toContain("See the runbook\n");
+ expect(documents).toContain("Owner: \nData: Confidential\n");
});
test.each([