Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

fix(api): allow the local Vite server when the Worker runs on localhost

spacesdrive41f0db76a673
diff --git a/backend/lib/config.ts b/backend/lib/config.ts
index 07781aa..a77d4f9 100644
--- a/backend/lib/config.ts
+++ b/backend/lib/config.ts
@@ -3,6 +3,7 @@ import type { Env } from '../types/env.ts';
const DEFAULT_BACKEND_URL = 'http://localhost:8787';
const DEFAULT_FRONTEND_URL = 'http://localhost:5173';
const DEV_ORIGINS = ['http://localhost:5173', 'http://127.0.0.1:5173'];
+const LOCAL_HOSTNAMES = ['localhost', '127.0.0.1'];
const CALLBACK_PATH = '/api/auth/instagram/callback';
export interface CloudinaryConfig {
@@ -43,14 +44,20 @@ function originOf(url: string): string | null {
}
}
-// FRONTEND_URL and any ALLOWED_ORIGINS; the local Vite server only when neither is set
+// FRONTEND_URL and any ALLOWED_ORIGINS, by origin
function allowedOrigins(env: Env): string[] {
const listed = [env.FRONTEND_URL, ...(env.ALLOWED_ORIGINS ?? '').split(',')]
.map(value => cleanSecret(value))
.filter((value): value is string => !!value)
.map(originOf)
.filter((origin): origin is string => origin !== null);
- return listed.length > 0 ? [...new Set(listed)] : DEV_ORIGINS;
+ return [...new Set(listed)];
+}
+
+// a Worker served from localhost is a development instance, so the local Vite server may call it too
+export function corsOrigins(config: AppConfig, requestUrl: string): string[] {
+ const local = LOCAL_HOSTNAMES.includes(new URL(requestUrl).hostname);
+ return local ? [...config.allowedOrigins, ...DEV_ORIGINS] : config.allowedOrigins;
}
export function readInstagramOAuthConfig(env: Env): InstagramOAuthConfig {
diff --git a/backend/middleware/cors.ts b/backend/middleware/cors.ts
index d704f2c..58d5eca 100644
--- a/backend/middleware/cors.ts
+++ b/backend/middleware/cors.ts
@@ -1,10 +1,10 @@
import { cors } from 'hono/cors';
-import { readConfig } from '../lib/config.ts';
+import { corsOrigins, readConfig } from '../lib/config.ts';
import { RATE_LIMIT_HEADERS } from './rateLimit.ts';
// only the app's own origins may call the API from a browser; a stolen token is useless from another site
export const apiCors = cors({
- origin: (origin, c) => (readConfig(c.env).allowedOrigins.includes(origin) ? origin : null),
+ origin: (origin, c) => (corsOrigins(readConfig(c.env), c.req.url).includes(origin) ? origin : null),
// the browser may only read these when CORS exposes them
exposeHeaders: [...RATE_LIMIT_HEADERS, 'X-Request-Id'],
maxAge: 600,
diff --git a/backend/tests/lib/config.test.ts b/backend/tests/lib/config.test.ts
index e1ef600..1c56fe3 100644
--- a/backend/tests/lib/config.test.ts
+++ b/backend/tests/lib/config.test.ts
@@ -1,6 +1,6 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
-import { readConfig } from '../../lib/config.ts';
+import { corsOrigins, readConfig } from '../../lib/config.ts';
import type { Env } from '../../types/env.ts';
const BOM = String.fromCharCode(0xfeff);
@@ -16,8 +16,8 @@ test('secrets lose a byte order mark and whitespace, and blank ones count as uns
assert.equal(config({ CLOUDINARY_CLOUD_NAME: 'cloud' }).cloudinary, null);
});
-test('CORS allows FRONTEND_URL and ALLOWED_ORIGINS by origin, and the Vite server only when neither is set', () => {
- assert.deepEqual(config({}).allowedOrigins, ['http://localhost:5173', 'http://127.0.0.1:5173']);
+test('CORS allows FRONTEND_URL and ALLOWED_ORIGINS by origin', () => {
+ assert.deepEqual(config({}).allowedOrigins, []);
assert.deepEqual(config({ FRONTEND_URL: 'https://app.example.com/' }).allowedOrigins, ['https://app.example.com']);
assert.deepEqual(
config({ FRONTEND_URL: 'https://app.example.com', ALLOWED_ORIGINS: ' https://preview.example.pages.dev , not a url,https://app.example.com/x' }).allowedOrigins,
@@ -34,3 +34,9 @@ test('the Instagram OAuth urls drop trailing slashes', () => {
appSecret: undefined,
});
});
+
+test('the local Vite server is allowed only when the Worker itself runs on localhost', () => {
+ const settings = config({ FRONTEND_URL: 'https://app.example.com' });
+ assert.deepEqual(corsOrigins(settings, 'https://api.example.workers.dev/api/health'), ['https://app.example.com']);
+ assert.deepEqual(corsOrigins(settings, 'http://localhost:8787/api/health'), ['https://app.example.com', 'http://localhost:5173', 'http://127.0.0.1:5173']);
+});