Skip to content

Commit 0c6cdc8

Browse files
fix: web router
1 parent 15b0209 commit 0c6cdc8

5 files changed

Lines changed: 20 additions & 13 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@okkema/worker",
3-
"version": "4.7.4",
3+
"version": "4.7.5",
44
"description": "Cloudflare Workers Toolkit",
55
"files": [
66
"dist",

src/auth/jwk.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ export const JWK = {
2828
})
2929
return resp.json<{ keys: JsonWebKey[] }>()
3030
},
31-
url(issuer): string {
31+
url(issuer: string): string {
3232
const url = new URL(issuer)
33+
if (url.protocol !== "https:") {
34+
throw new Problem({
35+
title: "JWK Error",
36+
detail: "JWK issuer must use HTTPS",
37+
})
38+
}
3339
if (!url.pathname.endsWith("/")) url.pathname += "/"
3440
url.pathname += ".well-known/jwks.json"
3541
return url.href

src/auth/jwt.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,14 @@ export const JWT = {
186186
detail: "Missing 'Authorization' header.",
187187
status: 401,
188188
})
189-
let type, token
190-
try {
191-
;[type, token] = header.split(" ")
192-
} catch {
189+
const parts = header.split(" ", 2)
190+
if (parts.length !== 2)
193191
throw new Problem({
194192
title: "JWT Error",
195193
detail: "Unable to parse 'Authorization' header.",
196194
status: 401,
197195
})
198-
}
196+
const [type, token] = parts
199197
if (type !== "Bearer")
200198
throw new Problem({
201199
title: "JWT Error",

src/web/router.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import type { APIContext, MiddlewareNext, MiddlewareHandler } from "astro"
22
import { sequence } from "astro:middleware"
33

4+
const NORMALIZE = /\/+(\/|$)/g // strip double & trailing slash
5+
46
export function router(routes: Record<string, MiddlewareHandler>) {
57
const entries = Object.entries(routes)
68
return function (context: APIContext, next: MiddlewareNext) {
79
return sequence(
810
...entries.filter(function ([path]) {
9-
return context.url.pathname.match(RegExp(`^${(path
10-
.replace(/\/+(\/|$)/g, '$1')) // strip double & trailing splash
11-
.replace(/(\/?)\*/g, '($1.*)?') // wildcard
12-
}/*$`))
11+
return context.url.pathname.replace(NORMALIZE, '$1')
12+
.match(RegExp(`^${(path
13+
.replace(NORMALIZE, '$1'))
14+
.replace(/(\/?)\*/g, '($1.*)?') // wildcard
15+
}/*$`))
1316
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1417
}).map(([_, handler]) => handler)
1518
)(context, next)

0 commit comments

Comments
 (0)