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: vulnerability #37

Merged
merged 2 commits into from
May 17, 2024
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ console.log(braces.expand('a{b}c'));
console.log(braces('a/{b,c}/d', { maxLength: 3 })); //=> throws an error
```

### options.maxSymbols

**Type**: `Number`

**Default**: `1024`

**Description**: Limit the count of unique symbols the input string.

```js
console.log(braces('a/{b,c}/d', { maxSymbols: 2 })); //=> throws an error
```

### options.expand

**Type**: `Boolean`
Expand Down
1 change: 1 addition & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module.exports = {
MAX_LENGTH: 1024 * 64,
MAX_SYMBOLS: 1024,

// Digits
CHAR_0: '0', /* 0 */
Expand Down
62 changes: 41 additions & 21 deletions lib/parse.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
'use strict';

const stringify = require('./stringify');
const {isCorrectBraces, validateInput} = require('./validate-input');

/**
* Constants
*/

const {
MAX_LENGTH,
MAX_SYMBOLS,
CHAR_BACKSLASH, /* \ */
CHAR_BACKTICK, /* ` */
CHAR_COMMA, /* , */
Expand All @@ -34,6 +36,11 @@ const parse = (input, options = {}) => {
}

let opts = options || {};

validateInput(input, {
maxSymbols: opts.maxSymbols || MAX_SYMBOLS,
});

let max = typeof opts.maxLength === 'number' ? Math.min(MAX_LENGTH, opts.maxLength) : MAX_LENGTH;
if (input.length > max) {
throw new SyntaxError(`Input length (${input.length}), exceeds max characters (${max})`);
Expand Down Expand Up @@ -304,30 +311,43 @@ const parse = (input, options = {}) => {
push({ type: 'text', value });
}

flattenBlocks(stack)
markImbalancedBraces(ast);
push({ type: 'eos' });

return ast;
};

module.exports = parse;

function markImbalancedBraces({nodes}) {
// Mark imbalanced braces and brackets as invalid
for (const node of nodes) {
if (!node.nodes && !node.invalid) {
if (node.type === 'open') node.isOpen = true;
if (node.type === 'close') node.isClose = true;
if (!node.nodes) node.type = 'text';

node.invalid = true;
}

delete node.parent;
delete node.prev;
}
}

function flattenBlocks(stack) {
let block;
do {
block = stack.pop();

if (block.type !== 'root') {
block.nodes.forEach(node => {
if (!node.nodes) {
if (node.type === 'open') node.isOpen = true;
if (node.type === 'close') node.isClose = true;
if (!node.nodes) node.type = 'text';
node.invalid = true;
}
});
if (block.type === 'root')
continue;

// get the location of the block on parent.nodes (block's siblings)
let parent = stack[stack.length - 1];
let index = parent.nodes.indexOf(block);
// replace the (invalid) block with it's nodes
parent.nodes.splice(index, 1, ...block.nodes);
}
// get the location of the block on parent.nodes (block's siblings)
let parent = stack.at(-1);
let index = parent.nodes.indexOf(block);
// replace the (invalid) block with its nodes
parent.nodes.splice(index, 1, ...block.nodes);
} while (stack.length > 0);

push({ type: 'eos' });
return ast;
};

module.exports = parse;
}
12 changes: 12 additions & 0 deletions lib/validate-input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports.validateInput = (line, {maxSymbols}) => {
const symbols = {};

for (const current of line) {
symbols[current] = (symbols[current] || 0) + 1;
}

for (const [value, count] of Object.entries(symbols)) {
if (count > maxSymbols)
throw SyntaxError(`To many symbols '${value}'. Maximum: ${maxSymbols} allowed. Received: ${count}`);
}
};
10 changes: 10 additions & 0 deletions test/braces.parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ describe('braces.parse()', () => {
let MAX_LENGTH = 1024 * 64;
assert.throws(() => parse('.'.repeat(MAX_LENGTH + 2)));
});
it('should throw an error when symbols exceeds max symbols count default', () => {
let SYMBOLS= 1024;
assert.throws(() => parse('.'.repeat(MAX_SYMBOLS * 2)));
});
it('should throw an error when symbols exceeds max symbols count ', () => {
let SYMBOLS= 2;
assert.throws(() => parse('...', {
maxSymbols: 2,
}));
});
});

describe('valid', () => {
Expand Down