Skip to content

fix: guard against proxy traps raising exceptions in REPL tokenizer #4457

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

Merged
merged 2 commits into from
Jan 2, 2025
Merged
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
37 changes: 25 additions & 12 deletions lib/node_modules/@stdlib/repl/lib/tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

var parse = require( 'acorn-loose' ).parse;
var walk = require( 'acorn-walk' );
var hasProp = require( '@stdlib/assert/has-property' );
var linkedList = require( '@stdlib/utils/linked-list' );
var contains = require( '@stdlib/array/base/assert/contains' );
var resolveLocalScopes = require( './resolve_local_scopes.js' );
Expand Down Expand Up @@ -173,7 +174,7 @@
*/
function onComment( block, text, start, end ) {
if ( block ) {
// TODO: add support for highlighting multi-line comments.

Check warning on line 177 in lib/node_modules/@stdlib/repl/lib/tokenizer.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected 'todo' comment: 'TODO: add support for highlighting...'
return;
}
tokens.push({
Expand Down Expand Up @@ -227,7 +228,7 @@
for ( i = 0; i < COMMANDS.length; i++ ) {
command = COMMANDS[ i ];
if ( node.name === command[ 0 ] ) {
tokens.push( {
tokens.push({
'value': node.name,
'type': 'command',
'start': node.start,
Expand All @@ -240,14 +241,14 @@
identifier = context[ node.name ];
if ( identifier ) {
if ( isLiteralType( typeof identifier ) ) {
tokens.push( {
tokens.push({
'value': node.name,
'type': 'variable',
'start': node.start,
'end': node.end
});
} else {
tokens.push( {
tokens.push({
'value': node.name,
'type': typeof identifier,
'start': node.start,
Expand Down Expand Up @@ -313,19 +314,27 @@
}
// Case: 'bar' in `foo['bar']` - property already pushed as a string token. Ignore...
if ( property.value.type === 'Literal' ) {
obj = obj[ property.value.value ];
if ( !obj ) {
// Property not found in context:
try {
if ( !hasProp( obj, property.value.value ) ) {
// Property not found in context:
break;
}
obj = obj[ property.value.value ];
} catch ( error ) { // eslint-disable-line no-unused-vars
break;
}
property = properties.next();
continue;
}
// Case: `foo.bar` - resolve property and push it as a token...
if ( property.value.type === 'Identifier' ) {
obj = obj[ property.value.name ];
if ( !obj ) {
// Property not found in context:
try {
if ( !hasProp( obj, property.value.name ) ) {
// Property not found in context:
break;
}
obj = obj[ property.value.name ];
} catch ( error ) { // eslint-disable-line no-unused-vars
break;
}
if ( !compute ) {
Expand Down Expand Up @@ -356,9 +365,13 @@
// Couldn't compute the internal `MemberExpression` into a definite name:
break;
}
obj = obj[ computed ];
if ( !obj ) {
// Property not found in context:
try {
if ( !hasProp( obj, computed ) ) {
// Property not found in context:
break;
}
obj = obj[ computed ];
} catch ( error ) { // eslint-disable-line no-unused-vars
break;
}
property = properties.next();
Expand Down
Loading