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: Add sanitization of the redirect uri to prevent XSS #277

Merged
merged 1 commit into from
Jan 17, 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
17 changes: 15 additions & 2 deletions packages/oauth/src/form/login/form-auth-login-service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { hawtio } from '@hawtio/react'
import { log } from '../../globals'
import { oAuthService } from '../../oauth-service'
import { FetchOptions, fetchPath, joinPaths, redirect, relToAbsUrl } from '../../utils'
import {
FetchOptions,
fetchPath,
joinPaths,
redirect,
relToAbsUrl,
sanitizeUri,
validateRedirectURI,
} from '../../utils'
import { FORM_TOKEN_STORAGE_KEY } from '../globals'

export type ValidationCallback = {
Expand Down Expand Up @@ -58,7 +66,12 @@ class FormAuthLoginService {
const currentUri = new URL(window.location.href)
const searchParams: URLSearchParams = currentUri.searchParams
if (searchParams.has('redirect_uri')) {
return searchParams.get('redirect_uri') as string
const uri = new URL(searchParams.get('redirect_uri') as string)
if (validateRedirectURI(uri)) {
return sanitizeUri(uri)
} else {
log.error('invalid redirect_uri', uri.toString())
}
}

return relToAbsUrl(hawtio.getBasePath() || window.location.origin)
Expand Down
22 changes: 22 additions & 0 deletions packages/oauth/src/utils/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,28 @@ export function logoutRedirect(redirectUri: URL): void {
})
}

export function validateRedirectURI(redirectUri: URL) {
const currentUrl = new URL(window.location.href)
const { hostname, port, protocol } = redirectUri
return (
hostname === currentUrl.hostname &&
port === currentUrl.port &&
protocol === currentUrl.protocol &&
['http:', 'https:'].includes(protocol)
)
}

export function sanitizeUri(url: URL) {
const searchParams = url.searchParams

if (searchParams.toString() !== '') {
searchParams.forEach((value, key) => {
searchParams.set(key, encodeURIComponent(value))
})
}
return url.href
}

export function redirect(target: URL) {
log.debug('Redirecting to URI:', target)
// Redirect to the target URI
Expand Down