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.wsRouter.use("/", domainProxy.wsRouter.router)
app.router.all("/proxy/(:port)(/*)?", (req, res) => {
pathProxy.proxy(req, res)
app.router.all("/proxy/(:port)(/*)?", async (req, res) => {
await pathProxy.proxy(req, res)
})
app.wsRouter.get("/proxy/(:port)(/*)?", async (req) => {
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.
// So the proxied app must be aware it is running
// under /absproxy/<someport>/
app.router.all("/absproxy/(:port)(/*)?", (req, res) => {
pathProxy.proxy(req, res, {
app.router.all("/absproxy/(:port)(/*)?", async (req, res) => {
await pathProxy.proxy(req, res, {
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}` : ""}`
}
export function proxy(
export async function proxy(
req: Request,
res: Response,
opts?: {
passthroughPath?: boolean
},
): void {
if (!authenticated(req)) {
): Promise<void> {
if (!(await authenticated(req))) {
// If visiting the root (/:port only) redirect to the login page.
if (!req.params[0] || req.params[0] === "/") {
const to = self(req)