Skip to content

Commit 9b69d77

Browse files
author
brizental
committed
Attend to review comments
1 parent 675bfbe commit 9b69d77

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

glean/src/plugins/encryption.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,40 @@ import { JSONObject } from "../core/utils";
1212
import CoreEvents from "../core/events";
1313

1414
/**
15-
* A plugin that encrypts the payload of pings before they are stored and sent.
15+
* A plugin that listens for the `afterPingCollection` event and encrypts **all** outgoing pings
16+
* with the JWK provided upon initialization.
17+
*
18+
* This plugin will modify the schema of outgoing pings to:
19+
*
20+
* ```json
21+
* {
22+
* payload: "<encrypted-payload>"
23+
* }
24+
* ```
1625
*/
1726
class PingEncryptionPlugin extends Plugin<typeof CoreEvents["afterPingCollection"]> {
18-
constructor(readonly jwk: JWK) {
27+
/**
28+
* Creates a new PingEncryptionPlugin instance.
29+
*
30+
* @param jwk The JWK that will be used to encode outgoing ping payloads.
31+
* @param alg The algorithm this plugin will use for parsing the JWK. If this argument is not present,
32+
* we will look for the `alg` key in the JWK. If neither is present we defaut to "ECDH-ES".
33+
*/
34+
constructor(private jwk: JWK, private alg?: string) {
1935
super(CoreEvents["afterPingCollection"].name, "pingEncryptionPlugin");
36+
37+
if (!alg) {
38+
this.alg = jwk.alg ? jwk.alg : "ECDH-ES";
39+
}
2040
}
2141

2242
async action(payload: PingPayload): Promise<JSONObject> {
23-
const key = await parseJwk(this.jwk);
43+
const key = await parseJwk(this.jwk, this.alg);
2444
const encoder = new TextEncoder();
2545
const encodedPayload = await new CompactEncrypt(encoder.encode(JSON.stringify(payload)))
2646
.setProtectedHeader({
2747
kid: this.jwk.kid,
28-
alg: this.jwk.alg,
48+
alg: this.alg,
2949
enc: "A256GCM",
3050
typ: "JWE",
3151
})

glean/tests/plugins/encryption.spec.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ import PingEncryptionPlugin from "../../src/plugins/encryption";
1313
const sandbox = sinon.createSandbox();
1414

1515
describe("PingEncryptionPlugin", function() {
16+
// eslint-disable-next-line mocha/no-hooks-for-single-case
1617
beforeEach(async function() {
1718
await Glean.testResetGlean("something something");
1819
});
1920

21+
// eslint-disable-next-line mocha/no-hooks-for-single-case
2022
afterEach(function () {
2123
sandbox.restore();
2224
});
@@ -26,25 +28,31 @@ describe("PingEncryptionPlugin", function() {
2628
sandbox.stub(Glean["pingUploader"], "triggerUpload").callsFake(() => Promise.resolve());
2729

2830
await Glean.testUninitialize();
31+
await Glean.testInitialize(
32+
"something something",
33+
true,
34+
{
35+
plugins: [
36+
new PingEncryptionPlugin({
37+
"crv": "P-256",
38+
"kid": "test",
39+
"kty": "EC",
40+
"x": "Q20tsJdrryWJeuPXTM27wIPb_YbsdYPpkK2N9O6aXwM",
41+
"y": "1onW1swaCcN1jkmkIwhXpCm55aMP8GRJln5E8WQKLJk"
42+
})
43+
]
44+
}
45+
);
2946

30-
const plugin = new PingEncryptionPlugin({
31-
"kid": "test",
32-
"alg": "ECDH-ES",
33-
"crv": "P-256",
34-
"kty": "EC",
35-
"x": "Qqihp7EryDN2-qQ-zuDPDpy5mJD5soFBDZmzPWTmjwk",
36-
"y": "PiEQVUlywi2bEsA3_5D0VFrCHClCyUlLW52ajYs-5uc"
37-
});
38-
await Glean.testInitialize("something something", true, { plugins: [ plugin ]});
3947
const ping = new PingType({
4048
name: "ping",
4149
includeClientId: true,
4250
sendIfEmpty: true,
4351
});
44-
4552
await PingMaker.collectAndStorePing("ident", ping);
4653
const recordedPing = (await Glean.pingsDatabase.getAllPings())["ident"];
47-
54+
4855
assert.ok("payload" in recordedPing.payload);
56+
assert.strictEqual(Object.keys(recordedPing.payload).length, 1);
4957
});
5058
});

0 commit comments

Comments
 (0)