From d2839107066c9070ae5434b1d072b39f13ab80e3 Mon Sep 17 00:00:00 2001 From: Yarmo Mackenbach Date: Thu, 6 Oct 2022 17:57:07 +0200 Subject: [PATCH] Fix key caching --- package.json | 2 +- server/keys.js | 24 +++++++++++++----------- yarn.lock | 19 +++++++------------ 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 2a69d24..f6a7464 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,6 @@ "ajv": "^8.6.3", "bent": "^7.3.12", "body-parser": "^1.19.0", - "cache": "^3.0.0", "dialog-polyfill": "^0.5.6", "doipjs": "^0.16.3", "dotenv": "^8.2.0", @@ -18,6 +17,7 @@ "got": "^11.8.2", "hash-wasm": "^4.9.0", "jstransformer-markdown-it": "^2.1.0", + "keyv": "^4.5.0", "libravatar": "^3.0.0", "openpgp": "^5.5.0", "pug": "^3.0.0", diff --git a/server/keys.js b/server/keys.js index 281704c..8a04f08 100644 --- a/server/keys.js +++ b/server/keys.js @@ -32,9 +32,9 @@ import * as doipjs from 'doipjs' import { readKey, readCleartextMessage, verify } from 'openpgp' import { computeWKDLocalPart } from './utils.js' import { createHash } from 'crypto' -import cache from 'cache' +import Keyv from 'keyv' -let c = process.env.ENABLE_EXPERIMENTAL_CACHE ? new cache(60 * 1000) : null +const c = process.env.ENABLE_EXPERIMENTAL_CACHE ? new Keyv() : null const fetchWKD = (id) => { return new Promise(async (resolve, reject) => { @@ -57,9 +57,8 @@ const fetchWKD = (id) => { let plaintext const hash = createHash('md5').update(id).digest('hex') - - if (c && c.get(hash)) { - plaintext = c.get(hash) + if (c && await c.get(hash)) { + plaintext = Uint8Array.from((await c.get(hash)).split(',')) } if (!plaintext) { @@ -86,13 +85,13 @@ const fetchWKD = (id) => { reject(new Error(`No public keys could be fetched using WKD`)) } } - + if (!plaintext) { reject(new Error(`No public keys could be fetched using WKD`)) } if (c) { - c.put(hash, plaintext) + await c.set(hash, plaintext.toString(), 60 * 1000) } } @@ -128,14 +127,17 @@ const fetchHKP = (id, keyserverDomain) => { query = `0x${id}` } + output.fetchURL = `https://${keyserverDomain}/pks/lookup?op=get&options=mr&search=${query}` + const hash = createHash('md5').update(`${id}__${keyserverDomain}`).digest('hex') - if (c && c.get(hash)) { - output = c.get(hash) + if (c && await c.get(hash)) { + output.publicKey = await readKey({ + armoredKey: await c.get(hash) + }) } else { try { output.publicKey = await doipjs.keys.fetchHKP(id, keyserverDomain) - output.fetchURL = `https://${keyserverDomain}/pks/lookup?op=get&options=mr&search=${query}` } catch(error) { reject(new Error(`No public keys could be fetched using HKP`)) } @@ -146,7 +148,7 @@ const fetchHKP = (id, keyserverDomain) => { } if (c) { - c.put(hash, output) + await c.set(hash, output.publicKey.armor(), 60 * 1000) } resolve(output) diff --git a/yarn.lock b/yarn.lock index 803cec8..a1db48b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1272,13 +1272,6 @@ bytesish@^0.4.1: resolved "https://registry.npmjs.org/bytesish/-/bytesish-0.4.4.tgz" integrity sha512-i4uu6M4zuMUiyfZN4RU2+i9+peJh//pXhd9x1oSe1LBkZ3LEbCoygu8W0bXTukU1Jme2txKuotpCZRaC3FLxcQ== -cache@^3.0.0: - version "3.0.0" - resolved "https://registry.yarnpkg.com/cache/-/cache-3.0.0.tgz#1c5857e874f7064be641114a605c7e2ae8a80880" - integrity sha512-sNoM5jithfalxIceo/uFFm5bOlGjux2y8jEvjNb0F/cACWQaMmWuEPTLl6GzLHdFcNsbWBBdqkBd9NyefZ5UQQ== - dependencies: - ds "^1.4.2" - cacheable-lookup@^5.0.3: version "5.0.4" resolved "https://registry.npmjs.org/cacheable-lookup/-/cacheable-lookup-5.0.4.tgz" @@ -1823,11 +1816,6 @@ dotenv@^8.2.0: resolved "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz" integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw== -ds@^1.4.2: - version "1.4.2" - resolved "https://registry.yarnpkg.com/ds/-/ds-1.4.2.tgz#0857aa213790a4fb3abb365b9cec0e9ba8569393" - integrity sha512-d5nMCjfod+srvE/1Bnt/u+L++6N8KJx3ZAi95AGp0g6RtfuGDNlGciWL/iiwKHsFVBVnA3/HEFUq5SW1NgTQ3Q== - duplexer@^0.1.2: version "0.1.2" resolved "https://registry.yarnpkg.com/duplexer/-/duplexer-0.1.2.tgz#3abe43aef3835f8ae077d136ddce0f276b0400e6" @@ -2908,6 +2896,13 @@ keyv@^4.0.0: dependencies: json-buffer "3.0.1" +keyv@^4.5.0: + version "4.5.0" + resolved "https://registry.yarnpkg.com/keyv/-/keyv-4.5.0.tgz#dbce9ade79610b6e641a9a65f2f6499ba06b9bc6" + integrity sha512-2YvuMsA+jnFGtBareKqgANOEKe1mk3HKiXu2fRmAfyxG0MJAywNhi5ttWA3PMjl4NmpyjZNbFifR2vNjW1znfA== + dependencies: + json-buffer "3.0.1" + kind-of@^6.0.2: version "6.0.3" resolved "https://registry.npmjs.org/kind-of/-/kind-of-6.0.3.tgz"