|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { test } from 'tap' |
| 7 | +import { execFileSync } from 'node:child_process' |
| 8 | + |
| 9 | +const { buildLookup } = require('../integration/test-builder.js') as { |
| 10 | + buildLookup: (path: string) => string |
| 11 | +} |
| 12 | + |
| 13 | +function evalLookup (path: string, body: unknown): unknown { |
| 14 | + const expr = buildLookup(path) |
| 15 | + const script = `const response = { body: ${JSON.stringify(body)} }; process.stdout.write(JSON.stringify(${expr}))` |
| 16 | + return JSON.parse(execFileSync(process.execPath, ['-e', script], { encoding: 'utf8' })) |
| 17 | +} |
| 18 | + |
| 19 | +function assertValidJs (t: { doesNotThrow: (fn: () => void) => void }, path: string): void { |
| 20 | + const expr = buildLookup(path) |
| 21 | + t.doesNotThrow(() => execFileSync(process.execPath, ['-e', `const response = {body:{}}; ${expr}`])) |
| 22 | +} |
| 23 | + |
| 24 | +test('escaped dots stay one key', t => { |
| 25 | + t.equal(buildLookup('logs\\.otel.enabled'), 'response.body?.["logs.otel"]?.["enabled"]') |
| 26 | + t.equal(evalLookup('logs\\.otel.enabled', { 'logs.otel': { enabled: true } }), true) |
| 27 | + t.end() |
| 28 | +}) |
| 29 | + |
| 30 | +test('plain dotted path still splits', t => { |
| 31 | + t.equal(evalLookup('foo.bar', { foo: { bar: 1 } }), 1) |
| 32 | + t.end() |
| 33 | +}) |
| 34 | + |
| 35 | +test('numeric path segment is an index', t => { |
| 36 | + t.equal(evalLookup('0.name', [{ name: 'a' }]), 'a') |
| 37 | + t.end() |
| 38 | +}) |
| 39 | + |
| 40 | +test('$body is the raw body', t => { |
| 41 | + t.match(buildLookup('$body'), /response\.body/) |
| 42 | + t.end() |
| 43 | +}) |
| 44 | + |
| 45 | +test('quote and slash in a key stay valid js', t => { |
| 46 | + t.equal(buildLookup("foo.bar'baz"), 'response.body?.["foo"]?.["bar\'baz"]') |
| 47 | + t.equal(evalLookup("foo.bar'baz", { foo: { "bar'baz": 2 } }), 2) |
| 48 | + assertValidJs(t, 'logs\\.otel.enabled') |
| 49 | + t.end() |
| 50 | +}) |
| 51 | + |
| 52 | +test('adversarial path segments stay valid js', t => { |
| 53 | + assertValidJs(t, '../') |
| 54 | + assertValidJs(t, '?#') |
| 55 | + t.equal(evalLookup('?#', { '?#': 4 }), 4) |
| 56 | + t.same(evalLookup('', { x: 1 }), { x: 1 }) |
| 57 | + t.end() |
| 58 | +}) |
0 commit comments