fix: allow explicit HTTP origin in test environments
This commit is contained in:
@@ -26,6 +26,27 @@ describe('OpenApiService', () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('only permits a plain HTTP public origin when an isolated test environment explicitly opts in', async () => {
|
||||
const previousOrigin = process.env.HTTP_API_PUBLIC_ORIGIN;
|
||||
const previousAllowInsecure = process.env.HTTP_API_ALLOW_INSECURE_ORIGIN;
|
||||
process.env.HTTP_API_PUBLIC_ORIGIN = 'http://100.93.204.60:12026/';
|
||||
delete process.env.HTTP_API_ALLOW_INSECURE_ORIGIN;
|
||||
const prisma = {
|
||||
smsApplication: { findFirst: jest.fn().mockResolvedValue({ id: 'app-1', name: '应用A', httpConfig: null, httpIpAllowlist: [] }) },
|
||||
};
|
||||
try {
|
||||
const service = new OpenApiService(prisma as never, {} as never);
|
||||
await expect(service.getConfig('app-1')).rejects.toThrow('HTTP_API_ALLOW_INSECURE_ORIGIN');
|
||||
process.env.HTTP_API_ALLOW_INSECURE_ORIGIN = 'true';
|
||||
await expect(service.getConfig('app-1')).resolves.toEqual(expect.objectContaining({ publicOrigin: 'http://100.93.204.60:12026' }));
|
||||
} finally {
|
||||
if (previousOrigin === undefined) delete process.env.HTTP_API_PUBLIC_ORIGIN;
|
||||
else process.env.HTTP_API_PUBLIC_ORIGIN = previousOrigin;
|
||||
if (previousAllowInsecure === undefined) delete process.env.HTTP_API_ALLOW_INSECURE_ORIGIN;
|
||||
else process.env.HTTP_API_ALLOW_INSECURE_ORIGIN = previousAllowInsecure;
|
||||
}
|
||||
});
|
||||
|
||||
it('replays a completed request for the same idempotency key and body', async () => {
|
||||
const prisma = {
|
||||
openApiRequest: { findUnique: jest.fn().mockResolvedValue({ bodyHash: 'same', status: 'completed', responseBody: { code: 'ACCEPTED', messageId: 'MSG-1' } }) },
|
||||
|
||||
@@ -428,10 +428,12 @@ function httpApiPublicOrigin() {
|
||||
const configured = process.env.HTTP_API_PUBLIC_ORIGIN?.trim().replace(/\/+$/, '');
|
||||
if (!configured) return undefined;
|
||||
const url = new URL(configured);
|
||||
if (url.protocol !== 'https:' || url.username || url.password || url.pathname !== '/' || url.search || url.hash) {
|
||||
const insecureHttpExplicitlyAllowed = process.env.HTTP_API_ALLOW_INSECURE_ORIGIN === 'true' && url.protocol === 'http:';
|
||||
if ((url.protocol !== 'https:' && !insecureHttpExplicitlyAllowed) || url.username || url.password || url.pathname !== '/' || url.search || url.hash) {
|
||||
// This value is copied into customer integration parameters, so fail closed instead of
|
||||
// publishing an insecure or path-dependent endpoint when deployment config is wrong.
|
||||
throw new Error('HTTP_API_PUBLIC_ORIGIN必须是无路径、无凭据的HTTPS源地址');
|
||||
// publishing an insecure or path-dependent endpoint unless an isolated test environment
|
||||
// has explicitly opted into plain HTTP.
|
||||
throw new Error('HTTP_API_PUBLIC_ORIGIN必须是无路径、无凭据的HTTPS源地址;隔离测试环境如需HTTP须显式启用HTTP_API_ALLOW_INSECURE_ORIGIN');
|
||||
}
|
||||
return url.origin;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user