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): omit DOCX drawing positions and sizes from extracted text (#2051)

Lanre Shittu1eb3692ff6f3
diff --git a/sdk/typescript/src/knowledge-base.ts b/sdk/typescript/src/knowledge-base.ts
index 756dccc69..878d0a066 100644
--- a/sdk/typescript/src/knowledge-base.ts
+++ b/sdk/typescript/src/knowledge-base.ts
@@ -332,6 +332,20 @@ function extractDocx(path: string, bytes: Uint8Array): string {
}[] = [];
const stories: { text: string[]; fields: typeof fields }[] = [];
const parser = new SaxesParser();
+ const positions = new Set(["align", "posOffset"]);
+ const drawingLayout = new Map([
+ [
+ "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",
+ positions,
+ ],
+ ["http://purl.oclc.org/ooxml/drawingml/wordprocessingDrawing", positions],
+ [
+ "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing",
+ new Set(["pctPosHOffset", "pctPosVOffset", "pctWidth", "pctHeight"]),
+ ],
+ ]);
+ const declarations = new Map<string, (Set<string> | undefined)[]>();
+ const layout: boolean[] = [];
let documentElement = false;
let deletedDepth = 0;
let instructionText = false;
@@ -351,8 +365,19 @@ function extractDocx(path: string, bytes: Uint8Array): string {
append((closed ? "" : field.literal.join("")) + field.result.join(""));
}
};
+ parser.on("attribute", ({ name, value }) => {
+ if (name !== "xmlns" && !name.startsWith("xmlns:")) return;
+ const bindings = declarations.get(name) ?? [];
+ bindings.push(drawingLayout.get(decodeXml(value)));
+ declarations.set(name, bindings);
+ });
parser.on("opentag", ({ name, attributes }) => {
- const localName = name.split(":").at(-1);
+ const colon = name.indexOf(":");
+ const declaration = colon < 0 ? "xmlns" : "xmlns:" + name.slice(0, colon);
+ const localName = name.split(":").at(-1)!;
+ layout.push(
+ declarations.get(declaration)?.at(-1)?.has(localName) ?? false,
+ );
if (
localName === "del" ||
localName === "delText" ||
@@ -385,7 +410,14 @@ function extractDocx(path: string, bytes: Uint8Array): string {
else if (localName === "tab" && !tabStops) append("\t");
else if (localName === "noBreakHyphen") append("-");
});
- parser.on("closetag", ({ name, isSelfClosing }) => {
+ parser.on("closetag", ({ name, attributes, isSelfClosing }) => {
+ layout.pop();
+ for (const key of Object.keys(attributes)) {
+ const bindings = declarations.get(key);
+ if (!bindings) continue;
+ bindings.pop();
+ if (!bindings.length) declarations.delete(key);
+ }
const localName = name.split(":").at(-1);
if (deletedDepth === 0 && localName === "txbxContent") {
while (fields.length > 0) finishField(false);
@@ -405,8 +437,14 @@ function extractDocx(path: string, bytes: Uint8Array): string {
deletedDepth--;
}
});
- parser.on("text", (value) => append(decodeXml(value)));
- parser.on("cdata", (value) => append(value.replaceAll("&amp;", "&")));
+ // Word stores drawing positions and relative sizes as character data.
+ const appendCharacters = (value: string) => {
+ if (!layout.at(-1)) append(value);
+ };
+ parser.on("text", (value) => appendCharacters(decodeXml(value)));
+ parser.on("cdata", (value) =>
+ appendCharacters(value.replaceAll("&amp;", "&")),
+ );
// Keep the existing entity decoder's handling of unusable references.
// CDATA is literal text, so only undo this escaping in its handler.
parser.write(xml.replaceAll("&", "&amp;")).close();
diff --git a/sdk/typescript/tests-ts/knowledge-base.test.ts b/sdk/typescript/tests-ts/knowledge-base.test.ts
index 50325d605..393fac1b9 100644
--- a/sdk/typescript/tests-ts/knowledge-base.test.ts
+++ b/sdk/typescript/tests-ts/knowledge-base.test.ts
@@ -672,6 +672,20 @@ describe("scan knowledge bases", () => {
"<w:r><w:instrText>Visible scope</w:instrText></w:r></w:p>",
"Visible scope\n",
],
+ [
+ "drawing placement and relative dimensions around visible text",
+ '<w:p xmlns:d="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"><w:r><w:t>Before </w:t>' +
+ '<mc:AlternateContent xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">' +
+ '<mc:Choice Requires="wps"><w:drawing><d:anchor><d:positionH relativeFrom="column"><d:align>center</d:align></d:positionH>' +
+ '<d:positionV relativeFrom="paragraph"><d:posOffset><![CDATA[12700]]></d:posOffset></d:positionV>' +
+ "<wps:txbx><w:txbxContent><w:p><w:r><w:t>Rule 12700.</w:t></w:r></w:p></w:txbxContent></wps:txbx>" +
+ '<sizeRelH xmlns="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"><pctWidth>75000</pctWidth></sizeRelH>' +
+ '</d:anchor></w:drawing></mc:Choice><mc:Fallback><w:pict><v:textbox xmlns:v="urn:schemas-microsoft-com:vml">' +
+ '<div xmlns:d="urn:example:format">Rule <d:align>12700.</d:align></div></v:textbox></w:pict></mc:Fallback></mc:AlternateContent>' +
+ "<w:drawing><d:anchor><d:positionH><d:posOffset>25400</d:posOffset></d:positionH></d:anchor></w:drawing>" +
+ "<w:t> After.</w:t></w:r></w:p>",
+ "Before Rule 12700.\nRule 12700. After.\n",
+ ],
])("preserves DOCX text with %s", async (_description, body, expected) => {
const root = await temporaryDirectory();
const source = join(root, "field-context.docx");