Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 74 additions & 1 deletion src/RequestExecutor/HttpRequestExecutor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
35 changes: 32 additions & 3 deletions src/RequestExecutor/HttpRequestExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"',
Expand All @@ -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<string, string | string[]>;
Expand Down
Loading