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 conditional parse logic for exponential numbers #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aws-kms-ee",
"version": "0.13.0",
"version": "0.14.0",
"description": "AWS KMS Envelope Encryption",
"main": "./lib/index.js",
"files": [
Expand Down
24 changes: 8 additions & 16 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,16 @@ import * as crypto from './crypto';

export const debug = require('debug')('kms');

const parse = (value) => {
// exported for unit tests
const isExpNumber = val => !Number.isNaN(Number(val)) && `${val}`.includes('E');
export const parse = (value) => {
/* istanbul ignore else */
if (value) {
// DEPRECATED - will remove this natural feature flag in future version
if (
!(value.startsWith('{') && value.endsWith('}')) && // ignore stringified object
!(value.startsWith('"') && value.endsWith('"')) && // ignore properly stringified string
value.split('E').length === 2 // without stringification it is impossible to tell a string that looks like an expo number from an actual number
) {
// forwards compatibility for previousy non-stringified strings that look exponential
if (value && !isExpNumber(value)) {
try {
return JSON.parse(value);
} catch (e) /* istanbul ignore next */ {
// this will handle when the encrypted value was not stringified
return value;
} else {
try {
return JSON.parse(value);
} catch (e) /* istanbul ignore next */ {
// this will handle when the encrypted value was not stringified
return value;
}
}
} else {
return value;
Expand Down
58 changes: 57 additions & 1 deletion test/unit/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'mocha';
import { expect } from 'chai';

import { encryptValue, decryptValue } from '../../src/utils';
import { encryptValue, decryptValue, parse } from '../../src/utils';
import * as crypto from '../../src/crypto';

import { MOCK_GEN_DK_RESPONSE, MOCK_DECRYPT_DK_RESPONSE } from '../../src/fixtures';
Expand Down Expand Up @@ -51,4 +51,60 @@ describe('utils.js', () => {
});
expect(decrypted).to.deep.equal(VALUE2);
});
describe('parse', () => {
it('should parse stringified string', () => {
const original = 'some string';
const parsed = parse(JSON.stringify(original));

expect(parsed).to.equal(original);
});
it('should parse stringified object', () => {
const original = {
f1: 'single capital E but not a num',
f2: {
f3: 'nested',
f4: [{ f5: 'nested in array' }],
},
};
const parsed = parse(JSON.stringify(original));

expect(parsed).to.deep.equal(original);
});
it('should parse stringified array with field value: exponential string', () => {
const original = [
'33E44',
];
const parsed = parse(JSON.stringify(original));

expect(parsed).to.deep.equal(original);
});
it('should parse stringified object with field value: exponential string', () => {
const original = {
f1: '33E44',
};
const parsed = parse(JSON.stringify(original));

expect(parsed).to.deep.equal(original);
});
it('should parse stringified object with field value: exponential number', () => {
const original = {
f1: 33E44,
};
const parsed = parse(JSON.stringify(original));

expect(parsed).to.deep.equal(original);
});
it('should parse stringified number', () => {
const original = 42;
const parsed = parse(JSON.stringify(original));

expect(parsed).to.deep.equal(original);
});
it('should NOT parse stringified exponential number', () => {
Copy link
Member

@jgilbert01 jgilbert01 Mar 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • it should NOT parse a non-stringified string that looks like an exponential number
  • it is a subtle difference, but i'm concerned too many things will fall into this condition

const original = '33E44';
const parsed = parse(original);

expect(parsed).to.equal(original);
});
});
});