From 037f1f5dfbd84ea19f6db792baab3a3f32483b6e Mon Sep 17 00:00:00 2001 From: Aleksandr Borovskii Date: Mon, 30 Nov 2026 10:50:00 +0100 Subject: [PATCH] fix(repeater): always report a string errorCode for failed HTTP requests The bridges classify a failed request by a Node.js-style string errorCode. libcurl codes are already converted in curl.on('error'), but other error sources still reach the execute() catch block unchanged: a virtual script that throws an error with a numeric code was reported with that number, and one that throws null or undefined made execute() reject, so the bridges never got an answer. Run numeric codes through CurlErrorMapper.toErrorCode, take only string values for syscall and name, and return an error response for any thrown value. --- .../HttpRequestExecutor.spec.ts | 75 ++++++++++++++++++- src/RequestExecutor/HttpRequestExecutor.ts | 35 ++++++++- 2 files changed, 106 insertions(+), 4 deletions(-) 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;