Only allow key caching inside the access helper

This commit is contained in:
J. Ryan Stinnett 2019-12-12 15:34:01 +00:00
parent 458cc9598d
commit 6338ee9683

View File

@ -27,8 +27,9 @@ import { _t } from './languageHandler';
// single secret storage operation, as it will clear the cached keys once the // single secret storage operation, as it will clear the cached keys once the
// operation ends. // operation ends.
let secretStorageKeys = {}; let secretStorageKeys = {};
let cachingAllowed = false;
export const getSecretStorageKey = async ({ keys: keyInfos }) => { async function getSecretStorageKey({ keys: keyInfos }) {
const keyInfoEntries = Object.entries(keyInfos); const keyInfoEntries = Object.entries(keyInfos);
if (keyInfoEntries.length > 1) { if (keyInfoEntries.length > 1) {
throw new Error("Multiple storage key requests not implemented"); throw new Error("Multiple storage key requests not implemented");
@ -36,7 +37,7 @@ export const getSecretStorageKey = async ({ keys: keyInfos }) => {
const [name, info] = keyInfoEntries[0]; const [name, info] = keyInfoEntries[0];
// Check the in-memory cache // Check the in-memory cache
if (secretStorageKeys[name]) { if (cachingAllowed && secretStorageKeys[name]) {
return [name, secretStorageKeys[name]]; return [name, secretStorageKeys[name]];
} }
@ -70,10 +71,12 @@ export const getSecretStorageKey = async ({ keys: keyInfos }) => {
const key = await inputToKey(input); const key = await inputToKey(input);
// Save to cache to avoid future prompts in the current session // Save to cache to avoid future prompts in the current session
secretStorageKeys[name] = key; if (cachingAllowed) {
secretStorageKeys[name] = key;
}
return [name, key]; return [name, key];
}; }
export const crossSigningCallbacks = { export const crossSigningCallbacks = {
getSecretStorageKey, getSecretStorageKey,
@ -101,6 +104,7 @@ export const crossSigningCallbacks = {
*/ */
export async function accessSecretStorage(func = async () => { }) { export async function accessSecretStorage(func = async () => { }) {
const cli = MatrixClientPeg.get(); const cli = MatrixClientPeg.get();
cachingAllowed = true;
try { try {
if (!cli.hasSecretStorageKey()) { if (!cli.hasSecretStorageKey()) {
@ -139,6 +143,7 @@ export async function accessSecretStorage(func = async () => { }) {
return await func(); return await func();
} finally { } finally {
// Clear secret storage key cache now that work is complete // Clear secret storage key cache now that work is complete
cachingAllowed = false;
secretStorageKeys = {}; secretStorageKeys = {};
} }
} }