|
| 1 | +/// |
| 2 | +/// Copyright © 2016-2026 The Thingsboard Authors |
| 3 | +/// |
| 4 | +/// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +/// you may not use this file except in compliance with the License. |
| 6 | +/// You may obtain a copy of the License at |
| 7 | +/// |
| 8 | +/// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +/// |
| 10 | +/// Unless required by applicable law or agreed to in writing, software |
| 11 | +/// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +/// See the License for the specific language governing permissions and |
| 14 | +/// limitations under the License. |
| 15 | +/// |
| 16 | + |
| 17 | +import { describe, test } from 'node:test'; |
| 18 | +import assert from 'node:assert/strict'; |
| 19 | +import { JsExecutor } from '../api/jsExecutor'; |
| 20 | + |
| 21 | +// describe('js-executor') groups all cases under <testsuite name="js-executor"> |
| 22 | +// in the JUnit XML so they show up under that suite in TeamCity's Tests tab, |
| 23 | +// alongside thousands of Java tests. |
| 24 | +describe('js-executor', () => { |
| 25 | + |
| 26 | +test('sandbox isolates args from host realm (JVN#16937365)', async () => { |
| 27 | + const exec = new JsExecutor(true); |
| 28 | + const script = await exec.compileScript(`function(msg, metadata, msgType){ |
| 29 | + var F = args.constructor.constructor; |
| 30 | + var p = F("return process")(); |
| 31 | + return p && p.mainModule ? 'reached-host' : 'isolated'; |
| 32 | + }`); |
| 33 | + await assert.rejects( |
| 34 | + exec.executeScript(script, ['{}', '{}', 'POST_TELEMETRY_REQUEST'], 5000), |
| 35 | + /process is not defined/, |
| 36 | + 'host process must not be reachable from inside the sandbox', |
| 37 | + ); |
| 38 | +}); |
| 39 | + |
| 40 | +test('sandbox passes string args through unchanged', async () => { |
| 41 | + const exec = new JsExecutor(true); |
| 42 | + const script = await exec.compileScript(`function(msg, metadata, msgType){ |
| 43 | + return { msgIsString: typeof msg === 'string', count: args.length, first: args[0] }; |
| 44 | + }`); |
| 45 | + const out = await exec.executeScript(script, ['hello', '{}', 'X'], 5000); |
| 46 | + // Field-by-field: the returned object is owned by the sandbox realm, so |
| 47 | + // its prototype is not the host Object.prototype and deepStrictEqual would |
| 48 | + // reject it on prototype mismatch even when the values match. |
| 49 | + assert.equal(out.msgIsString, true); |
| 50 | + assert.equal(out.count, 3); |
| 51 | + assert.equal(out.first, 'hello'); |
| 52 | +}); |
| 53 | + |
| 54 | +// The use_sandbox=false path is intentionally non-isolating: scripts compile |
| 55 | +// and run in the host realm via vm.compileFunction. The two tests below codify |
| 56 | +// that documented contract so any future behavior change shows up as a test |
| 57 | +// failure and forces a deliberate update of the docs and threat model. |
| 58 | + |
| 59 | +test('non-sandbox path does not isolate from host realm (documented contract)', async () => { |
| 60 | + const exec = new JsExecutor(false); |
| 61 | + const script = await exec.compileScript(`function(msg, metadata, msgType){ |
| 62 | + // Non-destructive host-reach probe: typeof process.platform is 'string' |
| 63 | + // only if the host process object is reachable. |
| 64 | + var F = args.constructor.constructor; |
| 65 | + return F('return typeof process.platform')(); |
| 66 | + }`); |
| 67 | + const out = await exec.executeScript(script, ['{}', '{}', 'X']); |
| 68 | + assert.equal(out, 'string', |
| 69 | + 'use_sandbox=false is documented as non-isolating; if this fails, the path was changed and docs/threat model must be updated'); |
| 70 | +}); |
| 71 | + |
| 72 | +test('non-sandbox path passes string args through unchanged', async () => { |
| 73 | + const exec = new JsExecutor(false); |
| 74 | + const script = await exec.compileScript(`function(msg, metadata, msgType){ |
| 75 | + return { msgIsString: typeof msg === 'string', count: args.length, first: args[0] }; |
| 76 | + }`); |
| 77 | + const out = await exec.executeScript(script, ['hello', '{}', 'X']); |
| 78 | + assert.equal(out.msgIsString, true); |
| 79 | + assert.equal(out.count, 3); |
| 80 | + assert.equal(out.first, 'hello'); |
| 81 | +}); |
| 82 | + |
| 83 | +}); // describe('js-executor') |
0 commit comments