fix: faulty roots when reverse-proxy adds a prefix
Massimo Melina committed
May 11, 2024 at 13:44 UTC
eb557e8f9e26920d11ec22f309c8f7cfb8551c17
2 files changed
+7
-13
src/apiMiddleware.ts
-11
@@ -36,19 +36,8 @@ export function apiMiddleware(apis: ApiHandlers) : Koa.Middleware {
36
// we don't rely on SameSite cookie option because it's https-only
37
let res
38
try {
39
- if (ctx.state.revProxyPath)
40
- for (const [k,v] of Object.entries(params))
41
- if (k.startsWith('uri'))
42
- if (typeof v === 'string')
43
- fixUri(params, k)
44
- else if (typeof (v as any)?.[0] === 'string')
45
- (v as string[]).forEach((x,i) => fixUri(v,i))
39
res = await apiFun(params, ctx)
40
if (res === null) return
48
-
49
- function fixUri(obj: any, k: string | number) {
50
- obj[k] = removeStarting(ctx.state.revProxyPath, obj[k])
51
- }
41
}
42
catch(e) {
43
if (typeof e === 'string') // message meant to be transmitted
src/roots.ts
+7
-2
@@ -1,5 +1,5 @@
1
import { defineConfig } from './config'
2
-import { ADMIN_URI, API_URI, CFG, isLocalHost, makeMatcher, SPECIAL_URI } from './misc'
2
+import { ADMIN_URI, API_URI, CFG, isLocalHost, makeMatcher, removeStarting, SPECIAL_URI } from './misc'
3
import Koa from 'koa'
4
import { disconnect } from './connections'
5
import _ from 'lodash'
@@ -37,12 +37,17 @@ export const rootsMiddleware: Koa.Middleware = (ctx, next) =>
37
return true // true will avoid calling next
38
}
39
if (!params) {
40
- ctx.path = join(root, ctx.path)
40
+ ctx.path = join(root, ctx.path, '')
41
return
42
}
43
for (const [k,v] of Object.entries(params))
44
if (k.startsWith('uri'))
45
params[k] = Array.isArray(v) ? v.map(x => join(root, x)) : join(root, v)
46
+
47
+ function join(a: string, b: any, removePrefix=ctx.state.revProxyPath) {
48
+ return a + (b && b[0] !== '/' ? '/' : '')
49
+ + removeStarting(removePrefix, b) // removal must be done before adding the root
50
+ }
51
})() || next()
52
53
function join(a: string, b: any) {