Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: vulnerability #37

Merged
merged 2 commits into from
May 17, 2024
Merged
Changes from 1 commit
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
54 changes: 33 additions & 21 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,30 +304,42 @@ 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)
continue;

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;
}
}

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;
}