fix: authentication check in path proxy (#5442)

`proxy` should `await` for result of `authenticated` call otherwise since otherwise it will always appear to be authenticated as the promise is truthy.

Co-authored-by: Asher <ash@coder.com>
This commit is contained in:
Rafael Calpena Rodrigues 2022-08-10 21:05:49 -03:00 committed by GitHub
parent c69f2c69f6
commit 2ec1e2de34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 7 deletions

View File

@ -94,8 +94,8 @@ export const register = async (app: App, args: DefaultedArgs): Promise<Disposabl
app.router.use("/", domainProxy.router) app.router.use("/", domainProxy.router)
app.wsRouter.use("/", domainProxy.wsRouter.router) app.wsRouter.use("/", domainProxy.wsRouter.router)
app.router.all("/proxy/(:port)(/*)?", (req, res) => { app.router.all("/proxy/(:port)(/*)?", async (req, res) => {
pathProxy.proxy(req, res) await pathProxy.proxy(req, res)
}) })
app.wsRouter.get("/proxy/(:port)(/*)?", async (req) => { app.wsRouter.get("/proxy/(:port)(/*)?", async (req) => {
await pathProxy.wsProxy(req as pluginapi.WebsocketRequest) await pathProxy.wsProxy(req as pluginapi.WebsocketRequest)
@ -103,8 +103,8 @@ export const register = async (app: App, args: DefaultedArgs): Promise<Disposabl
// These two routes pass through the path directly. // These two routes pass through the path directly.
// So the proxied app must be aware it is running // So the proxied app must be aware it is running
// under /absproxy/<someport>/ // under /absproxy/<someport>/
app.router.all("/absproxy/(:port)(/*)?", (req, res) => { app.router.all("/absproxy/(:port)(/*)?", async (req, res) => {
pathProxy.proxy(req, res, { await pathProxy.proxy(req, res, {
passthroughPath: true, passthroughPath: true,
}) })
}) })

View File

@ -14,14 +14,14 @@ const getProxyTarget = (req: Request, passthroughPath?: boolean): string => {
return `http://0.0.0.0:${req.params.port}/${req.params[0] || ""}${query ? `?${query}` : ""}` return `http://0.0.0.0:${req.params.port}/${req.params[0] || ""}${query ? `?${query}` : ""}`
} }
export function proxy( export async function proxy(
req: Request, req: Request,
res: Response, res: Response,
opts?: { opts?: {
passthroughPath?: boolean passthroughPath?: boolean
}, },
): void { ): Promise<void> {
if (!authenticated(req)) { if (!(await authenticated(req))) {
// If visiting the root (/:port only) redirect to the login page. // If visiting the root (/:port only) redirect to the login page.
if (!req.params[0] || req.params[0] === "/") { if (!req.params[0] || req.params[0] === "/") {
const to = self(req) const to = self(req)