Skip to content

Commit b4bd7e5

Browse files
author
Ioan Moldovan
authored
#5032 Show clearer error message why key is not usable (#5894)
* feat: show clearer error message why key is not usable * fix: pr reviews
1 parent 0c91fbe commit b4bd7e5

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

extension/chrome/elements/pgp_pubkey.htm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<div id="pgp_block" class="pgp_pubkey" data-test="container-pgp-pubkey">
1515
<div class="line error_container hidden">
1616
<div class="error_info">
17-
<span data-test="error-introduce-label">This OpenPGP key is not usable.</span>
17+
<span class="error_introduce_label" data-test="error-introduce-label">This OpenPGP key is not usable.</span>
1818
<input class="input_error_email" disabled data-test="error-email-input" />
1919
</div>
2020

extension/chrome/elements/pgp_pubkey.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { Url } from '../../js/common/core/common.js';
1313
import { View } from '../../js/common/view.js';
1414
import { Xss } from '../../js/common/platform/xss.js';
1515
import { ContactStore } from '../../js/common/platform/store/contact-store.js';
16+
import { Buf } from '../../js/common/core/buf.js';
17+
import { OpenPGPKey } from '../../js/common/core/crypto/pgp/openpgp-key.js';
1618

1719
// todo - this should use KeyImportUI for consistency.
1820
View.run(
@@ -71,7 +73,7 @@ View.run(
7173
!this.firstParsedPublicKey.usableForEncryption &&
7274
!this.firstParsedPublicKey.usableForSigning
7375
) {
74-
this.showKeyNotUsableError();
76+
await this.showKeyNotUsableError();
7577
} else {
7678
let emailText = '';
7779
if (this.parsedPublicKeys.length === 1) {
@@ -116,7 +118,7 @@ View.run(
116118
frameId: this.frameId,
117119
});
118120
} else {
119-
this.showKeyNotUsableError();
121+
await this.showKeyNotUsableError();
120122
}
121123
}
122124
this.sendResizeMsg();
@@ -137,6 +139,25 @@ View.run(
137139
);
138140
};
139141

142+
private getErrorText = async () => {
143+
let errorStr = '';
144+
const { keys, errs } = await KeyUtil.readMany(Buf.fromUtfStr(this.armoredPubkey));
145+
errorStr = errs.join('\n');
146+
for (const key of keys) {
147+
const errorMessage = await OpenPGPKey.checkPublicKeyError(key);
148+
if (errorMessage) {
149+
const match = new RegExp(/Error encrypting message: (.+)$/).exec(errorMessage);
150+
// remove `error: error encrypting message: part`, so error message will begin directly from error reason
151+
if (match) {
152+
errorStr += match[1];
153+
} else {
154+
errorStr += errorMessage;
155+
}
156+
}
157+
}
158+
return errorStr;
159+
};
160+
140161
private sendResizeMsg = () => {
141162
const origHeight = $('#pgp_block').height();
142163
if (!origHeight) {
@@ -167,8 +188,9 @@ View.run(
167188
}
168189
};
169190

170-
private showKeyNotUsableError = () => {
191+
private showKeyNotUsableError = async () => {
171192
$('.error_container').removeClass('hidden');
193+
$('.error_introduce_label').html(`This OpenPGP key is not usable.<br/><small>(${await this.getErrorText()})</small>`); // xss-escaped
172194
$('.hide_if_error').hide();
173195
$('.fingerprints, .add_contact, #manual_import_warning').remove();
174196
const email = this.firstParsedPublicKey?.emails[0];

extension/css/cryptup.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,10 @@ td {
14461446
align-items: flex-start;
14471447
}
14481448

1449+
.pgp_neutral .error_container .error_info .error_introduce_label {
1450+
white-space: pre-line;
1451+
}
1452+
14491453
.pgp_neutral .error_container .error_info span {
14501454
color: #a44;
14511455
font-size: 14px;
@@ -1632,6 +1636,10 @@ td {
16321636
display: none;
16331637
}
16341638

1639+
#pgp_block.pgp_pubkey .action_show_full {
1640+
width: 118px;
1641+
}
1642+
16351643
#pgp_block .three_dots {
16361644
text-align: center;
16371645
width: 25px;

extension/js/common/core/crypto/pgp/openpgp-key.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,19 @@ export class OpenPGPKey {
428428
return nonDummyPrvPackets.every(p => p.isDecrypted());
429429
}
430430

431+
public static async checkPublicKeyError(pubkey: Key): Promise<string | undefined> {
432+
try {
433+
const key = await OpenPGPKey.extractExternalLibraryObjFromKey(pubkey);
434+
await opgp.encrypt({
435+
message: await opgp.createMessage({ text: OpenPGPKey.encryptionText }),
436+
encryptionKeys: key.toPublic(),
437+
format: 'armored',
438+
});
439+
return undefined;
440+
} catch (err) {
441+
return String(err);
442+
}
443+
}
431444
public static isFullyEncrypted(key: OpenPGP.PrivateKey): boolean {
432445
const nonDummyPrvPackets = OpenPGPKey.getPrvPackets(key);
433446
return nonDummyPrvPackets.every(p => !p.isDecrypted());

test/source/tests/settings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ export const defineSettingsTests = (testVariant: TestVariant, testWithBrowser: T
382382
const firstFrameId = /frameId=.*?&/s.exec(framesUrls[0])![0];
383383
const errorFrame = await contactsFrame.getFrame(['pgp_pubkey.htm', firstFrameId]);
384384
await errorFrame.waitForContent('@error-introduce-label', 'This OpenPGP key is not usable.');
385+
await errorFrame.waitForContent('@error-introduce-label', 'Could not verify primary key: dsa keys are considered too weak');
385386
await errorFrame.waitForInputValue('@error-email-input', '[email protected]');
386387
})
387388
);

0 commit comments

Comments
 (0)