Skip to content

Commit

Permalink
fix: fix bug #1544, type check store parameters instead of coercing
Browse files Browse the repository at this point in the history
  • Loading branch information
FUDCo committed Sep 4, 2020
1 parent 473d04b commit 6d9b4b8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
22 changes: 15 additions & 7 deletions packages/SwingSet/src/kernel/state/storageWrapper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* global harden */

import { assert } from '@agoric/assert';
import { insistStorageAPI } from '../../storageAPI';

// We manage a host-realm Storage object with a has/getKeys/get/set/del API.
Expand Down Expand Up @@ -46,16 +47,19 @@ export function guardStorage(hostStorage) {
}

function has(key) {
return !!callAndWrapError('has', `${key}`);
assert.typeof(key, 'string');
return !!callAndWrapError('has', key);
}

// hostStorage.getKeys returns a host-realm Generator, so we return a
// kernel-realm wrapper generator that returns the same contents, and guard
// against the host-realm Generator throwing any new errors as it runs
function* getKeys(start, end) {
assert.typeof(start, 'string');
assert.typeof(end, 'string');
try {
for (const key of hostStorage.getKeys(`${start}`, `${end}`)) {
yield `${key}`;
for (const key of hostStorage.getKeys(start, end)) {
yield key;
}
} catch (err) {
console.error(
Expand All @@ -69,19 +73,23 @@ export function guardStorage(hostStorage) {
}

function get(key) {
const value = callAndWrapError('get', `${key}`);
assert.typeof(key, 'string');
const value = callAndWrapError('get', key);
if (value === undefined) {
return undefined;
}
return `${value}`;
return value;
}

function set(key, value) {
callAndWrapError('set', `${key}`, `${value}`);
assert.typeof(key, 'string');
assert.typeof(value, 'string');
callAndWrapError('set', key, value);
}

function del(key) {
callAndWrapError('delete', `${key}`);
assert.typeof(key, 'string');
callAndWrapError('delete', key);
}

return harden({ has, getKeys, get, set, delete: del });
Expand Down
18 changes: 18 additions & 0 deletions packages/SwingSet/test/test-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,24 @@ function duplicateKeeper(getState) {
return makeKernelKeeper(enhancedCrankBuffer);
}

test('hostStorage param guards', async t => {
const { kstorage, commitCrank } = buildKeeperStorageInMemory();
kstorage.set('foo', true);
t.throws(commitCrank, { message: /must be a string/ });
kstorage.set(true, 'foo');
t.throws(commitCrank, { message: /must be a string/ });
kstorage.has(true);
t.throws(commitCrank, { message: /must be a string/ });
kstorage.getKeys('foo', true);
t.throws(commitCrank, { message: /must be a string/ });
kstorage.getKeys(true, 'foo');
t.throws(commitCrank, { message: /must be a string/ });
kstorage.get(true);
t.throws(commitCrank, { message: /must be a string/ });
kstorage.delete(true);
t.throws(commitCrank, { message: /must be a string/ });
});

test('kernel state', async t => {
const { kstorage, getState, commitCrank } = buildKeeperStorageInMemory();
const k = makeKernelKeeper(kstorage);
Expand Down

0 comments on commit 6d9b4b8

Please sign in to comment.