Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/typescript/src/knowledge-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ function extractDocx(path: string, bytes: Uint8Array): string {
return decodeXml(
xml
.replace(/<\/(?:\w+:)?p\s*>/gu, "\n")
.replace(/<(?:\w+:)?br\b[^>]*>/gu, "\n")
.replace(/<(?:\w+:)?tab\b[^>]*\/>/gu, "\t")
.replace(/<[^>]+>/gu, ""),
);
Expand Down
20 changes: 16 additions & 4 deletions sdk/typescript/tests-ts/knowledge-base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,14 @@ async function extractedDocuments(path: string): Promise<string[]> {
);
}

function docx(text: string): Uint8Array {
function docx(
text: string,
secondLine?: string,
breakElement = "<w:br/>",
): Uint8Array {
return zipSync({
"word/document.xml": strToU8(
`<?xml version="1.0"?><w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body><w:p><w:r><w:t>${text}</w:t></w:r></w:p></w:body></w:document>`,
`<?xml version="1.0"?><w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body><w:p><w:r><w:t>${text}</w:t></w:r>${secondLine === undefined ? "" : `${breakElement}<w:r><w:t>${secondLine}</w:t></w:r>`}</w:p></w:body></w:document>`,
),
});
}
Expand Down Expand Up @@ -111,14 +115,22 @@ describe("scan knowledge bases", () => {
join(root, "architecture.pdf"),
pdf("Payment service boundary"),
);
await writeFile(join(root, "threat-model.docx"), docx("SSRF &amp; IDOR"));
await writeFile(
join(root, "threat-model.docx"),
docx("SSRF &amp; IDOR", "Review authentication"),
);
await writeFile(
join(root, "paired-break.docx"),
docx("Authorization", "Review permissions", "<w:br></w:br>"),
);

const knowledgeBase = await prepareKnowledgeBase([root]);
temporaryDirectories.push(knowledgeBase.path);
const documents = await extractedDocuments(knowledgeBase.path);

expect(documents).toContain("Payment service boundary");
expect(documents).toContain("SSRF & IDOR\n");
expect(documents).toContain("SSRF & IDOR\nReview authentication\n");
expect(documents).toContain("Authorization\nReview permissions\n");
});

test("cleans up documents and rediscovers directory contents on later runs", async () => {
Expand Down