@@ -12,71 +12,73 @@ const SN_REFERRER = 'sn_referrer'
12
12
const SN_REFERRER_NONCE = 'sn_referrer_nonce'
13
13
// key for referred pages
14
14
const SN_REFEREE_LANDING = 'sn_referee_landing'
15
- // rewrite to ~subname paths
15
+ // rewrite to ~subName paths
16
16
const TERRITORY_PATHS = [ '/~' , '/recent' , '/random' , '/top' , '/post' , '/edit' ]
17
17
18
- // get a domain mapping from the cache
19
- export async function getDomainMapping ( domain ) {
20
- const domainMappings = await getDomainMappingsCache ( )
21
- return domainMappings ?. [ domain ]
22
- }
23
-
24
18
// Redirects and rewrites for custom domains
25
- export async function customDomainMiddleware ( request , referrerResp , domain ) {
26
- const host = request . headers . get ( 'host' )
27
- const url = request . nextUrl . clone ( )
19
+ async function customDomainMiddleware ( req , referrerResp , mainDomain , domain , subName ) {
20
+ // clone the request url to build on top of it
21
+ const url = req . nextUrl . clone ( )
22
+ // get the pathname from the url
28
23
const pathname = url . pathname
29
- const mainDomain = process . env . NEXT_PUBLIC_URL
24
+ // get the query params from the url
25
+ const query = url . searchParams
30
26
31
27
// TEST
32
- domainLogger ( ) . log ( 'host' , host )
33
- domainLogger ( ) . log ( 'mainDomain' , mainDomain )
28
+ domainLogger ( ) . log ( 'req.headers host' , req . headers . get ( 'host' ) )
29
+ domainLogger ( ) . log ( 'main domain' , mainDomain )
30
+ domainLogger ( ) . log ( 'custom domain' , domain )
31
+ domainLogger ( ) . log ( 'subName' , subName )
34
32
domainLogger ( ) . log ( 'pathname' , pathname )
35
- domainLogger ( ) . log ( 'query' , url . searchParams )
33
+ domainLogger ( ) . log ( 'query' , query )
36
34
37
- const requestHeaders = new Headers ( request . headers )
38
- requestHeaders . set ( 'x-stacker-news-subname' , domain . subName )
35
+ const requestHeaders = new Headers ( req . headers )
36
+ requestHeaders . set ( 'x-stacker-news-subname' , subName )
39
37
40
38
// Auth sync redirects with domain and optional callbackUrl and multiAuth params
41
39
if ( pathname === '/login' || pathname === '/signup' ) {
42
40
const redirectUrl = new URL ( pathname , mainDomain )
43
- redirectUrl . searchParams . set ( 'domain' , host )
44
- if ( url . searchParams . get ( 'callbackUrl' ) ) {
45
- redirectUrl . searchParams . set ( 'callbackUrl' , url . searchParams . get ( 'callbackUrl' ) )
41
+ redirectUrl . searchParams . set ( 'domain' , domain )
42
+ if ( query . get ( 'callbackUrl' ) ) {
43
+ redirectUrl . searchParams . set ( 'callbackUrl' , query . get ( 'callbackUrl' ) )
46
44
}
47
- if ( url . searchParams . get ( 'multiAuth' ) ) {
48
- redirectUrl . searchParams . set ( 'multiAuth' , url . searchParams . get ( 'multiAuth' ) )
45
+ if ( query . get ( 'multiAuth' ) ) {
46
+ redirectUrl . searchParams . set ( 'multiAuth' , query . get ( 'multiAuth' ) )
49
47
}
50
48
const redirectResp = NextResponse . redirect ( redirectUrl , {
51
49
headers : requestHeaders
52
50
} )
53
- return applyReferrerCookies ( redirectResp , referrerResp ) // apply referrer cookies to the redirect
51
+ // apply referrer cookies to the redirect
52
+ return applyReferrerCookies ( redirectResp , referrerResp )
54
53
}
55
54
56
55
// If trying to access a ~subname path, rewrite to /
57
- if ( pathname . startsWith ( `/~${ domain . subName } ` ) ) {
56
+ if ( pathname . startsWith ( `/~${ subName } ` ) ) {
58
57
// remove the territory prefix from the path
59
- const cleanPath = pathname . replace ( `/~${ domain . subName } ` , '' ) || '/'
58
+ const cleanPath = pathname . replace ( `/~${ subName } ` , '' ) || '/'
60
59
domainLogger ( ) . log ( 'Redirecting to clean path:' , cleanPath )
61
60
const redirectResp = NextResponse . redirect ( new URL ( cleanPath + url . search , url . origin ) , {
62
61
headers : requestHeaders
63
62
} )
64
- return applyReferrerCookies ( redirectResp , referrerResp ) // apply referrer cookies to the redirect
63
+ // apply referrer cookies to the redirect
64
+ return applyReferrerCookies ( redirectResp , referrerResp )
65
65
}
66
66
67
67
// If we're at the root or a territory path, rewrite to the territory path
68
68
if ( pathname === '/' || TERRITORY_PATHS . some ( p => pathname . startsWith ( p ) ) ) {
69
69
const internalUrl = new URL ( url )
70
- internalUrl . pathname = `/~${ domain . subName } ${ pathname === '/' ? '' : pathname } `
70
+ internalUrl . pathname = `/~${ subName } ${ pathname === '/' ? '' : pathname } `
71
71
domainLogger ( ) . log ( 'Rewrite to:' , internalUrl . pathname )
72
72
// rewrite to the territory path
73
73
const resp = NextResponse . rewrite ( internalUrl , {
74
74
headers : requestHeaders
75
75
} )
76
- return applyReferrerCookies ( resp , referrerResp ) // apply referrer cookies to the rewrite
76
+ // apply referrer cookies to the rewrite
77
+ return applyReferrerCookies ( resp , referrerResp )
77
78
}
78
79
79
- return NextResponse . next ( { // continue if we don't need to rewrite or redirect
80
+ // continue if we don't need to rewrite or redirect
81
+ return NextResponse . next ( {
80
82
request : {
81
83
headers : requestHeaders
82
84
}
@@ -171,7 +173,7 @@ function referrerMiddleware (request) {
171
173
return response
172
174
}
173
175
174
- export function applySecurityHeaders ( resp ) {
176
+ function applySecurityHeaders ( resp ) {
175
177
const isDev = process . env . NODE_ENV === 'development'
176
178
177
179
const nonce = Buffer . from ( crypto . randomUUID ( ) ) . toString ( 'base64' )
@@ -227,12 +229,18 @@ export async function middleware (request) {
227
229
}
228
230
229
231
// If we're on a custom domain, handle that next
230
- const host = request . headers . get ( 'host' )
231
- const isAllowedDomain = await getDomainMapping ( host ?. toLowerCase ( ) )
232
- if ( isAllowedDomain ) {
233
- domainLogger ( ) . log ( 'detected allowed custom domain' , isAllowedDomain )
234
- const customDomainResp = await customDomainMiddleware ( request , referrerResp , isAllowedDomain )
235
- return applySecurityHeaders ( customDomainResp )
232
+ const domain = request . headers . get ( 'x-forwarded-host' ) || request . headers . get ( 'host' )
233
+ // get the main domain from the env vars
234
+ const mainDomain = new URL ( process . env . NEXT_PUBLIC_URL ) . host
235
+ if ( domain !== mainDomain ) {
236
+ // get the subName from the domain mappings cache
237
+ const { subName } = await getDomainMappingsCache ( ) ?. [ domain ?. toLowerCase ( ) ]
238
+ if ( subName ) {
239
+ domainLogger ( ) . log ( 'detected allowed custom domain for: ' , subName )
240
+ // handle the custom domain
241
+ const customDomainResp = await customDomainMiddleware ( request , referrerResp , mainDomain , domain , subName )
242
+ return applySecurityHeaders ( customDomainResp )
243
+ }
236
244
}
237
245
238
246
return applySecurityHeaders ( referrerResp )
0 commit comments