Skip to content

Commit

Permalink
Add runtime guardrail for SSI
Browse files Browse the repository at this point in the history
When in SSI, bail out if our version of Node.js is incompatible.
  • Loading branch information
bengl committed May 16, 2024
1 parent 001f002 commit 7a88c4f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
11 changes: 11 additions & 0 deletions init.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ if (process.env.DD_INJECTION_ENABLED) {
initBailout = true
}
}

// If we're running via single-step install, and the runtime doesn't match
// the engines field in package.json, then we should not initialize the tracer.
if (!initBailout) {
const { engines } = require('./package.json')
const version = process.versions.node
const semver = require('semver')
if (!semver.satisfies(version, engines.node)) {
initBailout = true
}
}
}

if (!initBailout) {
Expand Down
47 changes: 35 additions & 12 deletions integration-tests/init.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,45 @@ describe('init.js', () => {
return sandbox.remove()
})

context('when dd-trace is not in the app dir', () => {
const NODE_OPTIONS = `--require ${path.join(__dirname, '..', 'init.js')}`
it('should initialize the tracer, if no DD_INJECTION_ENABLED', () => {
return runTest(cwd, { NODE_OPTIONS }, 'true\n')
context('preferring app-dir dd-trace', () => {
context('when dd-trace is not in the app dir', () => {
const NODE_OPTIONS = `--require ${path.join(__dirname, '..', 'init.js')}`
it('should initialize the tracer, if no DD_INJECTION_ENABLED', () => {
return runTest(cwd, { NODE_OPTIONS }, 'true\n')
})
it('should not initialize the tracer, if DD_INJECTION_ENABLED', () => {
return runTest(cwd, { NODE_OPTIONS, DD_INJECTION_ENABLED }, 'false\n')
})
})
it('should not initialize the tracer, if DD_INJECTION_ENABLED', () => {
return runTest(cwd, { NODE_OPTIONS, DD_INJECTION_ENABLED }, 'false\n')
context('when dd-trace in the app dir', () => {
const NODE_OPTIONS = '--require dd-trace/init.js'
it('should initialize the tracer, if no DD_INJECTION_ENABLED', () => {
return runTest(cwd, { NODE_OPTIONS }, 'true\n')
})
it('should initialize the tracer, if DD_INJECTION_ENABLED', () => {
return runTest(cwd, { NODE_OPTIONS, DD_INJECTION_ENABLED }, 'true\n')
})
})
})
context('when dd-trace in the app dir', () => {
const NODE_OPTIONS = '--require dd-trace/init.js'
it('should initialize the tracer, if no DD_INJECTION_ENABLED', () => {
return runTest(cwd, { NODE_OPTIONS }, 'true\n')
context('runtime version check', () => {
const NODE_OPTIONS = `--require ${path.join(__dirname, 'init', 'setversion.js')} --require dd-trace/init.js`
context('when node version is less than engines field', () => {
const FAKE_VERSION = '3.0.0'
it('should initialize the tracer, if no DD_INJECTION_ENABLED', () => {
return runTest(cwd, { NODE_OPTIONS, FAKE_VERSION }, 'true\n')
})
it('should not initialize the tracer, if DD_INJECTION_ENABLED', () => {
return runTest(cwd, { NODE_OPTIONS, DD_INJECTION_ENABLED, FAKE_VERSION }, 'false\n')
})
})
it('should initialize the tracer, if DD_INJECTION_ENABLED', () => {
return runTest(cwd, { NODE_OPTIONS, DD_INJECTION_ENABLED }, 'true\n')
context('when node version is more than engines field', () => {
const FAKE_VERSION = '50000.0.0'
it('should initialize the tracer, if no DD_INJECTION_ENABLED', () => {
return runTest(cwd, { NODE_OPTIONS, FAKE_VERSION }, 'true\n')
})
it('should initialize the tracer, if DD_INJECTION_ENABLED', () => {
return runTest(cwd, { NODE_OPTIONS, DD_INJECTION_ENABLED, FAKE_VERSION }, 'true\n')
})
})
})
})
7 changes: 7 additions & 0 deletions integration-tests/init/setversion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

const desc = Reflect.getOwnPropertyDescriptor(process.versions, 'node')

desc.value = process.env.FAKE_VERSION

Reflect.defineProperty(process.versions, 'node', desc)

0 comments on commit 7a88c4f

Please sign in to comment.