Skip to content

Commit 8c1c473

Browse files
authored
repo sync
2 parents c6ef7c4 + 200133b commit 8c1c473

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

content/developers/overview/secret-scanning.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,48 @@ openssl_key = OpenSSL::PKey::EC.new(current_key)
278278
puts openssl_key.verify(OpenSSL::Digest::SHA256.new, Base64.decode64(signature), payload.chomp)
279279
```
280280

281+
**Validation sample in JavaScript**
282+
```js
283+
const crypto = require("crypto");
284+
const axios = require("axios");
285+
286+
const GITHUB_KEYS_URI = "https://api.github.com/meta/public_keys/secret_scanning";
287+
288+
/**
289+
* Verify a payload and signature against a public key
290+
* @param {String} payload the value to verify
291+
* @param {String} signature the expected value
292+
* @param {String} keyID the id of the key used to generated the signature
293+
* @return {void} throws if the signature is invalid
294+
*/
295+
const verify_signature = async (payload, signature, keyID) => {
296+
if (typeof payload !== "string" || payload.length === 0) {
297+
throw new Error("Invalid payload");
298+
}
299+
if (typeof signature !== "string" || signature.length === 0) {
300+
throw new Error("Invalid signature");
301+
}
302+
if (typeof keyID !== "string" || keyID.length === 0) {
303+
throw new Error("Invalid keyID");
304+
}
305+
306+
const keys = (await axios.get(GITHUB_KEYS_URI)).data;
307+
if (!(keys?.public_keys instanceof Array) || keys.length === 0) {
308+
throw new Error("No public keys found");
309+
}
310+
311+
const publicKey = keys.public_keys.find((k) => k.key_identifier === keyID) ?? null;
312+
if (publicKey === null) {
313+
throw new Error("No public key found matching key identifier");
314+
}
315+
316+
const verify = crypto.createVerify("SHA256").update(payload);
317+
if (!verify.verify(publicKey.key, Buffer.from(signature, "base64"), "base64")) {
318+
throw new Error("Signature does not match payload");
319+
}
320+
};
321+
```
322+
281323
#### Implement secret revocation and user notification in your secret alert service
282324
283325
For {% data variables.product.prodname_secret_scanning %} in public repositories, you can enhance your secret alert service to revoke the exposed secrets and notify the affected users. How you implement this in your secret alert service is up to you, but we recommend considering any secrets that {% data variables.product.prodname_dotcom %} sends you messages about as public and compromised.

0 commit comments

Comments
 (0)