mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 22:09:16 +01:00
refactor(api): 📦 Change sanitizer from DOMPurify to xss
This commit is contained in:
parent
a430db5c30
commit
154f17ab12
7 changed files with 99 additions and 52 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import { apiRoute, applyConfig } from "@api";
|
||||
import { errorResponse, jsonResponse } from "@response";
|
||||
import { sanitizeHtml } from "@sanitization";
|
||||
import { sanitizeHtml, sanitizedHtmlStrip } from "@sanitization";
|
||||
import { config } from "config-manager";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import ISO6391 from "iso-639-1";
|
||||
|
|
@ -97,10 +97,9 @@ export default apiRoute<typeof meta, typeof schema>(
|
|||
|
||||
const sanitizedNote = await sanitizeHtml(note ?? "");
|
||||
|
||||
const sanitizedDisplayName = await sanitizeHtml(display_name ?? "", {
|
||||
ALLOWED_TAGS: [],
|
||||
ALLOWED_ATTR: [],
|
||||
});
|
||||
const sanitizedDisplayName = await sanitizedHtmlStrip(
|
||||
display_name ?? "",
|
||||
);
|
||||
|
||||
let mediaManager: MediaBackend;
|
||||
|
||||
|
|
|
|||
|
|
@ -361,4 +361,62 @@ describe(meta.route, () => {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("HTML injection testing", () => {
|
||||
test("should not allow HTML injection", async () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(new URL(meta.route, config.http.base_url), {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${tokens[0].accessToken}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
status: "Hi! <script>alert('Hello, world!');</script>",
|
||||
federate: false,
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
const object = (await response.json()) as APIStatus;
|
||||
|
||||
expect(object.content).toBe(
|
||||
"<p>Hi! <script>alert('Hello, world!');</script></p>",
|
||||
);
|
||||
});
|
||||
|
||||
test("should not allow HTML injection in spoiler_text", async () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(new URL(meta.route, config.http.base_url), {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${tokens[0].accessToken}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
status: "Hello, world!",
|
||||
spoiler_text:
|
||||
"uwu <script>alert('Hello, world!');</script>",
|
||||
federate: false,
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toBe(
|
||||
"application/json",
|
||||
);
|
||||
|
||||
const object = (await response.json()) as APIStatus;
|
||||
|
||||
expect(object.spoiler_text).toBe(
|
||||
"uwu <script>alert('Hello, world!');</script>",
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue