Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(application-generic): Allow unauthorized certs for bridge url #6717

Merged
merged 1 commit into from
Oct 19, 2024
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
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package.json
apps/api/src/metadata.ts

/.nx/cache
/.nx/workspace-data
/.nx/workspace-data
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import got, {
HTTPError,
MaxRedirectsError,
OptionsOfTextResponseBody,
ParseError,
ReadError,
RequestError,
TimeoutError,
Expand Down Expand Up @@ -130,6 +131,11 @@ export class ExecuteBridgeRequest {
afterResponse:
command.afterResponse !== undefined ? [command.afterResponse] : [],
},
/*
* Reject self-signed and invalid certificates in Production environments but allow them in Development
* as it's common for developers to use self-signed certificates in local environments.
*/
rejectUnauthorized: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SokratisVidros the new error response looks great! One thing though - you forgot to revert the change here to check Environement.name after testing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😮‍💨 Fixed on next. Thanks for noticing. That's what happens if you are committing code in between Duplo gaming sessions.

};

const request = [PostActionEnum.EXECUTE, PostActionEnum.PREVIEW].includes(
Expand Down Expand Up @@ -315,6 +321,18 @@ export class ExecuteBridgeRequest {
});
}

if (error instanceof ParseError) {
Logger.error(
`Bridge URL response code is 2xx, but parsing body fails. \`${url}\``,
LOG_CONTEXT,
);
throw new BadRequestException({
message:
BRIDGE_EXECUTION_ERROR.MAXIMUM_REDIRECTS_EXCEEDED.message(url),
code: BRIDGE_EXECUTION_ERROR.MAXIMUM_REDIRECTS_EXCEEDED.code,
});
}

if (body.code === TUNNEL_ERROR_CODE) {
// Handle known tunnel errors
const tunnelBody = body as TunnelResponseError;
Expand All @@ -328,6 +346,17 @@ export class ExecuteBridgeRequest {
});
}

if (error.code === 'DEPTH_ZERO_SELF_SIGNED_CERT') {
Logger.error(
`Bridge URL is uing a self-signed certificate that is not allowed for production environments. \`${url}\``,
LOG_CONTEXT,
);
throw new BadRequestException({
message: BRIDGE_EXECUTION_ERROR.SELF_SIGNED_CERTIFICATE.message(url),
code: BRIDGE_EXECUTION_ERROR.SELF_SIGNED_CERTIFICATE.code,
});
}

if (error.response?.statusCode === 502) {
/*
* Tunnel was live, but the Bridge endpoint was down.
Expand Down
10 changes: 10 additions & 0 deletions libs/application-generic/src/utils/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ export const BRIDGE_EXECUTION_ERROR = {
code: 'BRIDGE_REQUEST_TIMEOUT',
message: (url: string) => `Bridge request timeout for \`${url}\``,
},
BRIDGE_PARSE_ERROR: {
code: 'BRIDGE_PARSE_ERROR',
message: (url: string) =>
`Bridge response for \`${url}\` is not valid JSON`,
},
UNSUPPORTED_PROTOCOL: {
code: 'UNSUPPORTED_PROTOCOL',
message: (url: string) => `Unsupported protocol for \`${url}\``,
Expand All @@ -88,4 +93,9 @@ export const BRIDGE_EXECUTION_ERROR = {
code: 'MAXIMUM_REDIRECTS_EXCEEDED',
message: (url: string) => `Maximum redirects exceeded for \`${url}\``,
},
SELF_SIGNED_CERTIFICATE: {
code: 'SELF_SIGNED_CERTIFICATE',
message: (url: string) =>
`Bridge Endpoint can't use a self signed certificate in production environments.`,
},
} satisfies Record<string, { code: string; message: (url: string) => string }>;
Loading