Skip to content
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
26 changes: 23 additions & 3 deletions packages/router-core/src/redirect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SAFE_URL_PROTOCOLS, isDangerousProtocol } from './utils'
import type { NavigateOptions } from './link'
import type { AnyRouter, RegisteredRouter } from './router'
import type { ParsedLocation } from './location'

export type AnyRedirect = Redirect<any, any, any, any, any>

Expand All @@ -14,7 +15,13 @@ export type Redirect<
TMaskFrom extends string = TFrom,
TMaskTo extends string = '.',
> = Response & {
options: NavigateOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo>
options: NavigateOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo> & {
/**
* @internal
* A **trusted** built location that can be used to redirect to.
*/
_builtLocation?: ParsedLocation
}
redirectHandled?: boolean
}

Expand Down Expand Up @@ -45,6 +52,11 @@ export type RedirectOptions<
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RedirectType#headers-property)
*/
headers?: HeadersInit
/**
* @internal
* A **trusted** built location that can be used to redirect to.
*/
_builtLocation?: ParsedLocation
} & NavigateOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo>

export type ResolvedRedirect<
Expand Down Expand Up @@ -113,13 +125,21 @@ export function redirect<
opts.statusCode = opts.statusCode || opts.code || 307

// Block dangerous protocols in redirect href
if (typeof opts.href === 'string' && isDangerousProtocol(opts.href)) {
if (
!opts._builtLocation &&
typeof opts.href === 'string' &&
isDangerousProtocol(opts.href)
) {
throw new Error(
`Redirect blocked: unsafe protocol in href "${opts.href}". Only ${SAFE_URL_PROTOCOLS.join(', ')} protocols are allowed.`,
)
}

if (!opts.reloadDocument && typeof opts.href === 'string') {
if (
!opts._builtLocation &&
!opts.reloadDocument &&
typeof opts.href === 'string'
) {
try {
new URL(opts.href)
opts.reloadDocument = true
Expand Down
12 changes: 8 additions & 4 deletions packages/router-core/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2292,8 +2292,11 @@ export class RouterCore<
// always uses this.origin when constructing URLs
if (this.latestLocation.publicHref !== nextLocation.publicHref) {
const href = this.getParsedLocationHref(nextLocation)

throw redirect({ href })
if (nextLocation.external) {
throw redirect({ href })
} else {
throw redirect({ href, _builtLocation: nextLocation })
}
}
}

Expand Down Expand Up @@ -2622,8 +2625,9 @@ export class RouterCore<
resolveRedirect = (redirect: AnyRedirect): AnyRedirect => {
const locationHeader = redirect.headers.get('Location')

if (!redirect.options.href) {
const location = this.buildLocation(redirect.options)
if (!redirect.options.href || redirect.options._builtLocation) {
const location =
redirect.options._builtLocation ?? this.buildLocation(redirect.options)
const href = this.getParsedLocationHref(location)
redirect.options.href = href
redirect.headers.set('Location', href)
Expand Down
Loading