diff --git a/src/RequestExecutor/HttpRequestExecutor.spec.ts b/src/RequestExecutor/HttpRequestExecutor.spec.ts index 404bcf7b..c0804461 100644 --- a/src/RequestExecutor/HttpRequestExecutor.spec.ts +++ b/src/RequestExecutor/HttpRequestExecutor.spec.ts @@ -16,7 +16,7 @@ import { verify, when } from 'ts-mockito'; -import { Curl } from '@brightsec/node-libcurl'; +import { Curl, CurlCode } from '@brightsec/node-libcurl'; import forge from 'node-forge'; import http from 'node:http'; import http2 from 'node:http2'; @@ -596,6 +596,79 @@ describe('HttpRequestExecutor', () => { }); }); + it.each([ + { + code: CurlCode.CURLE_OPERATION_TIMEDOUT, + expected: 'ETIMEDOUT' + }, + { + code: CurlCode.CURLE_UNSUPPORTED_PROTOCOL, + expected: 'CURLE_UNSUPPORTED_PROTOCOL' + } + ])( + 'should report a numeric error code $code as the string $expected', + async ({ code, expected }) => { + // arrange + const { request, requestOptions } = createRequest(); + const { hostname: virtualScriptId } = new URL(requestOptions.url); + const virtualScript = new VirtualScript( + virtualScriptId, + VirtualScriptType.LOCAL, + 'console.log("test code");' + ); + when(spy(virtualScript).exec(anyString(), anything())).thenReject( + Object.assign(new Error('script failed'), { code }) + ); + when(virtualScriptsMock.find(virtualScriptId)).thenReturn( + virtualScript + ); + const sut = buildSut(); + + // act + const response = await sut.execute(request); + + // assert + expect(response).toMatchObject({ + protocol: Protocol.HTTP, + statusCode: undefined, + errorCode: expected, + message: 'script failed' + }); + } + ); + + it.each([null, undefined, 'script failed'])( + 'should return an error response when a script throws %p', + async (thrown) => { + // arrange + const { request, requestOptions } = createRequest(); + const { hostname: virtualScriptId } = new URL(requestOptions.url); + const virtualScript = new VirtualScript( + virtualScriptId, + VirtualScriptType.LOCAL, + 'console.log("test code");' + ); + when(spy(virtualScript).exec(anyString(), anything())).thenCall(() => + Promise.reject(thrown) + ); + when(virtualScriptsMock.find(virtualScriptId)).thenReturn( + virtualScript + ); + const sut = buildSut(); + + // act + const response = await sut.execute(request); + + // assert + expect(response).toMatchObject({ + protocol: Protocol.HTTP, + statusCode: undefined, + errorCode: undefined, + message: String(thrown) + }); + } + ); + it('should truncate response body with not white-listed mime type', async () => { // arrange const bigBody = 'x'.repeat(1025); diff --git a/src/RequestExecutor/HttpRequestExecutor.ts b/src/RequestExecutor/HttpRequestExecutor.ts index bb5dc641..1dd9076c 100644 --- a/src/RequestExecutor/HttpRequestExecutor.ts +++ b/src/RequestExecutor/HttpRequestExecutor.ts @@ -116,9 +116,7 @@ export class HttpRequestExecutor implements RequestExecutor { return await this.tryRequestWithCertificates(options, targetCerts); } catch (err) { - const { cause } = err; - const { message, code, syscall, name } = cause ?? err; - const errorCode = code ?? syscall ?? name; + const { message, errorCode } = this.describeFailure(err); logger.error( 'Error executing request: "%s %s HTTP/1.1"', @@ -135,6 +133,37 @@ export class HttpRequestExecutor implements RequestExecutor { } } + private describeFailure(err: unknown): { + message?: string; + errorCode?: string; + } { + // Virtual scripts can throw any value, not only an Error. + if (typeof err !== 'object' || err === null) { + return { message: String(err) }; + } + + const { cause } = err as { cause?: unknown }; + const { message, code, syscall, name } = ( + typeof cause === 'object' && cause !== null ? cause : err + ) as { + message?: unknown; + code?: unknown; + syscall?: unknown; + name?: unknown; + }; + + return { + message: typeof message === 'string' ? message : undefined, + // The bridges classify failures by string codes only. + errorCode: + typeof code === 'number' + ? CurlErrorMapper.toErrorCode(code) + : [code, syscall, name].find( + (value): value is string => typeof value === 'string' + ) + }; + } + private request(options: Request): Promise<{ statusCode: number; headers: Record;