Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test → val (9 January 2025) #843

Merged
merged 7 commits into from
Jan 9, 2025
45 changes: 7 additions & 38 deletions services/app-api/utils/sanitize/sanitize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,25 @@ const safeUndefined = undefined;

const cleanString = "test";

const dirtyImgString = '<img src="foo.png" onload="alert("Hello!")"/>';
const cleanImgString = '<img src="foo.png">';

const dirtyLinkString = "<UL><li><A HREF=//google.com>click</UL>";
const dirtyLinkString = "<ul><li><a href=//google.com>click</ul>";
const cleanLinkString = '<ul><li><a href="//google.com">click</a></li></ul>';

const dirtyScriptString =
"<math><mi//xlink:href='data:x,<script>alert(4)</script>'>";
const cleanScriptString = "<math><mi></mi></math>";

const dirtySvgString = "<svg><g/onload=alert(2)//<p>";
const cleanSvgString = "<svg><g></g></svg>";

// ARRAYS

const dirtyStringArray = [
cleanString,
dirtyImgString,
dirtyLinkString,
dirtySvgString,
dirtyScriptString,
];
const cleanStringArray = [
cleanString,
cleanImgString,
cleanLinkString,
cleanSvgString,
cleanScriptString,
];
const dirtyStringArray = [cleanString, dirtyLinkString];
const cleanStringArray = [cleanString, cleanLinkString];

const dirtyNestedStringArray = [dirtyStringArray, dirtyStringArray];
const cleanNestedStringArray = [cleanStringArray, cleanStringArray];

// OBJECTS

const dirtyObject = {
string: dirtyImgString,
string: dirtyLinkString,
array: dirtyStringArray,
};
const cleanObject = {
string: cleanImgString,
string: cleanLinkString,
array: cleanStringArray,
};

Expand All @@ -61,10 +39,7 @@ const cleanObjectArray = [cleanObject, cleanObject];

const dirtyComplexObject = {
string1: cleanString,
string2: dirtyImgString,
string3: dirtyLinkString,
string4: dirtySvgString,
string5: dirtyScriptString,
string2: dirtyLinkString,
array: dirtyStringArray,
nestedStringArray: dirtyNestedStringArray,
nestedObjectArray: dirtyObjectArray,
Expand All @@ -74,10 +49,7 @@ const dirtyComplexObject = {
};
const cleanComplexObject = {
string1: cleanString,
string2: cleanImgString,
string3: cleanLinkString,
string4: cleanSvgString,
string5: cleanScriptString,
string2: cleanLinkString,
array: cleanStringArray,
nestedStringArray: cleanNestedStringArray,
nestedObjectArray: cleanObjectArray,
Expand All @@ -93,10 +65,7 @@ describe("Test sanitizeString", () => {
});

test("Test sanitizeString cleans dirty strings", () => {
expect(sanitizeString(dirtyImgString)).toEqual(cleanImgString);
expect(sanitizeString(dirtyLinkString)).toEqual(cleanLinkString);
expect(sanitizeString(dirtySvgString)).toEqual(cleanSvgString);
expect(sanitizeString(dirtyScriptString)).toEqual(cleanScriptString);
});
});

Expand Down
16 changes: 16 additions & 0 deletions services/app-api/utils/sanitize/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ import { JSDOM } from "jsdom";
const windowEmulator: any = new JSDOM("").window;
const DOMPurify = createDOMPurify(windowEmulator);

/*
* DOMPurify prevents all XSS attacks by default. With these settings, it also
* prevents "deception" attacks. If an attacker could put <div style="...">
* into the site's admin banner, they could make give the banner any appearance,
* overlaid anywhere on the page. For example, a fake "session expired" modal
* with a malicious link. Thus, this very strict DOMPurify config.
*/
DOMPurify.setConfig({
// Only these tags will be allowed through
ALLOWED_TAGS: ["ul", "ol", "li", "a", "#text"],
// On those tags, only these attributes are allowed
ALLOWED_ATTR: ["href", "alt"],
// If a tag is removed, so will all its child elements & text
KEEP_CONTENT: false,
});

// sanitize string
export const sanitizeString = (string: string) => {
if (DOMPurify.isSupported) {
Expand Down
6 changes: 3 additions & 3 deletions services/database/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"serverless-dynamodb": "^0.2.53"
},
"dependencies": {
"@aws-sdk/client-dynamodb": "^3.693.0",
"@aws-sdk/client-s3": "^3.693.0",
"@aws-sdk/lib-dynamodb": "^3.693.0"
"@aws-sdk/client-dynamodb": "^3.699.0",
"@aws-sdk/client-s3": "^3.699.0",
"@aws-sdk/lib-dynamodb": "^3.699.0"
}
}
Loading
Loading