Hosting: stos fitos-alt na alternatywnym urzadzeniu (remote/deploy.ps1, porty 13xxx) + CORS_EXTRA_ORIGINS dla dostepu po IP
This commit is contained in:
@@ -8,3 +8,5 @@ FITOS_FIRST_ADMIN_PASSWORD=ChangeMe123!
|
||||
LOG_LEVEL=info
|
||||
# true tylko gdy API jest serwowane po HTTPS (po HTTP przeglądarka odrzuca cookie Secure i refresh nie działa)
|
||||
COOKIE_SECURE=false
|
||||
# dodatkowe dokładne origins CORS (host na alternatywnym urządzeniu) — puste w dev; localhost jest dozwolony automatycznie
|
||||
# CORS_EXTRA_ORIGINS=http://192.168.50.127:13002,http://192.168.50.127:13003
|
||||
|
||||
@@ -30,9 +30,41 @@ export function assertProductionSecrets(cfg: SecretsGuardInput): void {
|
||||
}
|
||||
}
|
||||
|
||||
function escapeRegexLiteral(value: string): string {
|
||||
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
}
|
||||
|
||||
/**
|
||||
* CORS_EXTRA_ORIGINS: lista dokładnych originów oddzielonych przecinkiem,
|
||||
* np. "http://192.168.50.127:13002,http://192.168.50.127:13003" — używana gdy
|
||||
* apka jest hostowana poza główną domeną (host na alternatywnym urządzeniu).
|
||||
*/
|
||||
export function parseExtraCorsOrigins(raw: string | undefined): string[] {
|
||||
return (raw ?? '')
|
||||
.split(',')
|
||||
.map((s) => s.trim())
|
||||
.filter((s) => s.length > 0);
|
||||
}
|
||||
|
||||
export function buildCorsOriginPatterns(nodeEnv: string, extraOrigins: string[]): RegExp[] {
|
||||
return [
|
||||
/^https?:\/\/(www\.)?domena\.pl(:\d+)?$/,
|
||||
/^https?:\/\/admin\.domena\.pl(:\d+)?$/,
|
||||
/^https?:\/\/(www\.)?tisachi\.ddns\.net(:\d+)?$/,
|
||||
// dodatkowe origins traktujemy dosłownie (escapowane) — pełne dopasowanie originu
|
||||
...extraOrigins.map((o) => new RegExp(`^${escapeRegexLiteral(o)}$`)),
|
||||
// localhost tylko poza produkcją (dev/testy e2e); w produkcji originy wyłącznie znad domeny
|
||||
...(nodeEnv === 'production'
|
||||
? []
|
||||
: [/^https?:\/\/localhost(:\d+)?$/, /^https?:\/\/127\.0\.0\.1(:\d+)?$/]),
|
||||
];
|
||||
}
|
||||
|
||||
const nodeEnv = process.env.NODE_ENV || 'development';
|
||||
|
||||
export const config = {
|
||||
port: parseInt(process.env.PORT || '9002', 10),
|
||||
nodeEnv: process.env.NODE_ENV || 'development',
|
||||
nodeEnv,
|
||||
databaseUrl: process.env.DATABASE_URL || 'postgresql://fitos:fitos@localhost:5432/fitos',
|
||||
jwt: {
|
||||
accessSecret: process.env.JWT_ACCESS_SECRET || 'dev-access-secret',
|
||||
@@ -44,15 +76,10 @@ export const config = {
|
||||
uploadDir: process.env.UPLOAD_DIR || path.join(process.cwd(), 'uploads'),
|
||||
// true only when served over HTTPS — over plain HTTP browsers drop Secure cookies (breaks refresh)
|
||||
cookieSecure: process.env.COOKIE_SECURE === 'true',
|
||||
corsOriginPatterns: [
|
||||
/^https?:\/\/(www\.)?domena\.pl(:\d+)?$/,
|
||||
/^https?:\/\/admin\.domena\.pl(:\d+)?$/,
|
||||
/^https?:\/\/(www\.)?tisachi\.ddns\.net(:\d+)?$/,
|
||||
// localhost tylko poza produkcją (dev/testy e2e); w produkcji originy wyłącznie znad domeny
|
||||
...(process.env.NODE_ENV === 'production'
|
||||
? []
|
||||
: [/^https?:\/\/localhost(:\d+)?$/, /^https?:\/\/127\.0\.0\.1(:\d+)?$/]),
|
||||
],
|
||||
corsOriginPatterns: buildCorsOriginPatterns(
|
||||
nodeEnv,
|
||||
parseExtraCorsOrigins(process.env.CORS_EXTRA_ORIGINS),
|
||||
),
|
||||
};
|
||||
|
||||
if (config.nodeEnv === 'production') {
|
||||
|
||||
51
api/test/unit/config-cors.test.ts
Normal file
51
api/test/unit/config-cors.test.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { buildCorsOriginPatterns, parseExtraCorsOrigins } from '../../src/config.js';
|
||||
|
||||
const allows = (patterns: RegExp[], origin: string) => patterns.some((re) => re.test(origin));
|
||||
|
||||
describe('parseExtraCorsOrigins', () => {
|
||||
it('zwraca pustą listę dla braku zmiennej', () => {
|
||||
expect(parseExtraCorsOrigins(undefined)).toEqual([]);
|
||||
});
|
||||
|
||||
it('dzieli po przecinku, trykuje i pomija puste elementy', () => {
|
||||
expect(
|
||||
parseExtraCorsOrigins(' http://192.168.50.127:13002, ,http://192.168.50.127:13003,'),
|
||||
).toEqual(['http://192.168.50.127:13002', 'http://192.168.50.127:13003']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildCorsOriginPatterns', () => {
|
||||
it('zawsze dopuszcza główną domenę (dowolny port)', () => {
|
||||
const patterns = buildCorsOriginPatterns('production', []);
|
||||
expect(allows(patterns, 'http://tisachi.ddns.net:13002')).toBe(true);
|
||||
expect(allows(patterns, 'https://tisachi.ddns.net')).toBe(true);
|
||||
});
|
||||
|
||||
it('dodaje dokładne origins z listy (np. alt-host po IP)', () => {
|
||||
const patterns = buildCorsOriginPatterns('production', [
|
||||
'http://192.168.50.127:13002',
|
||||
'http://192.168.50.127:13003',
|
||||
]);
|
||||
expect(allows(patterns, 'http://192.168.50.127:13002')).toBe(true);
|
||||
expect(allows(patterns, 'http://192.168.50.127:13003')).toBe(true);
|
||||
});
|
||||
|
||||
it('extra origin dopasowuje dokładnie — nie jako prefiks ani inny port', () => {
|
||||
const patterns = buildCorsOriginPatterns('production', ['http://192.168.50.127:13002']);
|
||||
expect(allows(patterns, 'http://192.168.50.127:130022')).toBe(false);
|
||||
expect(allows(patterns, 'http://192.168.50.127')).toBe(false);
|
||||
expect(allows(patterns, 'http://evil.com/?x=http://192.168.50.127:13002')).toBe(false);
|
||||
});
|
||||
|
||||
it('metacharaktery w origin nie stają się wzorcem regex', () => {
|
||||
const patterns = buildCorsOriginPatterns('production', ['http://a.example.com:13002']);
|
||||
expect(allows(patterns, 'http://a.example.com:13002')).toBe(true);
|
||||
expect(allows(patterns, 'http://aXexample.com:13002')).toBe(false);
|
||||
});
|
||||
|
||||
it('localhost tylko poza produkcją', () => {
|
||||
expect(allows(buildCorsOriginPatterns('development', []), 'http://localhost:5173')).toBe(true);
|
||||
expect(allows(buildCorsOriginPatterns('production', []), 'http://localhost:5173')).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user