diff --git a/playgrounds/elysia/README.md b/playgrounds/elysia/README.md new file mode 100644 index 0000000..b5d1413 --- /dev/null +++ b/playgrounds/elysia/README.md @@ -0,0 +1,27 @@ +# WebDecoy and Elysia + +This demo passes Bun's incoming requests through the WebDecoy Fetch guard +before Elysia handles them. The rules run in process and need no WebDecoy +account or API key. The server binds to loopback. + +## Run locally + +```sh +bun install --frozen-lockfile +bun run start +``` + +In a second terminal: + +```sh +curl -i http://127.0.0.1:3000/ +curl -i http://127.0.0.1:3000/demo-decoy +``` + +The ordinary route returns 200 and logs `ALLOW`. The demo path has no Elysia +route and returns Elysia's usual 404, while the guard logs `DENY` in monitor +mode. The decision changes the response only when enforcement is enabled. + +Run `bun test` to verify both modes against a loopback server on an ephemeral +port. The demo uses the socket peer IP and trusts zero forwarding hops. +Configure trusted proxies deliberately if deploying behind a reverse proxy. diff --git a/playgrounds/elysia/bun.lock b/playgrounds/elysia/bun.lock new file mode 100644 index 0000000..7ced123 --- /dev/null +++ b/playgrounds/elysia/bun.lock @@ -0,0 +1,50 @@ +{ + "lockfileVersion": 2, + "configVersion": 1, + "workspaces": { + "": { + "name": "webdecoy-elysia-playground", + "dependencies": { + "@webdecoy/node": "0.31.0", + "elysia": "1.4.32", + }, + }, + }, + "packages": { + "@borewit/text-codec": ["@borewit/text-codec@0.2.3", "", {}, "sha512-49CycTz1aAZmvFcwkeFyhud1HUmRIB5Tabf7ALV+VA4Iu85MTfz2cUwzPagMFAdVBvrWdkCxG67I30tUwwpKsw=="], + + "@sinclair/typebox": ["@sinclair/typebox@0.34.52", "", {}, "sha512-XiMQh7qqVlxZzcVD+kkGMNGMzcTrDMLWI7S4x7z1MkCkbDPrekpZXEUK0eZqZFMuHQg2a2DZOcDIh9o5v3Gonw=="], + + "@tokenizer/inflate": ["@tokenizer/inflate@0.6.1", "", { "dependencies": { "debug": "^4.4.3", "strtok3": "^10.8.0", "token-types": "^6.2.0" } }, "sha512-EuqSyHdOXN61cc5/rsW6uUn1TJZJGZr56jlkUbsLwVJ6uHgvwQyFkYfOCf+zzPFAYjftPk1WeJnEhpLVvzbm1A=="], + + "@tokenizer/token": ["@tokenizer/token@0.3.0", "", {}, "sha512-OvjF+z51L3ov0OyAU0duzsYuvO01PH7x4t6DJx+guahgTnBHkhJdG7soQeTSFLWN3efnHyibZ4Z8l2EuWwJN3A=="], + + "@webdecoy/node": ["@webdecoy/node@0.31.0", "", {}, "sha512-RzhrjFCBxmEWY5Q+aGb691M/3tajNaFDLTFmxwsPS7hNhaOAUBSIlS3RmGNknNACcS0vC2iROlrY4EnduCEE1Q=="], + + "cookie": ["cookie@1.1.1", "", {}, "sha512-ei8Aos7ja0weRpFzJnEA9UHJ/7XQmqglbRwnf2ATjcB9Wq874VKH9kfjjirM6UhU2/E5fFYadylyhFldcqSidQ=="], + + "debug": ["debug@4.4.3", "", { "dependencies": { "ms": "^2.1.3" }, "peerDependencies": { "supports-color": "*" }, "optionalPeers": ["supports-color"] }, "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA=="], + + "elysia": ["elysia@1.4.32", "", { "dependencies": { "cookie": "^1.1.1", "exact-mirror": "^0.2.9", "fast-decode-uri-component": "^1.0.1", "memoirist": "^0.4.0" }, "peerDependencies": { "@sinclair/typebox": ">= 0.34.0 < 1", "@types/bun": ">= 1.2.0", "file-type": ">= 20.0.0", "openapi-types": ">= 12.0.0", "typescript": ">= 5.0.0" }, "optionalPeers": ["@types/bun", "typescript"] }, "sha512-pSWBPKJe0t24r6oQydL8xmFnbOOVs0bXN1ShIwSPxXJbE/z9NRRXs8FigQRhXyjdbi3YSuJgEKe22BVx4cU99w=="], + + "exact-mirror": ["exact-mirror@0.2.9", "", { "peerDependencies": { "@sinclair/typebox": "^0.34.15" }, "optionalPeers": ["@sinclair/typebox"] }, "sha512-7+ecLdWdlY0Br1E1Fax2Pss5VU3b1WQwJ8bdyt5azPNdhKHuivjQlV8baXF9NkQwlvnyOwaM6ITBiuK43PQ4wg=="], + + "fast-decode-uri-component": ["fast-decode-uri-component@1.0.1", "", {}, "sha512-WKgKWg5eUxvRZGwW8FvfbaH7AXSh2cL+3j5fMGzUMCxWBJ3dV3a7Wz8y2f/uQ0e3B6WmodD3oS54jTQ9HVTIIg=="], + + "file-type": ["file-type@22.3.0", "", { "dependencies": { "@tokenizer/inflate": "^0.6.0", "strtok3": "^10.6.3", "token-types": "^6.1.2", "uint8array-extras": "^1.6.0" } }, "sha512-PrPedgnnWl5bWz3JHgar0QSzRvzeyFh7dZ9JpYOgcPZu4KALS9zw4MMa6qXRYyjeBsAlpaWB2boLuG991+0HIQ=="], + + "ieee754": ["ieee754@1.2.1", "", {}, "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA=="], + + "memoirist": ["memoirist@0.4.0", "", {}, "sha512-zxTgA0mSYELa66DimuNQDvyLq36AwDlTuVRbnQtB+VuTcKWm5Qc4z3WkSpgsFWHNhexqkIooqpv4hdcqrX5Nmg=="], + + "ms": ["ms@2.1.3", "", {}, "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA=="], + + "openapi-types": ["openapi-types@12.1.3", "", {}, "sha512-N4YtSYJqghVu4iek2ZUvcN/0aqH1kRDuNqzcycDxhOUpg7GdvLa2F3DgS6yBNhInhv2r/6I0Flkn7CqL8+nIcw=="], + + "strtok3": ["strtok3@10.8.0", "", { "dependencies": { "@tokenizer/token": "^0.3.0" } }, "sha512-e6bW3fD6V01DPmcbLfNtn0cjsiHvTKS/hivgbzqYMfHb/nLes4QQ0H5QBxlkPQayF4xv08AvlSgptGwN86Eemw=="], + + "token-types": ["token-types@6.2.0", "", { "dependencies": { "@borewit/text-codec": "^0.2.3", "@tokenizer/token": "^0.3.0", "ieee754": "^1.2.1" } }, "sha512-s9euYHM//T3loJtbNUYdbAzsMlvKBeRS+XCLFy+UGosMQH19Hwjap5k19i4PPAEJJBsVIpMpVTxl4fIqCuNfUg=="], + + "uint8array-extras": ["uint8array-extras@1.6.0", "", {}, "sha512-8iAasVS4wUx0gPLjH8Xtz2PeDzTSiy8QiLGu2DT3X5GU+egFEQhpnbtkehbAwjvDcxIERmCYcysiODFmE3Ud0Q=="], + } +} diff --git a/playgrounds/elysia/package.json b/playgrounds/elysia/package.json new file mode 100644 index 0000000..b9f5425 --- /dev/null +++ b/playgrounds/elysia/package.json @@ -0,0 +1,13 @@ +{ + "name": "webdecoy-elysia-playground", + "private": true, + "type": "module", + "scripts": { + "start": "bun server.ts", + "test": "bun test" + }, + "dependencies": { + "@webdecoy/node": "0.31.0", + "elysia": "1.4.32" + } +} diff --git a/playgrounds/elysia/server.test.ts b/playgrounds/elysia/server.test.ts new file mode 100644 index 0000000..a445d43 --- /dev/null +++ b/playgrounds/elysia/server.test.ts @@ -0,0 +1,22 @@ +import { expect, test } from 'bun:test'; +import { startDemo } from './server'; + +test.each([ + ['monitor', 404], + ['enforce', 403], +] as const)('%s keeps the ordinary route available', async (mode, decoyStatus) => { + const decisions: Array<[string, string]> = []; + const server = startDemo({ mode, port: 0, onDecision: (path, verdict) => decisions.push([path, verdict]) }); + try { + const ordinary = await fetch(server.url); + expect(ordinary.status).toBe(200); + expect(await ordinary.text()).toBe('Hello from Elysia'); + + const decoy = await fetch(new URL('/demo-decoy', server.url)); + expect(decoy.status).toBe(decoyStatus); + await decoy.text(); + expect(decisions).toEqual([['/', 'ALLOW'], ['/demo-decoy', 'DENY']]); + } finally { + await server.stop(true); + } +}); diff --git a/playgrounds/elysia/server.ts b/playgrounds/elysia/server.ts new file mode 100644 index 0000000..dedaba0 --- /dev/null +++ b/playgrounds/elysia/server.ts @@ -0,0 +1,32 @@ +import { createFetchGuard, tripwire } from '@webdecoy/node'; +import { Elysia } from 'elysia'; + +interface DemoOptions { + port?: number; + mode?: 'monitor' | 'enforce'; + onDecision?: (path: string, conclusion: string) => void; +} + +export function startDemo({ port = 3000, mode = 'monitor', onDecision }: DemoOptions = {}) { + const app = new Elysia().get('/', () => 'Hello from Elysia'); + const guard = createFetchGuard({ + mode, + trustProxy: 0, + rules: [tripwire({ paths: ['/demo-decoy'], includeDefaults: false })], + }); + + return Bun.serve({ + hostname: '127.0.0.1', + port, + async fetch(request, server) { + const { decision, response } = await guard.check(request, server.requestIP(request)?.address); + onDecision?.(new URL(request.url).pathname, decision.conclusion); + return response ?? app.handle(request); + }, + }); +} + +if (import.meta.main) { + const server = startDemo({ onDecision: (path, verdict) => console.log(verdict, path) }); + console.log(`Elysia demo listening at ${server.url}`); +}