Skip to content

Commit 886ed21

Browse files
committed
wip: try migrating to argon2
1 parent d1d2530 commit 886ed21

File tree

9 files changed

+31
-25
lines changed

9 files changed

+31
-25
lines changed

src/node/http.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ export const replaceTemplates = <T extends object>(
4545
/**
4646
* Throw an error if not authorized. Call `next` if provided.
4747
*/
48-
export const ensureAuthenticated = (req: express.Request, _?: express.Response, next?: express.NextFunction): void => {
49-
if (!authenticated(req)) {
48+
export const ensureAuthenticated = async(req: express.Request, _?: express.Response, next?: express.NextFunction): Promise<void> => {
49+
if (await !authenticated(req)) {
5050
throw new HttpError("Unauthorized", HttpCode.Unauthorized)
5151
}
5252
if (next) {

src/node/routes/domainProxy.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ const maybeProxy = (req: Request): string | undefined => {
3232
return port
3333
}
3434

35-
router.all("*", (req, res, next) => {
35+
router.all("*", async (req, res, next) => {
3636
const port = maybeProxy(req)
3737
if (!port) {
3838
return next()
3939
}
4040

4141
// Must be authenticated to use the proxy.
42-
if (!authenticated(req)) {
42+
if (await !authenticated(req)) {
4343
// Let the assets through since they're used on the login page.
4444
if (req.path.startsWith("/static/") && req.method === "GET") {
4545
return next()
@@ -73,14 +73,14 @@ router.all("*", (req, res, next) => {
7373

7474
export const wsRouter = WsRouter()
7575

76-
wsRouter.ws("*", (req, _, next) => {
76+
wsRouter.ws("*", async (req, _, next) => {
7777
const port = maybeProxy(req)
7878
if (!port) {
7979
return next()
8080
}
8181

8282
// Must be authenticated to use the proxy.
83-
ensureAuthenticated(req)
83+
await ensureAuthenticated(req)
8484

8585
proxy.ws(req, req.ws, req.head, {
8686
ignorePath: true,

src/node/routes/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ export const register = async (
104104
// These two routes pass through the path directly.
105105
// So the proxied app must be aware it is running
106106
// under /absproxy/<someport>/
107-
app.all("/absproxy/(:port)(/*)?", (req, res) => {
108-
pathProxy.proxy(req, res, {
107+
app.all("/absproxy/(:port)(/*)?", async (req, res) => {
108+
await pathProxy.proxy(req, res, {
109109
passthroughPath: true,
110110
})
111111
})
112-
wsApp.get("/absproxy/(:port)(/*)?", (req) => {
113-
pathProxy.wsProxy(req as pluginapi.WebsocketRequest, {
112+
wsApp.get("/absproxy/(:port)(/*)?", async (req) => {
113+
await pathProxy.wsProxy(req as pluginapi.WebsocketRequest, {
114114
passthroughPath: true,
115115
})
116116
})
@@ -120,7 +120,7 @@ export const register = async (
120120
const pluginApi = new PluginAPI(logger, process.env.CS_PLUGIN, process.env.CS_PLUGIN_PATH, workingDir)
121121
await pluginApi.loadPlugins()
122122
pluginApi.mount(app, wsApp)
123-
app.use("/api/applications", ensureAuthenticated, apps.router(pluginApi))
123+
app.use("/api/applications", await ensureAuthenticated, apps.router(pluginApi))
124124
wrapper.onDispose(() => pluginApi.dispose())
125125
}
126126

src/node/routes/login.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ const limiter = new RateLimiter()
4949

5050
export const router = Router()
5151

52-
router.use((req, res, next) => {
52+
router.use(async(req, res, next) => {
5353
const to = (typeof req.query.to === "string" && req.query.to) || "/"
54-
if (authenticated(req)) {
54+
if (await authenticated(req)) {
5555
return redirect(req, res, to, { to: undefined })
5656
}
5757
next()

src/node/routes/pathProxy.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ const getProxyTarget = (req: Request, passthroughPath?: boolean): string => {
1515
return `http://0.0.0.0:${req.params.port}/${req.params[0] || ""}${query ? `?${query}` : ""}`
1616
}
1717

18-
export function proxy(
18+
export async function proxy(
1919
req: Request,
2020
res: Response,
2121
opts?: {
2222
passthroughPath?: boolean
2323
},
24-
): void {
25-
if (!authenticated(req)) {
24+
): Promise<void> {
25+
if (await !authenticated(req)) {
2626
// If visiting the root (/:port only) redirect to the login page.
2727
if (!req.params[0] || req.params[0] === "/") {
2828
const to = normalize(`${req.baseUrl}${req.path}`)
@@ -45,13 +45,13 @@ export function proxy(
4545
})
4646
}
4747

48-
export function wsProxy(
48+
export async function wsProxy(
4949
req: pluginapi.WebsocketRequest,
5050
opts?: {
5151
passthroughPath?: boolean
5252
},
53-
): void {
54-
ensureAuthenticated(req)
53+
): Promise<void> {
54+
await ensureAuthenticated(req)
5555
_proxy.ws(req, req.ws, req.head, {
5656
ignorePath: true,
5757
target: getProxyTarget(req, opts?.passthroughPath),

src/node/routes/static.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ router.get("/(:commit)(/*)?", async (req, res) => {
1818
// Used by VS Code to load extensions into the web worker.
1919
const tar = getFirstString(req.query.tar)
2020
if (tar) {
21-
ensureAuthenticated(req)
21+
await ensureAuthenticated(req)
2222
let stream: Readable = tarFs.pack(pathToFsPath(tar))
2323
if (req.headers["accept-encoding"] && req.headers["accept-encoding"].includes("gzip")) {
2424
logger.debug("gzipping tar", field("path", tar))
@@ -43,7 +43,7 @@ router.get("/(:commit)(/*)?", async (req, res) => {
4343

4444
// Make sure it's in code-server if you aren't authenticated. This lets
4545
// unauthenticated users load the login assets.
46-
if (!resourcePath.startsWith(rootPath) && !authenticated(req)) {
46+
if (!resourcePath.startsWith(rootPath) && await !authenticated(req)) {
4747
throw new HttpError("Unauthorized", HttpCode.Unauthorized)
4848
}
4949

src/node/routes/update.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const router = Router()
77

88
const provider = new UpdateProvider()
99

10+
// TODO see if this breaks because we can't await ensureAuthenticated
1011
router.get("/check", ensureAuthenticated, async (req, res) => {
1112
const update = await provider.getUpdate(req.query.force === "true")
1213
res.json({

src/node/routes/vscode.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const router = Router()
1919
const vscode = new VscodeProvider()
2020

2121
router.get("/", async (req, res) => {
22-
if (!authenticated(req)) {
22+
if (await !authenticated(req)) {
2323
return redirect(req, res, "login", {
2424
// req.baseUrl can be blank if already at the root.
2525
to: req.baseUrl && req.baseUrl !== "/" ? req.baseUrl : undefined,
@@ -61,6 +61,7 @@ router.get("/", async (req, res) => {
6161

6262
/**
6363
* TODO: Might currently be unused.
64+
* TODO@jsjoeio might break because ensureAuthenticated is async
6465
*/
6566
router.get("/resource(/*)?", ensureAuthenticated, async (req, res) => {
6667
if (typeof req.query.path === "string") {
@@ -71,6 +72,7 @@ router.get("/resource(/*)?", ensureAuthenticated, async (req, res) => {
7172

7273
/**
7374
* Used by VS Code to load files.
75+
* TODO@jsjoeio might break because ensureAuthenticated is async
7476
*/
7577
router.get("/vscode-remote-resource(/*)?", ensureAuthenticated, async (req, res) => {
7678
if (typeof req.query.path === "string") {
@@ -82,6 +84,7 @@ router.get("/vscode-remote-resource(/*)?", ensureAuthenticated, async (req, res)
8284
/**
8385
* VS Code webviews use these paths to load files and to load webview assets
8486
* like HTML and JavaScript.
87+
* TODO@jsjoeio might break because ensureAuthenticated is async
8588
*/
8689
router.get("/webview/*", ensureAuthenticated, async (req, res) => {
8790
res.set("Content-Type", getMediaMime(req.path))
@@ -128,6 +131,7 @@ const fetchTimeout = 5 * 60 * 1000
128131
// The callback endpoints are used during authentication. A URI is stored on
129132
// /callback and then fetched later on /fetch-callback.
130133
// See ../../../lib/vscode/resources/web/code-web.js
134+
// TODO@jsjoeio might break because ensureAuthenticated is async
131135
router.get("/callback", ensureAuthenticated, async (req, res) => {
132136
const uriKeys = [
133137
"vscode-requestId",
@@ -167,6 +171,7 @@ router.get("/callback", ensureAuthenticated, async (req, res) => {
167171
res.sendFile(path.join(rootPath, "lib/vscode/resources/web/callback.html"))
168172
})
169173

174+
// TODO@jsjoeio might break becasue ensureAuthenticated is async
170175
router.get("/fetch-callback", ensureAuthenticated, async (req, res) => {
171176
const id = getRequestId(req)
172177

@@ -195,7 +200,7 @@ router.get("/fetch-callback", ensureAuthenticated, async (req, res) => {
195200
})
196201

197202
export const wsRouter = WsRouter()
198-
203+
// TODO@jsjoeio might break becasue ensureAuthenticated is async
199204
wsRouter.ws("/", ensureAuthenticated, async (req) => {
200205
const magic = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
201206
const reply = crypto

typings/pluginapi.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,12 @@ export const proxy: ProxyServer
145145
/**
146146
* Middleware to ensure the user is authenticated. Throws if they are not.
147147
*/
148-
export function ensureAuthenticated(req: express.Request, res?: express.Response, next?: express.NextFunction): void
148+
export function ensureAuthenticated(req: express.Request, res?: express.Response, next?: express.NextFunction): Promise<void>
149149

150150
/**
151151
* Returns true if the user is authenticated.
152152
*/
153-
export function authenticated(req: express.Request): boolean
153+
export function authenticated(req: express.Request): Promise<boolean>
154154

155155
/**
156156
* Replace variables in HTML: TO, BASE, CS_STATIC_BASE, and OPTIONS.

0 commit comments

Comments
 (0)