Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure keywords do not collide with numbers #1133

Merged
merged 1 commit into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions packages/zoe/src/cleanProposal.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
import harden from '@agoric/harden';
import { assert, details } from '@agoric/assert';
import { assert, details, openDetail } from '@agoric/assert';
import { mustBeComparable } from '@agoric/same-structure';

import { arrayToObj, assertSubset } from './objArrayConversion';

export const assertCapASCII = keyword => {
// We adopt simple requirements on keywords so that they do not accidentally
// conflict with existing property names.
// We require keywords to be strings, ascii identifiers beggining with an
// upper case letter, and distinct from the stringified form of any number.
//
// Of numbers, only NaN and Infinity, when stringified to a property name,
// produce an ascii identifier beginning with an upper case letter.
// With this rule, a computed indexing expression like `a[+i]` cannot
// lookup a keyword-named property no matter what `i` is.
export const assertKeywordName = keyword => {
assert.typeof(keyword, 'string');
const firstCapASCII = /^[A-Z][a-zA-Z0-9_$]*$/;
assert(
firstCapASCII.test(keyword),
details`keyword ${keyword} must be ascii and must start with a capital letter.`,
details`keyword ${openDetail(
keyword,
)} must be ascii and must start with a capital letter.`,
);
assert(
keyword !== 'NaN' && keyword !== 'Infinity',
details`keyword ${openDetail(keyword)} must not be a number's name`,
);
};

Expand Down Expand Up @@ -65,7 +80,7 @@ export const cleanKeywords = keywordRecord => {

// Assert all key characters are ascii and keys start with a
// capital letter.
keywords.forEach(assertCapASCII);
keywords.forEach(assertKeywordName);

return keywords;
};
Expand Down
4 changes: 2 additions & 2 deletions packages/zoe/src/contracts/multipoolAutoswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import produceIssuer from '@agoric/ertp';
import { assert, details } from '@agoric/assert';

import { makeTable, makeValidateProperties } from '../table';
import { assertCapASCII } from '../cleanProposal';
import { assertKeywordName } from '../cleanProposal';
import {
makeZoeHelpers,
getCurrentPrice,
Expand Down Expand Up @@ -85,7 +85,7 @@ export const makeContract = harden(
// Allows users to add new liquidity pools. `newTokenIssuer` and
// `newTokenKeyword` must not have been already used
const addPool = (newTokenIssuer, newTokenKeyword) => {
assertCapASCII(newTokenKeyword);
assertKeywordName(newTokenKeyword);
const { issuerKeywordRecord } = zcf.getInstanceRecord();
const keywords = Object.keys(issuerKeywordRecord);
const issuers = Object.values(issuerKeywordRecord);
Expand Down
4 changes: 2 additions & 2 deletions packages/zoe/src/zoe.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { producePromise } from '@agoric/produce-promise';

import {
cleanProposal,
assertCapASCII,
assertKeywordName,
getKeywords,
cleanKeywords,
} from './cleanProposal';
Expand Down Expand Up @@ -556,7 +556,7 @@ const makeZoe = (additionalEndowments = {}) => {

addNewIssuer: (issuerP, keyword) =>
issuerTable.getPromiseForIssuerRecord(issuerP).then(issuerRecord => {
assertCapASCII(keyword);
assertKeywordName(keyword);
const { issuerKeywordRecord } = instanceTable.get(instanceHandle);
assert(
!getKeywords(issuerKeywordRecord).includes(keyword),
Expand Down