Skip to content

Commit

Permalink
fix: Ensure url is valid before opening to prevent XSS (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
scottlovegrove committed Jul 4, 2024
1 parent 0bdb93c commit 68537a3
Showing 1 changed file with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,28 @@ export function registerMarkdownParser(markdownParser: (text: string) => string)
}
}

/**
* Protects against XSS attacks by validating the URL.
* @param url
* @returns
*/
function isValidUrl(url: string): boolean {
try {
// Parse the URL using the URL constructor
const parsedUrl = new URL(url)

// Check for allowed protocols
if (parsedUrl.protocol === 'http:' || parsedUrl.protocol === 'https:') {
return true
} else {
return false
}
} catch {
// If URL constructor throws an error, it's an invalid URL
return false
}
}

/**
* To support markdown, register a markdown parser via `registerMarkdownParser`
* @see registerMarkdownParser
Expand Down Expand Up @@ -97,7 +119,7 @@ export function AdaptiveCardRenderer({
try {
const inputs = adaptiveCard.getAllInputs()
const inputsObject = getInputObject(inputs)
if (action instanceof OpenUrlAction && action.url) {
if (action instanceof OpenUrlAction && action.url && isValidUrl(action.url)) {
window.open(action.url, '_blank')
} else if (action instanceof ClipboardAction && action.text) {
clipboardHandler(action.text)
Expand Down

0 comments on commit 68537a3

Please sign in to comment.