Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(infra): allow any localhost port in dev upload CORS (#8274)

teo998a20b0485c
diff --git a/infra/packages/resources/src/resources/cors.test.ts b/infra/packages/resources/src/resources/cors.test.ts
new file mode 100644
index 0000000000..677cf6dad8
--- /dev/null
+++ b/infra/packages/resources/src/resources/cors.test.ts
@@ -0,0 +1,41 @@
+import { describe, expect, mock, test } from 'bun:test';
+
+const fixedLocalPorts = ['http', 'https'].flatMap((scheme) =>
+ Array.from({ length: 10 }, (_, i) => `${scheme}://localhost:${3000 + i}`)
+);
+
+async function originsFor(stack: string): Promise<string[]> {
+ mock.module('../../../shared', () => ({ stack }));
+ // A distinct specifier per stack makes Bun evaluate cors.ts again.
+ const { ALLOWED_ORIGINS } = await import(`./cors.ts?stack=${stack}`);
+ return ALLOWED_ORIGINS;
+}
+
+describe('ALLOWED_ORIGINS', () => {
+ test('prod allows only the fixed local ports', async () => {
+ const origins = await originsFor('prod');
+ expect(origins.slice(0, fixedLocalPorts.length)).toEqual(fixedLocalPorts);
+ expect(origins).not.toContain('http://localhost:*');
+ expect(origins).not.toContain('https://localhost:*');
+ });
+
+ test('non-prod stacks allow any local port', async () => {
+ const prod = await originsFor('prod');
+ for (const stack of ['dev', 'local']) {
+ const origins = await originsFor(stack);
+ expect(origins.slice(0, 2)).toEqual([
+ 'http://localhost:*',
+ 'https://localhost:*',
+ ]);
+ expect(origins.slice(2)).toEqual(prod.slice(fixedLocalPorts.length));
+ }
+ });
+
+ test('each origin has at most one wildcard, as S3 requires', async () => {
+ for (const stack of ['dev', 'prod']) {
+ for (const origin of await originsFor(stack)) {
+ expect(origin.split('*').length).toBeLessThanOrEqual(2);
+ }
+ }
+ });
+});
diff --git a/infra/packages/resources/src/resources/cors.ts b/infra/packages/resources/src/resources/cors.ts
index fe3fd63e97..993e78a4a1 100644
--- a/infra/packages/resources/src/resources/cors.ts
+++ b/infra/packages/resources/src/resources/cors.ts
@@ -1,4 +1,6 @@
-export const ALLOWED_ORIGINS = [
+import { stack } from '../../../shared';
+
+const PROD_LOCAL_ORIGINS = [
'http://localhost:3000',
'http://localhost:3001',
'http://localhost:3002',
@@ -19,6 +21,13 @@ export const ALLOWED_ORIGINS = [
'https://localhost:3007',
'https://localhost:3008',
'https://localhost:3009',
+];
+
+// Browsers upload straight to presigned S3 URLs, so a local frontend on any
+// port needs CORS against non-prod buckets.
+const NON_PROD_LOCAL_ORIGINS = ['http://localhost:*', 'https://localhost:*'];
+
+const SHARED_ORIGINS = [
'http://host.local:3000',
'https://app-dev.macro.com',
'https://app-staging.macro.com',
@@ -41,3 +50,9 @@ export const ALLOWED_ORIGINS = [
'https://*.preview.macro.com',
'tauri://localhost',
];
+
+/** Browser origins allowed by S3 bucket CORS rules on the current Pulumi stack. */
+export const ALLOWED_ORIGINS = [
+ ...(stack === 'prod' ? PROD_LOCAL_ORIGINS : NON_PROD_LOCAL_ORIGINS),
+ ...SHARED_ORIGINS,
+];
diff --git a/infra/stacks/bulk-upload/upload-bucket.test.ts b/infra/stacks/bulk-upload/upload-bucket.test.ts
index 9bddc61c20..6983f57a4e 100644
--- a/infra/stacks/bulk-upload/upload-bucket.test.ts
+++ b/infra/stacks/bulk-upload/upload-bucket.test.ts
@@ -84,8 +84,8 @@ describe('Slack import staging protections', () => {
'HEAD',
]);
expect(rule.allowedOrigins).toContain('https://macro.com');
- expect(rule.allowedOrigins).toContain('http://localhost:3000');
- expect(rule.allowedOrigins).toContain('https://localhost:3000');
+ expect(rule.allowedOrigins).toContain('http://localhost:*');
+ expect(rule.allowedOrigins).toContain('https://localhost:*');
expect(rule.allowedOrigins).not.toContain('*');
expect(rule.exposeHeaders).toContain('ETag');
});