Skip to content

Commit 491d980

Browse files
committed
fix: prevent xss in parameters and steps
1 parent 8ae5665 commit 491d980

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import { SafeString } from "handlebars/runtime";
1+
import { escapeExpression, SafeString } from "handlebars/runtime";
22

33
const URL_REGEXP = /^(\w)+:\/\/.*/;
44

55
export default function (text) {
6-
return URL_REGEXP.test(text)
7-
? new SafeString(`<a href="${text}" class="link" target="_blank">${text}</a>`)
8-
: text;
6+
if (!URL_REGEXP.test(text)) {
7+
return text;
8+
}
9+
10+
const safeText = escapeExpression(text);
11+
12+
return new SafeString(
13+
`<a href="${safeText}" class="link" target="_blank" rel="noopener noreferrer">${safeText}</a>`
14+
);
915
}
Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
import { SafeString } from "handlebars/runtime";
1+
import { escapeExpression, SafeString } from "handlebars/runtime";
22

3-
const URL_REGEXP = /((?:(https?:\/\/|ftp:\/\/|mailto:)|www\.)\S+?)(\s|"|'|\)|]|}|&#62|$)/gm;
4-
5-
const encodeHTMLEntities = (rawString) =>
6-
rawString.replace(/[\u00A0-\u9999<>&]/gim, (i) => `&#${i.charCodeAt(0)};`);
3+
const URL_REGEXP =
4+
/((?:(https?:\/\/|ftp:\/\/|mailto:)|www\.)\S+?)(\s|"|'|\)|]|}|&#62|$)/gm;
75

86
export default function (text) {
97
const hasUrl = text !== undefined && text.match(URL_REGEXP);
10-
return hasUrl
11-
? new SafeString(
12-
encodeHTMLEntities(text).replace(
13-
URL_REGEXP,
14-
(_, urlFullText, urlProtocol, terminalSymbol) => {
15-
return `<a class="link" target="_blank" href="${
16-
urlProtocol ? urlFullText : `https://${urlFullText}`
17-
}">${urlFullText}</a>${terminalSymbol} `;
18-
},
19-
),
20-
)
21-
: text;
8+
9+
if (!hasUrl) {
10+
return text;
11+
}
12+
13+
const escapedText = escapeExpression(text);
14+
15+
return new SafeString(
16+
escapedText.replace(
17+
URL_REGEXP,
18+
(_, urlFullText, urlProtocol, terminalSymbol) => {
19+
const href = urlProtocol ? urlFullText : `https://${urlFullText}`;
20+
21+
// eslint-disable-next-line max-len
22+
return `<a class="link" target="_blank" href="${href}" rel="noopener noreferrer">${urlFullText}</a>${terminalSymbol} `;
23+
},
24+
),
25+
);
2226
}

0 commit comments

Comments
 (0)