Skip to content

PLAT-2461 fix style attribute and blockquote #56

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

Merged
merged 1 commit into from
Sep 12, 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
17 changes: 9 additions & 8 deletions src/domain/Challenge.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ChallengeDomain as LegacyChallengeDomain } from "@topcoder-framework/domain-acl";
import { DomainHelper, PhaseFactRequest, PhaseFactResponse } from "@topcoder-framework/lib-common";
import xss from "xss";
import { sanitize } from "../helpers/Sanitizer";
import CoreOperations from "../common/CoreOperations";
import { Value } from "../dal/models/nosql/parti_ql";
import IdGenerator from "../helpers/IdGenerator";
Expand Down Expand Up @@ -129,15 +129,15 @@ class ChallengeDomain extends CoreOperations<Challenge, CreateChallengeInput> {
}

public async create(input: CreateChallengeInput, metadata: Metadata): Promise<Challenge> {
input.name = xss(input.name);
input.name = sanitize(input.name);

// prettier-ignore
const handle = metadata?.get("handle").length > 0 ? metadata?.get("handle")?.[0].toString() : "tcwebservice";

if (Array.isArray(input.discussions)) {
for (const discussion of input.discussions) {
discussion.id = IdGenerator.generateUUID();
discussion.name = xss(discussion.name.substring(0, 100));
discussion.name = sanitize(discussion.name.substring(0, 100));
}
}

Expand All @@ -151,6 +151,7 @@ class ChallengeDomain extends CoreOperations<Challenge, CreateChallengeInput> {

// End Anti-Corruption Layer

// prettier-ignore
const challenge: Challenge = {
id: IdGenerator.generateUUID(),
created: now,
Expand All @@ -177,8 +178,8 @@ class ChallengeDomain extends CoreOperations<Challenge, CreateChallengeInput> {
legacy,
phases,
legacyId: legacyChallengeId != null ? legacyChallengeId : undefined,
description: xss(input.description ?? ""),
privateDescription: xss(input.privateDescription ?? ""),
description: sanitize(input.description ?? "", input.descriptionFormat),
privateDescription: sanitize(input.privateDescription ?? "", input.descriptionFormat),
metadata:
input.metadata.map((m) => {
let parsedValue = m.value;
Expand Down Expand Up @@ -286,14 +287,14 @@ class ChallengeDomain extends CoreOperations<Challenge, CreateChallengeInput> {
scanCriteria,
// prettier-ignore
{
name: input.name != null ? xss(input.name) : undefined,
name: input.name != null ? sanitize(input.name) : undefined,
typeId: input.typeId != null ? input.typeId : undefined,
trackId: input.trackId != null ? input.trackId : undefined,
timelineTemplateId: input.timelineTemplateId != null ? input.timelineTemplateId : undefined,
legacy: input.legacy != null ? input.legacy : undefined,
billing: input.billing != null ? input.billing : undefined,
description: input.description != null ? xss(input.description) : undefined,
privateDescription: input.privateDescription != null ? xss(input.privateDescription) : undefined,
description: input.description != null ? sanitize(input.description, input.descriptionFormat ?? challenge.descriptionFormat) : undefined,
privateDescription: input.privateDescription != null ? sanitize(input.privateDescription, input.descriptionFormat ?? challenge.descriptionFormat) : undefined,
descriptionFormat: input.descriptionFormat != null ? input.descriptionFormat : undefined,
task: input.task != null ? input.task : undefined,
winners: input.winnerUpdate != null ? input.winnerUpdate.winners : undefined,
Expand Down
38 changes: 38 additions & 0 deletions src/helpers/Sanitizer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as xss from "xss";

const xssWhiteTags = xss.getDefaultWhiteList();
for (const key of Object.keys(xssWhiteTags)) {
// Allow style attribute
if (!xssWhiteTags[key]?.includes("style")) {
xssWhiteTags[key]?.push("style");
}
}

// XSS filter for html
const htmlXSS = new xss.FilterXSS();

// XSS filter for markdown
const markdownXSS = new xss.FilterXSS({
whiteList: xssWhiteTags,
escapeHtml: (html) => {
const splitted = html.split("\n");
return splitted
.map((str) => {
// Handle blockquote which starts with '>'
const blockquoteMatched = str.match(/^\s*>/);
if (blockquoteMatched) {
// prettier-ignore
return blockquoteMatched[0] + str.substring(blockquoteMatched[0].length).replace(/</g, "&lt;").replace(/>/g, "&gt;");
}
return str.replace(/</g, "&lt;").replace(/>/g, "&gt;");
})
.join("\n");
},
});

export function sanitize(str: string, format: string = "html"): string {
if (!str) {
return str;
}
return format === "markdown" ? markdownXSS.process(str) : htmlXSS.process(str);
}