fix: faulty account-redirect when used with reverse-proxy and a prefixed url #589

Massimo Melina committed May 12, 2024 at 17:26 UTC 8fa9fb884ec0aa0153b0ae3fbeafb1790f4b1dc8
3 files changed +16 -12
frontend/src/App.ts
+2 -2
@@ -9,7 +9,7 @@ import { useSnapState } from './state'
9 import { I18Nprovider } from './i18n'
10 import { proxy, useSnapshot } from "valtio"
11 import { Spinner } from "./components"
12 -import { getHFS, getPrefixUrl } from '@hfs/shared'
12 +import { enforceStarting, getHFS, getPrefixUrl } from '@hfs/shared'
13 import { Toasts } from './toasts'
14
15 function App() {
@@ -36,7 +36,7 @@ function App() {
36
37 function NavigationExtractor(props: any) {
38 const go = useNavigate() // expose navigate function for programmatic usage
39 - getHFS().navigate = (uri: string) => go(getPrefixUrl() + uri)
39 + getHFS().navigate = (uri: string) => go(getPrefixUrl() + enforceStarting('/', uri))
40 return h(Fragment, props)
41 }
42
src/cross.ts
+13 -1
@@ -115,6 +115,14 @@ export function prefix(pre: Falsy | string, v: string | number | undefined | nul
115 return v ? (pre||'') + v + (post || '') : ''
116 }
117
118 +export function join(a: string, b: string, joiner='/') { // similar to path.join but OS independent
119 + if (!b) return a
120 + if (!a) return b
121 + const ends = a.at(-1) === joiner
122 + const starts = b[0] === joiner
123 + return a + (!ends && !starts ? joiner + b : ends && starts ? b.slice(1) : b)
124 +}
125 +
126 export function wait<T=undefined>(ms: number, val?: T): Promise<T | undefined> {
127 return new Promise(res=> setTimeout(res,ms,val))
128 }
@@ -128,7 +136,11 @@ export function objSameKeys<S extends object,VR=any>(src: S, newValue:(value:Tru
136 }
137
138 export function enforceFinal(sub:string, s:string, evenEmpty=false) {
131 - return !evenEmpty && !s || s.endsWith(sub) ? s : s+sub
139 + return evenEmpty && !s || !s.endsWith(sub) ? s + sub : s
140 +}
141 +
142 +export function enforceStarting(sub:string, s:string, evenEmpty=false) {
143 + return evenEmpty && !s || !s.startsWith(sub) ? sub + s : s
144 }
145
146 export function removeStarting(sub: string, s: string) {
src/roots.ts
+1 -9
@@ -1,5 +1,5 @@
1 import { defineConfig } from './config'
2 -import { ADMIN_URI, API_URI, Callback, CFG, isLocalHost, makeMatcher, removeStarting, SPECIAL_URI } from './misc'
2 +import { ADMIN_URI, API_URI, Callback, CFG, isLocalHost, join, makeMatcher, removeStarting, SPECIAL_URI } from './misc'
3 import Koa from 'koa'
4 import { disconnect } from './connections'
5 import _ from 'lodash'
@@ -48,11 +48,3 @@ export const rootsMiddleware: Koa.Middleware = (ctx, next) =>
48 params[k] = Array.isArray(v) ? v.map(cb) : cb(v)
49 }
50 })() || next()
51 -
52 -function join(a: string, b: string, joiner='/') { // similar to path.join but OS independent
53 - if (!b) return a
54 - if (!a) return b
55 - const ends = a.at(-1) === joiner
56 - const starts = b[0] === joiner
57 - return a + (!ends && !starts ? joiner + b : ends && starts ? b.slice(1) : b)
58 -}