mirror of
https://github.com/Elysola/WebHooker.git
synced 2026-11-20 17:13:49 +00:00
fix(security): harden URL and HTML sanitization
This commit is contained in:
6 files changed
+9
-6
No files matched your search
@@ -7,7 +7,7 @@ import { renderNeutralMessage } from "./render";
|
||||
const markdown = new MarkdownIt({ html: false, linkify: false, breaks: false, typographer: false });
|
||||
|
||||
function markdownToText(value: string): string {
|
||||
return markdown.renderInline(value).replace(/<[^>]*>/gu, "");
|
||||
return markdown.renderInline(value).replace(/<[^>]*>/gu, "").replace(/</g, "");
|
||||
}
|
||||
|
||||
function smallAvatar(url: string): string {
|
||||
|
||||
@@ -74,7 +74,7 @@ function capHtml(text: string, max: number): string {
|
||||
const weight = entity ? 1 : step;
|
||||
// One character is kept for the ellipsis.
|
||||
if (shown + weight > max - 1) {
|
||||
const rest = text.slice(i).replace(/<[^>]*>/g, "");
|
||||
const rest = text.slice(i).replace(/<[^>]*>/g, "").replace(/</g, "");
|
||||
// What is left fits after all (no ellipsis needed).
|
||||
if (shown + rest.replace(/&(?:#\d+|#x[0-9a-f]+|[a-z]+);/gi, "x").length <= max) return text;
|
||||
const closers = [...stack]
|
||||
|
||||
@@ -218,6 +218,9 @@ function stripHtml(input: string): string {
|
||||
.replace(/>/gi, ">")
|
||||
.replace(/"/gi, '"')
|
||||
.replace(/'/gi, "'")
|
||||
// Entity decoding can reveal a tag prefix after the first pass. Remove
|
||||
// angle brackets again so encoded `<script` cannot survive sanitization.
|
||||
.replace(/</g, "")
|
||||
.replace(/&#x([0-9a-f]+);/gi, (m, hex: string) => codePoint(parseInt(hex, 16), m))
|
||||
.replace(/&#(\d+);/g, (m, dec: string) => codePoint(Number(dec), m))
|
||||
.replace(/&/gi, "&");
|
||||
|
||||
Reference in new issue
Block a user