Skip to content

Commit fa97470

Browse files
committed
chore: tweaks
1 parent f81d3ff commit fa97470

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

docs/dev/deploy.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ If `SENTRY_AUTH_TOKEN` is missing, builds still pass, but Sentry releases and so
151151

152152
### HTTPS Enforcement
153153

154-
Enforce HTTPS at Cloudflare instead of in the Worker.
154+
Keep Cloudflare's blanket HTTP-to-HTTPS redirect off so bare `curl curl.md/<url>` requests reach the Worker. The Worker serves unauthenticated URL-fetch paths over HTTP and redirects all other HTTP routes to HTTPS.
155155

156156
1. Go to [Edge Certificates](https://dash.cloudflare.com/?to=/:account/:zone/ssl-tls/edge-certificates) for the `curl.md` zone.
157-
2. Enable [Always Use HTTPS](https://developers.cloudflare.com/ssl/edge-certificates/additional-options/always-use-https/).
157+
2. Disable [Always Use HTTPS](https://developers.cloudflare.com/ssl/edge-certificates/additional-options/always-use-https/).
158158
3. Enable [HTTP Strict Transport Security (HSTS)](https://developers.cloudflare.com/ssl/edge-certificates/additional-options/http-strict-transport-security/).
159159
4. Use these HSTS settings.
160160
- **Max Age Header**: `6 months`

src/entry-server.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ export default Sentry.withSentry<Env, QueueHandlerMessage>(
3131
{
3232
async fetch(request, env, ctx) {
3333
const url = new URL(request.url)
34+
const firstSegment = url.pathname.split('/')[1] ?? ''
35+
36+
// Keep unauthenticated curl fetch paths working over HTTP, but enforce HTTPS elsewhere.
37+
if (url.protocol === 'http:' && url.hostname === env.HOST) {
38+
const isFetchPath = firstSegment.includes('.') || /^https?:$/.test(firstSegment)
39+
if (!isFetchPath) {
40+
url.protocol = 'https:'
41+
return new Response(null, { status: 301, headers: { location: url.toString() } })
42+
}
43+
if (
44+
request.headers.has('authorization') ||
45+
request.headers.has('cookie') ||
46+
url.searchParams.has('t') ||
47+
url.searchParams.has('token')
48+
)
49+
return new Response('Use HTTPS for authenticated requests', { status: 400 })
50+
}
3451

3552
// Route API requests to the Hono API handler
3653
if (url.pathname.startsWith('/api/')) return api.fetch(new Request(url, request), env, ctx)
@@ -66,7 +83,6 @@ export default Sentry.withSentry<Env, QueueHandlerMessage>(
6683
}
6784

6885
// Route dot-segment paths (e.g. curl.md/example.com) to the API handler under /api prefix
69-
const firstSegment = url.pathname.split('/')[1] ?? ''
7086
if (firstSegment.includes('.') || /^https?:$/.test(firstSegment)) {
7187
const protocolMatch = url.pathname.match(/^\/(https?:\/\/)(.+)/)
7288
if (protocolMatch) {

0 commit comments

Comments
 (0)