From 870cdb25e9036f05c99277c5f2d5df8ca516db2b Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Fri, 30 Oct 2026 08:53:52 -0500 Subject: [PATCH] fix(adapters): reject createApp({ tls }) on Deno and Bun Only Node's listen() reads tls. serveDeno and serveBun serve through the runtime's own listener, so an app created with tls came up as plain HTTP without warning, for a caller who had asked for HTTPS. createApp now marks the app with a symbol, following the MARK_EDGE pattern. Both adapters throw on that mark before they boot or bind. Signed-off-by: Diego Resendez --- src/app/index.ts | 5 ++++- src/security.ts | 17 +++++++++++++++++ test/adapter-tls.test.ts | 30 ++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 test/adapter-tls.test.ts diff --git a/src/app/index.ts b/src/app/index.ts index 92ef455..e101189 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -45,7 +45,7 @@ import { getArgs, getHandlerNeeds, resolveArgs } from '../params'; import { buildOpenApi, type OpenApiInfo } from '../openapi'; import { connectLink, isPermanentRefusal, type Link } from '../mesh/link'; import { Rooms } from '../rooms'; -import type { TlsOptions, SecurityOptions, CorsOptions } from '../security'; +import { TLS_REQUESTED, type TlsOptions, type SecurityOptions, type CorsOptions } from '../security'; import { buildRemote, type RemoteRoute as TeacupRemoteRoute } from '../mesh/teacup'; import { buildManifest, @@ -533,6 +533,9 @@ export function createApp(opts: AppOptions): App { // `edgeHandler` calls this: on the edge core never encodes, whatever detection concluded. [MARK_EDGE]: edgeMarker(compression), + + // `serveDeno` and `serveBun` refuse an app that asked for TLS only Node's listener can give. + [TLS_REQUESTED]: Boolean(opts.tls), }); } diff --git a/src/security.ts b/src/security.ts index f36e809..45d866e 100644 --- a/src/security.ts +++ b/src/security.ts @@ -10,6 +10,23 @@ export interface TlsOptions { passphrase?: string; } +/** + * Set on an app created with `tls`, so the adapters that serve through their runtime's own listener + * (`serveDeno`, `serveBun`) can refuse it. Only Node's `listen()` reads `tls`; anywhere else it would + * serve plain HTTP to someone who asked for HTTPS. Symbol-keyed because it is adapter plumbing. + */ +export const TLS_REQUESTED: unique symbol = Symbol.for('green-tea.tls-requested'); + +/** Throws when `app` was created with `tls`, which `adapter` cannot honour. */ +export function refuseTls(app: object, adapter: string): void { + if ((app as { [TLS_REQUESTED]?: boolean })[TLS_REQUESTED]) { + throw new Error( + `${adapter} does not support createApp({ tls }); it would serve plain HTTP. ` + + `Configure TLS on the runtime's own server or terminate it at a proxy, and drop \`tls\`.`, + ); + } +} + /** CORS policy. `origins` may be a literal, list, `'*'`, or a predicate. */ export interface CorsOptions { origins: string | string[] | '*' | ((origin: string) => boolean); diff --git a/test/adapter-tls.test.ts b/test/adapter-tls.test.ts new file mode 100644 index 0000000..c3e288e --- /dev/null +++ b/test/adapter-tls.test.ts @@ -0,0 +1,30 @@ +import { afterEach, expect, it, vi } from 'vitest'; +import { createApp } from '../src'; +import { serveDeno } from '../src/deno'; +import { serveBun } from '../src/bun'; + +afterEach(() => { + vi.unstubAllGlobals(); +}); + +// Only Node's `listen()` reads `tls`. Deno and Bun serve through their own listener, so an app +// created with `tls` would come up as plain HTTP there — refused before anything binds. +it.each([ + ['serveDeno', 'Deno', serveDeno], + ['serveBun', 'Bun', serveBun], +] as const)('%s refuses an app created with tls', async (name, global, serve) => { + const listen = vi.fn(); + vi.stubGlobal(global, { serve: listen }); + const app = createApp({ modules: [], tls: { key: 'k', cert: 'c' } }); + + await expect(serve(app)).rejects.toThrow(`${name} does not support createApp({ tls })`); + expect(listen).not.toHaveBeenCalled(); +}); + +it('an app without tls is not refused', async () => { + const listen = vi.fn(() => ({ port: 0, stop: vi.fn() })); + vi.stubGlobal('Bun', { serve: listen }); + + await serveBun(createApp({ modules: [] })); + expect(listen).toHaveBeenCalledOnce(); +});