11import { SAFE_URL_PROTOCOLS , isDangerousProtocol } from './utils'
22import type { NavigateOptions } from './link'
33import type { AnyRouter , RegisteredRouter } from './router'
4+ import type { ParsedLocation } from './location'
45
56export type AnyRedirect = Redirect < any , any , any , any , any >
67
@@ -14,7 +15,13 @@ export type Redirect<
1415 TMaskFrom extends string = TFrom ,
1516 TMaskTo extends string = '.' ,
1617> = Response & {
17- options : NavigateOptions < TRouter , TFrom , TTo , TMaskFrom , TMaskTo >
18+ options : NavigateOptions < TRouter , TFrom , TTo , TMaskFrom , TMaskTo > & {
19+ /**
20+ * @internal
21+ * A **trusted** built location that can be used to redirect to.
22+ */
23+ _builtLocation ?: ParsedLocation
24+ }
1825 redirectHandled ?: boolean
1926}
2027
@@ -45,6 +52,11 @@ export type RedirectOptions<
4552 * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RedirectType#headers-property)
4653 */
4754 headers ?: HeadersInit
55+ /**
56+ * @internal
57+ * A **trusted** built location that can be used to redirect to.
58+ */
59+ _builtLocation ?: ParsedLocation
4860} & NavigateOptions < TRouter , TFrom , TTo , TMaskFrom , TMaskTo >
4961
5062export type ResolvedRedirect <
@@ -113,13 +125,21 @@ export function redirect<
113125 opts . statusCode = opts . statusCode || opts . code || 307
114126
115127 // Block dangerous protocols in redirect href
116- if ( typeof opts . href === 'string' && isDangerousProtocol ( opts . href ) ) {
128+ if (
129+ ! opts . _builtLocation &&
130+ typeof opts . href === 'string' &&
131+ isDangerousProtocol ( opts . href )
132+ ) {
117133 throw new Error (
118134 `Redirect blocked: unsafe protocol in href "${ opts . href } ". Only ${ SAFE_URL_PROTOCOLS . join ( ', ' ) } protocols are allowed.` ,
119135 )
120136 }
121137
122- if ( ! opts . reloadDocument && typeof opts . href === 'string' ) {
138+ if (
139+ ! opts . _builtLocation &&
140+ ! opts . reloadDocument &&
141+ typeof opts . href === 'string'
142+ ) {
123143 try {
124144 new URL ( opts . href )
125145 opts . reloadDocument = true
0 commit comments