Skip to content
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

fix: Ensure url is valid before opening to prevent XSS #69

Merged
merged 1 commit into from
Jul 4, 2024
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/ui-extensions-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@doist/ui-extensions-react",
"version": "12.0.0",
"version": "12.0.1",
"author": "Doist",
"license": "MIT",
"main": "dist/index.js",
Expand Down
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
Loading