Skip to content

Commit 06fa762

Browse files
feat(routing): add opt-in HTTPS redirects
1 parent c29fe76 commit 06fa762

38 files changed

Lines changed: 464 additions & 92 deletions

‎docs/guide/edge/redirects.md‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,21 @@ outline: deep
44

55
# Redirects
66

7-
Apps hosted on a Void platform automatically redirect HTTP requests to HTTPS with a permanent `308` redirect, preserving the path, query string, and request method. Local development continues to support HTTP.
7+
Enable HTTP-to-HTTPS redirects with `routing.alwaysUseHttps` in `void.config.ts`:
8+
9+
```ts
10+
import { defineConfig } from 'void/config';
11+
12+
export default defineConfig({
13+
routing: {
14+
alwaysUseHttps: true,
15+
},
16+
});
17+
```
18+
19+
This sends a permanent `308` redirect to the same hostname over HTTPS, preserving the path, query string, and request method. Local development continues to support HTTP, and certificate validation paths remain available over HTTP.
20+
21+
Set `false` to explicitly keep HTTP. Omit the setting to inherit your hosting platform's default, which is currently off. The option works on Void platforms and direct Cloudflare deployments of Void apps and static sites. Frameworks that own their Worker handler require their own HTTPS redirect configuration for direct Cloudflare deployments.
822

923
Define URL redirects in [`void.config.ts`](../../reference/config) using the `routing.redirects` field. Keys are source URL patterns, values are destination strings or objects with an explicit status.
1024

‎docs/reference/config.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,16 @@ This choice applies to subsequent `void deploy` runs. Atomic deployment sends tr
366366

367367
Routing and edge configuration for headers, redirects, rewrites, and caching.
368368

369+
#### `routing.alwaysUseHttps`
370+
371+
Redirect HTTP requests to HTTPS with a permanent `308` redirect. Set `true` to enable, `false` to explicitly keep HTTP, or omit to inherit the hosting platform default (currently off). Local development continues to use HTTP. See [HTTPS redirects](../guide/edge/redirects) for deployment support and details.
372+
373+
```ts
374+
routing: {
375+
alwaysUseHttps: true,
376+
}
377+
```
378+
369379
#### `routing.headers`
370380

371381
Custom response headers. Keys are URL patterns, values are arrays of `"Name: value"` strings. See [Custom Headers](../guide/edge/headers) for details.

‎packages/deploy-cloudflare/src/static.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ export type CloudflareStaticDeployConfig = Readonly<{
2424
/** Generate the deterministic thin Worker entry bundled by Wrangler. */
2525
export function renderCloudflareStaticEntry(options: {
2626
runtimeImport?: string;
27+
alwaysUseHttps?: boolean;
2728
headerRules?: ReadonlyArray<HeaderRule>;
2829
redirectRules?: ReadonlyArray<RedirectRule>;
2930
fallbackRules?: ReadonlyArray<RedirectRule>;
3031
versionMetadataBinding?: string;
3132
}): string {
3233
const config = JSON.stringify({
34+
...(options.alwaysUseHttps !== undefined ? { alwaysUseHttps: options.alwaysUseHttps } : {}),
3335
...(options.headerRules?.length ? { headerRules: options.headerRules } : {}),
3436
...(options.redirectRules?.length ? { redirectRules: options.redirectRules } : {}),
3537
...(options.fallbackRules?.length ? { fallbackRules: options.fallbackRules } : {}),

‎packages/deploy-cloudflare/test/unit/static.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ import {
1010
} from '../../src/index.ts';
1111

1212
describe('Cloudflare static deployment', () => {
13+
it.each([true, false, undefined])(
14+
'preserves alwaysUseHttps %s in the generated entry',
15+
(alwaysUseHttps) => {
16+
const entry = renderCloudflareStaticEntry({ alwaysUseHttps });
17+
if (alwaysUseHttps === undefined) {
18+
expect(entry).not.toContain('alwaysUseHttps');
19+
} else {
20+
expect(entry).toContain(`"alwaysUseHttps":${alwaysUseHttps}`);
21+
}
22+
},
23+
);
1324
it('renders one deterministic shared-runtime entry', () => {
1425
expect(
1526
renderCloudflareStaticEntry({

‎packages/deploy-core/src/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ export type DeployManifest = {
278278
hashedAssetsPrefix?: string;
279279
headerRules?: Array<HeaderRule>;
280280
redirectRules?: Array<RedirectRule>;
281+
/** Omitted values inherit the platform default; preserve explicit false. */
282+
alwaysUseHttps?: boolean;
281283
fallbackRules?: Array<RedirectRule>;
282284
websocket?: WebSocketManifestMeta;
283285
sandbox?: SandboxManifestMeta;

‎packages/deploy-core/src/validate.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,10 @@ export function validateManifestShape(manifest: unknown): string | null {
376376
}
377377
}
378378

379+
if (manifest.alwaysUseHttps !== undefined && typeof manifest.alwaysUseHttps !== 'boolean') {
380+
return 'invalid manifest shape: alwaysUseHttps must be a boolean';
381+
}
382+
379383
const redirectRulesError = validateRoutingRules(manifest.redirectRules, 'redirectRules');
380384
if (redirectRulesError) {
381385
return redirectRulesError;

‎packages/deploy-core/test/unit/validate.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
import { describe, expect, it } from 'vite-plus/test';
22
import { validateManifestShape, type DeployManifest } from '../../src/index.ts';
33

4+
describe('alwaysUseHttps', () => {
5+
it.each([true, false, undefined])('accepts %s', (alwaysUseHttps) => {
6+
expect(validateManifestShape({ version: 3, bindings: {}, alwaysUseHttps })).toBeNull();
7+
});
8+
it.each(['true', 1, null, {}])('rejects non-boolean %s', (alwaysUseHttps) => {
9+
expect(validateManifestShape({ version: 3, bindings: {}, alwaysUseHttps })).toContain(
10+
'alwaysUseHttps must be a boolean',
11+
);
12+
});
13+
});
14+
415
describe('custom Durable Object declarations', () => {
516
const binding = {
617
bindingName: 'WORKSPACE',

‎packages/edge/src/https.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/** Upgrade public HTTP requests without involving assets or application code. */
2+
export function redirectToHttps(request: Request): Response | undefined {
3+
const url = new URL(request.url);
4+
if (
5+
url.protocol !== 'http:' ||
6+
url.hostname === 'localhost' ||
7+
url.hostname.endsWith('.localhost') ||
8+
/^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(url.hostname) ||
9+
url.hostname === '[::1]' ||
10+
url.pathname.startsWith('/.well-known/acme-challenge/') ||
11+
url.pathname.startsWith('/.well-known/pki-validation/')
12+
) {
13+
return undefined;
14+
}
15+
url.protocol = 'https:';
16+
// Cloudflare's HTTP ports do not accept TLS. Upgrade to the default HTTPS port.
17+
url.port = '';
18+
return Response.redirect(url.href, 308);
19+
}

‎packages/edge/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export type { ProjectZeroTrustRouting } from './access.ts';
2+
export { redirectToHttps } from './https.ts';
23
export {
34
applyHeaderRules,
45
parseClearSiteData,

‎packages/edge/src/runtime.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { AssetConfig, HeaderRule, RedirectRule } from '@void/deploy-core';
22
import { applyHeaderRules } from './headers.ts';
3+
import { redirectToHttps } from './https.ts';
34
import { applyRoutingRules, mergeQueryIntoLocation } from './routing-rules.ts';
45

56
export type EdgeAssetFetcher = Readonly<{
@@ -11,6 +12,7 @@ export type StaticEdgeEnv = Readonly<{
1112
}>;
1213

1314
export type StaticEdgeConfig = Readonly<{
15+
alwaysUseHttps?: boolean;
1416
/** Optional native Worker self-metadata used to identify the served version. */
1517
versionMetadataBinding?: string;
1618
/**
@@ -50,6 +52,12 @@ export function createStaticEdgeHandler(config: StaticEdgeConfig): StaticEdgeHan
5052
{ headers: { 'cache-control': 'no-store' } },
5153
);
5254
}
55+
if (config.alwaysUseHttps) {
56+
const redirect = redirectToHttps(request);
57+
if (redirect) {
58+
return redirect;
59+
}
60+
}
5361
let effectiveUrl = new URL(originalUrl);
5462
let effectiveRequest = request;
5563

0 commit comments

Comments
 (0)