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

fix: Drop redundant sanitation for Find and Replace #2598

Merged
merged 1 commit into from
Dec 22, 2023
Merged
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
13 changes: 1 addition & 12 deletions api.planx.uk/modules/flows/findReplace/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ import { z } from "zod";
import { ServerError } from "../../../errors";
import { findAndReplaceInFlow } from "./service";
import { FlowGraph } from "@opensystemslab/planx-core/types";
import { JSDOM } from "jsdom";
import createDOMPurify from "dompurify";

// Setup JSDOM and DOMPurify
const window = new JSDOM("").window;
const DOMPurify = createDOMPurify(window);

interface FindAndReplaceResponse {
message: string;
Expand All @@ -23,12 +17,7 @@ export const findAndReplaceSchema = z.object({
}),
query: z.object({
find: z.string(),
replace: z
.string()
.optional()
.transform(
(val) => val && DOMPurify.sanitize(val, { ADD_ATTR: ["target"] }),
),
replace: z.string().optional(),
}),
});

Expand Down
276 changes: 0 additions & 276 deletions api.planx.uk/modules/flows/findReplace/findReplace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,279 +265,3 @@ describe("string replacement", () => {
});
});
});

describe("HTML replacement", () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropping this suite does not affect the percentage of test coverage, and I'll make sure it gets picked up in an E2E test that can genuinely test this.

const originalHTML = `<img src="cuteDogs.jpg"/>`;
const mockFlowData: Flow["data"] = {
_root: {
edges: ["RRQwM2zAgy", "vcTgmVQAre", "QsEdip17H5"],
},
"6dwuQp5xjA": {
data: {
text: "No",
},
type: 200,
},
"8AWcYxZgBw": {
data: {
fn: "property.constraints.planning",
text: "Is it monument inside a portal?",
},
type: 100,
edges: ["AJnWX6O1xt", "6dwuQp5xjA"],
},
AJnWX6O1xt: {
data: {
val: "designated.monument",
text: "Yes",
description: originalHTML,
},
type: 200,
},
Hfh8KuSzUq: {
data: {
val: "designated.monument",
text: "Yes",
description: originalHTML,
},
type: 200,
},
RRQwM2zAgy: {
data: {
fn: "property.constraints.planning",
text: "Is it a monument",
},
type: 100,
edges: ["Hfh8KuSzUq", "ft26KlH7Oy"],
},
ft26KlH7Oy: {
data: {
text: "No",
},
type: 200,
},
vcTgmVQAre: {
data: {
text: "internal-portal-test",
},
type: 300,
edges: ["8AWcYxZgBw"],
},
QsEdip17H5: {
type: 310,
data: {
flowId: "f54b6505-c352-4fbc-aca3-7c4be99b49d4",
},
},
};

beforeEach(() => {
queryMock.mockQuery({
name: "GetFlowData",
matchOnVariables: false,
data: {
flow: {
data: mockFlowData,
slug: "test",
},
},
});
});

describe("Replacing unsafe HTML", () => {
const unsafeHTML = "<img src=x onerror=prompt('Stored XSS')/>";
const safeHTML = '<img src="x"/>';

const replacedFlowData: Flow["data"] = {
_root: {
edges: ["RRQwM2zAgy", "vcTgmVQAre", "QsEdip17H5"],
},
"6dwuQp5xjA": {
data: {
text: "No",
},
type: 200,
},
"8AWcYxZgBw": {
data: {
fn: "property.constraints.planning",
text: "Is it monument inside a portal?",
},
type: 100,
edges: ["AJnWX6O1xt", "6dwuQp5xjA"],
},
AJnWX6O1xt: {
data: {
val: "monument",
text: "Yes",
description: safeHTML,
},
type: 200,
},
Hfh8KuSzUq: {
data: {
val: "monument",
text: "Yes",
description: safeHTML,
},
type: 200,
},
RRQwM2zAgy: {
data: {
fn: "property.constraints.planning",
text: "Is it a monument",
},
type: 100,
edges: ["Hfh8KuSzUq", "ft26KlH7Oy"],
},
ft26KlH7Oy: {
data: {
text: "No",
},
type: 200,
},
vcTgmVQAre: {
data: {
text: "internal-portal-test",
},
type: 300,
edges: ["8AWcYxZgBw"],
},
QsEdip17H5: {
type: 310,
data: {
flowId: "f54b6505-c352-4fbc-aca3-7c4be99b49d4",
},
},
};

beforeEach(() => {
queryMock.mockQuery({
name: "UpdateFlow",
matchOnVariables: false,
data: {
flow: {
data: replacedFlowData,
slug: "test",
},
},
});
});

it("sanitises unsafe replace values", async () => {
await supertest(app)
.post(`/flows/2/search?find=${originalHTML}&replace=${unsafeHTML}`)
.set(auth)
.expect(200)
.then((res) => {
expect(res.body).toEqual({
message:
'Found 2 matches of "<img src="cuteDogs.jpg"/>" and replaced with "<img src="x">"',
matches: {
AJnWX6O1xt: {
data: {
description: '<img src="cuteDogs.jpg"/>',
},
},
Hfh8KuSzUq: {
data: {
description: '<img src="cuteDogs.jpg"/>',
},
},
},
updatedFlow: replacedFlowData,
});
});
});
});

describe("Replacing safe HTML", () => {
const replacementHTML = `<a target="_blank" href="https://www.cute-dogs.com" rel="noopener noreferrer"/>`;
const replacedFlowData: Flow["data"] = {
_root: {
edges: ["RRQwM2zAgy", "vcTgmVQAre", "QsEdip17H5"],
},
"6dwuQp5xjA": {
data: {
text: "No",
},
type: 200,
},
"8AWcYxZgBw": {
data: {
fn: "property.constraints.planning",
text: "Is it monument inside a portal?",
},
type: 100,
edges: ["AJnWX6O1xt", "6dwuQp5xjA"],
},
AJnWX6O1xt: {
data: {
val: "monument",
text: "Yes",
description: replacementHTML,
},
type: 200,
},
Hfh8KuSzUq: {
data: {
val: "monument",
text: "Yes",
description: replacementHTML,
},
type: 200,
},
RRQwM2zAgy: {
data: {
fn: "property.constraints.planning",
text: "Is it a monument",
},
type: 100,
edges: ["Hfh8KuSzUq", "ft26KlH7Oy"],
},
ft26KlH7Oy: {
data: {
text: "No",
},
type: 200,
},
vcTgmVQAre: {
data: {
text: "internal-portal-test",
},
type: 300,
edges: ["8AWcYxZgBw"],
},
QsEdip17H5: {
type: 310,
data: {
flowId: "f54b6505-c352-4fbc-aca3-7c4be99b49d4",
},
},
};

beforeEach(() => {
queryMock.mockQuery({
name: "UpdateFlow",
matchOnVariables: false,
data: {
flow: {
data: replacedFlowData,
slug: "test",
},
},
});
});

it("does not remove the 'target' attribute from anchors", async () => {
await supertest(app)
.post(`/flows/2/search?find=${originalHTML}&replace=${replacementHTML}`)
.set(auth)
.expect(200)
.then((res) => {
// Checking substring as DOMPurify rearranges attribute order in a non-deterministic manner
expect(res.body.message).toMatch(/target=["]\\?_blank\\?["]/);
});
});
});
});
Loading