-
Notifications
You must be signed in to change notification settings - Fork 82
utils: surface troubleshooting guidance for certificate trust errors #2357
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
Open
alexweininger
wants to merge
5
commits into
main
Choose a base branch
from
alexweininger/cert-trust-error-messaging
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+144
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8ef9485
utils: surface troubleshooting guidance for certificate trust errors
alexweininger 5b2e733
Remove unnecessary type assertion in test helper
alexweininger 8a109f4
Address review: drop 'certificate has expired', clarify hint wording
alexweininger 3feb29e
Cover UNABLE_TO_GET_ISSUER_CERT (non-_LOCALLY) variant
alexweininger 6cec7b3
Use aka.ms redirect for the troubleshooting link
alexweininger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import type { IParsedError } from '../../index'; | ||
|
|
||
| /** | ||
| * Default troubleshooting documentation for certificate trust failures. This is a stable aka.ms | ||
| * redirect that points at the canonical Azure extensions troubleshooting guidance. | ||
| */ | ||
| export const certificateTroubleshootingLink: string = 'https://aka.ms/AA11ri61'; | ||
|
|
||
| /** | ||
| * Node.js/OpenSSL TLS error codes that indicate the runtime could not build a trusted | ||
| * certificate chain, which on developer machines is almost always a corporate proxy or | ||
| * SSL-inspection appliance whose root CA is not trusted by the Node extension host. | ||
| */ | ||
| const certificateErrorCodes: readonly string[] = [ | ||
| 'UNABLE_TO_GET_ISSUER_CERT_LOCALLY', | ||
| 'UNABLE_TO_GET_ISSUER_CERT', | ||
| 'UNABLE_TO_VERIFY_LEAF_SIGNATURE', | ||
| 'SELF_SIGNED_CERT_IN_CHAIN', | ||
| 'DEPTH_ZERO_SELF_SIGNED_CERT', | ||
| 'CERT_UNTRUSTED', | ||
| 'CERT_SIGNATURE_FAILURE', | ||
| 'CERT_CHAIN_TOO_LONG', | ||
| ]; | ||
|
|
||
| /** | ||
| * Message fragments that map to the same certificate trust failures. Some SDKs surface the | ||
| * human-readable OpenSSL string rather than the error code. | ||
| */ | ||
| const certificateErrorMessages: readonly string[] = [ | ||
| 'unable to get local issuer certificate', | ||
| 'unable to get issuer certificate', | ||
| 'unable to verify the first certificate', | ||
| 'self signed certificate', | ||
| 'self-signed certificate', | ||
| ]; | ||
|
|
||
| /** | ||
| * Determines whether a parsed error was caused by an untrusted TLS certificate chain, which | ||
| * typically happens behind a corporate proxy or SSL-inspection appliance. Callers can use this | ||
| * to surface targeted troubleshooting guidance (see {@link certificateTroubleshootingLink}) | ||
| * instead of the raw, cryptic error. | ||
| */ | ||
| export function isCertificateTrustError(parsedError: IParsedError): boolean { | ||
| const errorType = (parsedError.errorType ?? '').toUpperCase(); | ||
| if (certificateErrorCodes.some(code => errorType === code)) { | ||
| return true; | ||
| } | ||
|
|
||
| const message = (parsedError.message ?? '').toLowerCase(); | ||
| if (!message) { | ||
| return false; | ||
| } | ||
|
|
||
| if (certificateErrorCodes.some(code => message.includes(code.toLowerCase()))) { | ||
| return true; | ||
| } | ||
|
|
||
| return certificateErrorMessages.some(fragment => message.includes(fragment)); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import * as assert from 'assert'; | ||
| import { IParsedError } from '..'; | ||
| import { parseError } from '../src/parseError'; | ||
| import { isCertificateTrustError } from '../src/utils/certificateTrustError'; | ||
|
|
||
| function parsed(partial: Partial<IParsedError>): IParsedError { | ||
| const base: IParsedError = { | ||
| errorType: '', | ||
| message: '', | ||
| isUserCancelledError: false, | ||
| name: 'Error', | ||
| }; | ||
| return { ...base, ...partial }; | ||
| } | ||
|
|
||
| suite('isCertificateTrustError', () => { | ||
| test('matches Node TLS error codes (errorType)', () => { | ||
| const codes = [ | ||
| 'UNABLE_TO_GET_ISSUER_CERT_LOCALLY', | ||
| 'UNABLE_TO_VERIFY_LEAF_SIGNATURE', | ||
| 'SELF_SIGNED_CERT_IN_CHAIN', | ||
| 'DEPTH_ZERO_SELF_SIGNED_CERT', | ||
| 'CERT_UNTRUSTED', | ||
| ]; | ||
| for (const errorType of codes) { | ||
| assert.strictEqual(isCertificateTrustError(parsed({ errorType })), true, errorType); | ||
| } | ||
| }); | ||
|
|
||
| test('matches error code embedded in the message', () => { | ||
| assert.strictEqual(isCertificateTrustError(parsed({ message: 'request failed: SELF_SIGNED_CERT_IN_CHAIN' })), true); | ||
| }); | ||
|
|
||
| test('matches OpenSSL human-readable messages (case-insensitive)', () => { | ||
| assert.strictEqual(isCertificateTrustError(parsed({ message: 'unable to get local issuer certificate' })), true); | ||
| assert.strictEqual(isCertificateTrustError(parsed({ message: 'Error: Unable To Get Local Issuer Certificate' })), true); | ||
| assert.strictEqual(isCertificateTrustError(parsed({ message: 'self signed certificate in certificate chain' })), true); | ||
| assert.strictEqual(isCertificateTrustError(parsed({ message: 'self-signed certificate' })), true); | ||
| assert.strictEqual(isCertificateTrustError(parsed({ message: 'unable to verify the first certificate' })), true); | ||
| }); | ||
|
|
||
| test('does not match unrelated errors', () => { | ||
| assert.strictEqual(isCertificateTrustError(parsed({ errorType: 'ECONNREFUSED', message: 'connection refused' })), false); | ||
| assert.strictEqual(isCertificateTrustError(parsed({ errorType: 'NotFoundError', message: 'The resource was not found.' })), false); | ||
| assert.strictEqual(isCertificateTrustError(parsed({ message: '' })), false); | ||
| assert.strictEqual(isCertificateTrustError(parsed({})), false); | ||
| }); | ||
|
|
||
| test('works with parseError output for a realistic cert error', () => { | ||
| const err = Object.assign(new Error('unable to get local issuer certificate'), { code: 'UNABLE_TO_GET_ISSUER_CERT_LOCALLY' }); | ||
| assert.strictEqual(isCertificateTrustError(parseError(err)), true); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.