Skip to content

Commit

Permalink
add label processing before regex comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
AllenDing authored and AllenDing committed Feb 14, 2024
1 parent 028e773 commit 83752ef
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion web/src/app/util/safeURL.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
// decodeHtmlEntites will decode common HTML entities in a string.
//
// This is useful for ensuring that any encoded characters in the text are converted
// back to their original form for comparision.
function decodeHtmlEntites(text: string): string {
return text
.replace(/&/g, '&')

Check failure

Code scanning / CodeQL

Double escaping or unescaping High

This replacement may produce '&' characters that are double-unescaped
here
.
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&quot;/g, '"')
.replace(/&#39;/g, "'")
}

// safeURL will determine if a url is safe for linking.
//
// It tries to determine if the label is misleading.
export function safeURL(url: string, label: string): boolean {
url = decodeHtmlEntites(url)
label = decodeHtmlEntites(label)

if (url.startsWith('mailto:')) {
const email = url.substr(7)
return email === label && email.includes('@')
Expand All @@ -14,7 +30,7 @@ export function safeURL(url: string, label: string): boolean {

// handle http protocols
if (!/https?:\/\//.test(url)) return false // require absolute URLs
if (!/[./]/.test(label)) return true // don't consider it a path/url without slashes or periods
if (!/[./]/.test(url)) return true // don't consider it a path/url without slashes or periods
if (url.startsWith(label)) return true // if it matches the beginning, then it's fine
if (url.replace(/^https?:\/\//, '').startsWith(label)) return true // same prefix without protocol
if (url.replace(/^https?:\/\//, '').startsWith('www.' + label)) return true // same prefix without protocol
Expand Down

0 comments on commit 83752ef

Please sign in to comment.