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

Trailing comment in sandbox expression shouldn't breake it #327

Merged
merged 1 commit into from
Jan 30, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Trailing comments in comlink script expressions broke sandbox evaluation

## [2.2.0] - 2023-01-02
### Added
Expand Down
14 changes: 14 additions & 0 deletions src/core/interpreter/sandbox/sandbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,20 @@ describe('sandbox', () => {
expect(Array.isArray((v as { b: unknown }).b)).toBe(true);
});

it('correctly works with line comment', () => {
expect(evalScript(config, '34; // this is number 34')).toStrictEqual(34);
});

it('errors with unclosed block comment', () => {
expect(() => evalScript(config, '34; /* this is number 34')).toThrow(
'Invalid or unexpected token'
);
});

it('correctly works without semicolon', () => {
expect(evalScript(config, '34')).toStrictEqual(34);
});

describe('stdlib', () => {
it('provides unstable stdlib', () => {
expect(
Expand Down
5 changes: 4 additions & 1 deletion src/core/interpreter/sandbox/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ export function evalScript(

log?.('Evaluating:', js);
const result = vm.run(
`'use strict';const vmResult = ${js};vmResult`
`
'use strict';
const vmResult = ${js}
;vmResult`
) as unknown;
const resultVm2Fixed = vm2ExtraArrayKeysFixup(result);

Expand Down