Skip to content

Commit 94e7922

Browse files
committed
Parameterizing allowed origins
1 parent d807e17 commit 94e7922

File tree

5 files changed

+26
-12
lines changed

5 files changed

+26
-12
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "labelzoom-cf-api-proxy",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"private": true,
55
"scripts": {
66
"deploy": "wrangler deploy",

src/cors.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,15 @@ const corsHeaders = {
55
"Access-Control-Allow-Credentials": "true",
66
};
77

8-
const corsValidOrigins = [
9-
'https://labelzoom.net',
10-
'https://www.labelzoom.net',
11-
'https://api.labelzoom.net',
12-
// 'http://localhost:3000',
13-
];
14-
158
/**
169
* Handle OPTIONS request for CORS
1710
* @param request
1811
* @returns
1912
*/
20-
export async function handleOptions(request: Request): Promise<Response> {
13+
export async function handleOptions(request: Request, env: Env): Promise<Response> {
2114
const origin = request.headers.get("Origin") ?? '';
2215

23-
if (!corsValidOrigins.includes(origin)) {
16+
if (!env.LZ_ALLOWED_ORIGINS.includes(origin)) {
2417
return new Response(`Origin ${origin} not allowed`, { status: 403 });
2518
}
2619

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export default {
9696
if (url.pathname.startsWith('/api/')) {
9797
if (request.method === "OPTIONS") {
9898
// Handle CORS preflight requests
99-
return handleOptions(request);
99+
return handleOptions(request, env);
100100
}
101101
if (url.pathname.startsWith('/api/v2/convert/')) {
102102
return responseWithAllowOrigin(

worker-configuration.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Generated by Wrangler
22
// After adding bindings to `wrangler.toml`, regenerate this interface via `npm run cf-typegen`
33
interface Env {
4+
LZ_ALLOWED_ORIGINS: string[];
45
LZ_LOG_SAMPLE_RATE: number;
56
LZ_PROD_API_BASE_URL: string;
67
LZ_PROD_API_SECRET_KEY: string;

wrangler.toml

+21-1
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,39 @@ cpu_ms = 100
2323
# Note: Use secrets to store sensitive data.
2424
# - https://developers.cloudflare.com/workers/configuration/secrets/
2525
[vars]
26+
LZ_ALLOWED_ORIGINS = [
27+
'https://labelzoom.net',
28+
'https://www.labelzoom.net'
29+
]
2630
LZ_LOG_SAMPLE_RATE = 1.0
2731
LZ_PROD_API_BASE_URL = "https://api-backend.labelzoom.net"
2832
# LZ_PROD_API_SECRET_KEY = <SECRET>
2933

3034
[env.beta.vars]
35+
LZ_ALLOWED_ORIGINS = [
36+
'https://labelzoom.net',
37+
'https://www.labelzoom.net'
38+
]
3139
LZ_LOG_SAMPLE_RATE = 1.0
3240
LZ_PROD_API_BASE_URL = "https://api-backend.labelzoom.net"
3341
[env.beta.limits]
3442
cpu_ms = 100
3543

3644
[env.public.vars]
45+
LZ_ALLOWED_ORIGINS = [
46+
'https://labelzoom.net',
47+
'https://www.labelzoom.net'
48+
]
3749
LZ_LOG_SAMPLE_RATE = 0.0
3850
LZ_PROD_API_BASE_URL = "https://api-backend.labelzoom.net"
3951
[env.public.limits]
4052
cpu_ms = 100
4153

4254
[env.eus1.vars]
55+
LZ_ALLOWED_ORIGINS = [
56+
'https://labelzoom.net',
57+
'https://www.labelzoom.net'
58+
]
4359
LZ_LOG_SAMPLE_RATE = 0.0
4460
LZ_PROD_API_BASE_URL = "https://prod-api-eus1-backend.labelzoom.net"
4561
[env.eus1.limits]
@@ -48,7 +64,11 @@ cpu_ms = 100
4864
# mode = "smart"
4965

5066
[env.eus2.vars]
51-
LZ_LOG_SAMPLE_RATE = 0.0
67+
LZ_ALLOWED_ORIGINS = [
68+
'https://labelzoom.net',
69+
'https://www.labelzoom.net'
70+
]
71+
LZ_LOG_SAMPLE_RATE = 0.001
5272
LZ_PROD_API_BASE_URL = "https://prod-api-eus2-backend.labelzoom.net"
5373
[env.eus2.limits]
5474
cpu_ms = 100

0 commit comments

Comments
 (0)