Skip to content

Detect destructuring process.env and use the config-helper instead #5901

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 5 commits into from
Jun 19, 2025
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
37 changes: 31 additions & 6 deletions eslint-rules/eslint-process-env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export default {
meta: {
type: 'problem',
docs: {
description: 'Disallow usage of process.env outside config.js'
description: 'Disallow usage of process.env outside config-helper.js'
},
schema: []
},
Expand All @@ -23,27 +23,52 @@ export default {
return {
// Handle direct member expressions: process.env.FOO
MemberExpression (node) {
if (node.object?.type === 'MemberExpression' &&
isProcessEnvObject(node.object)) {
// direct `process.env` or nested `process.env.FOO`
if (isProcessEnvObject(node) ||
node.object?.type === 'MemberExpression' && isProcessEnvObject(node.object)) {
report(node)
}
},

// Handle destructuring: const { FOO } = process.env
VariableDeclarator (node) {
if (isProcessEnvObject(node.init)) {
if (
node.init?.type === 'MemberExpression' &&
isProcessEnvObject(node.init) &&
node.id.type === 'Identifier'
) {
// const env = process.env
report(node)
} else if (
node.init?.type === 'Identifier' &&
node.init.name === 'process' &&
node.id.type === 'ObjectPattern'
) {
// const { env } = process
for (const prop of node.id.properties) {
if (prop.type === 'Property' && prop.key.name === 'env') {
report(node)
break
}
}
}
// const { FOO } = process.env
if (
node.init?.type === 'MemberExpression' &&
isProcessEnvObject(node.init)
) {
report(node)
}
},

// Handle spread operator: { ...process.env }
// Spread usage: { ...process.env } or { ...envAlias }
SpreadElement (node) {
if (isProcessEnvObject(node.argument)) {
report(node)
}
},

// Handle any function call with process.env as an argument
// Any function call receiving process.env
CallExpression (node) {
for (const arg of node.arguments) {
if (isProcessEnvObject(arg)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/dd-trace/src/plugins/util/ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const {
PR_NUMBER
} = require('./tags')
const { filterSensitiveInfoFromRepository } = require('./url')
const { getEnvironmentVariable } = require('../../config-helper')
const { getEnvironmentVariable, getEnvironmentVariables } = require('../../config-helper')

// Receives a string with the form 'John Doe <john.doe@gmail.com>'
// and returns { name: 'John Doe', email: 'john.doe@gmail.com' }
Expand Down Expand Up @@ -93,7 +93,7 @@ function getGitHubEventPayload () {
module.exports = {
normalizeRef,
getCIMetadata () {
const { env } = process
const env = getEnvironmentVariables()

let tags = {}

Expand Down
3 changes: 3 additions & 0 deletions packages/dd-trace/src/supported-configurations.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"supportedConfigurations": {
"DD_AAS_DOTNET_EXTENSION_VERSION": ["A"],
"DD_ACTION_EXECUTION_ID": ["A"],
"DD_AGENT_HOST": ["A"],
"DD_AGENTLESS_LOG_SUBMISSION_ENABLED": ["A"],
"DD_AGENTLESS_LOG_SUBMISSION_URL": ["A"],
Expand Down Expand Up @@ -49,6 +50,7 @@
"DD_CODE_ORIGIN_FOR_SPANS_ENABLED": ["A"],
"DD_CODE_ORIGIN_FOR_SPANS_EXPERIMENTAL_EXIT_SPANS_ENABLED": ["A"],
"DD_CRASHTRACKING_ENABLED": ["A"],
"DD_CUSTOM_TRACE_ID": ["A"],
"DD_DATA_STREAMS_ENABLED": ["A"],
"DD_DBM_PROPAGATION_MODE": ["A"],
"DD_DOGSTATSD_HOST": ["A"],
Expand Down Expand Up @@ -108,6 +110,7 @@
"DD_MINI_AGENT_PATH": ["A"],
"DD_OPENAI_LOGS_ENABLED": ["A"],
"DD_OPENAI_SPAN_CHAR_LIMIT": ["A"],
"DD_PIPELINE_EXECUTION_ID": ["A"],
"DD_PLAYWRIGHT_WORKER": ["A"],
"DD_PROFILING_CODEHOTSPOTS_ENABLED": ["A"],
"DD_PROFILING_CPU_ENABLED": ["A"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@
"BUILD_REQUESTEDFOREMAIL": "azure-pipelines-commit-author-email@datadoghq.com",
"BUILD_REQUESTEDFORID": "azure-pipelines-commit-author",
"BUILD_SOURCEVERSIONMESSAGE": "azure-pipelines-commit-message",
"DD_TEST_CASE_NAME": "http-repository-url-no-git-suffix",
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "http-repository-url-no-git-suffix",
"SYSTEM_JOBID": "azure-pipelines-job-id",
"SYSTEM_TASKINSTANCEID": "azure-pipelines-task-id",
"SYSTEM_TEAMFOUNDATIONSERVERURI": "https://azure-pipelines-server-uri.com/",
Expand Down Expand Up @@ -687,7 +687,7 @@
"BUILD_REQUESTEDFOREMAIL": "azure-pipelines-commit-author-email@datadoghq.com",
"BUILD_REQUESTEDFORID": "azure-pipelines-commit-author",
"BUILD_SOURCEVERSIONMESSAGE": "azure-pipelines-commit-message",
"DD_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix",
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix",
"SYSTEM_JOBID": "azure-pipelines-job-id",
"SYSTEM_TASKINSTANCEID": "azure-pipelines-task-id",
"SYSTEM_TEAMFOUNDATIONSERVERURI": "https://azure-pipelines-server-uri.com/",
Expand Down
4 changes: 2 additions & 2 deletions packages/dd-trace/test/plugins/util/ci-env/bitbucket.json
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@
"BITBUCKET_GIT_HTTP_ORIGIN": "https://bitbucket.org/DataDog/dogweb",
"BITBUCKET_PIPELINE_UUID": "{bitbucket-uuid}",
"BITBUCKET_REPO_FULL_NAME": "bitbucket-repo",
"DD_TEST_CASE_NAME": "http-repository-url-no-git-suffix"
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "http-repository-url-no-git-suffix"
},
{
"ci.job.url": "https://bitbucket.org/bitbucket-repo/addon/pipelines/home#!/results/bitbucket-build-num",
Expand All @@ -427,7 +427,7 @@
"BITBUCKET_GIT_HTTP_ORIGIN": "ssh://host.xz:54321/path/to/repo/",
"BITBUCKET_PIPELINE_UUID": "{bitbucket-uuid}",
"BITBUCKET_REPO_FULL_NAME": "bitbucket-repo",
"DD_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix"
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix"
},
{
"ci.job.url": "https://bitbucket.org/bitbucket-repo/addon/pipelines/home#!/results/bitbucket-build-num",
Expand Down
4 changes: 2 additions & 2 deletions packages/dd-trace/test/plugins/util/ci-env/bitrise.json
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@
"BITRISE_BUILD_URL": "https://bitrise-build-url.com//",
"BITRISE_GIT_MESSAGE": "bitrise-git-commit-message",
"BITRISE_TRIGGERED_WORKFLOW_ID": "bitrise-pipeline-name",
"DD_TEST_CASE_NAME": "http-repository-url-no-git-suffix",
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "http-repository-url-no-git-suffix",
"GIT_CLONE_COMMIT_HASH": "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
"GIT_REPOSITORY_URL": "https://github.com/DataDog/dogweb"
},
Expand All @@ -508,7 +508,7 @@
"BITRISE_BUILD_URL": "https://bitrise-build-url.com//",
"BITRISE_GIT_MESSAGE": "bitrise-git-commit-message",
"BITRISE_TRIGGERED_WORKFLOW_ID": "bitrise-pipeline-name",
"DD_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix",
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix",
"GIT_CLONE_COMMIT_HASH": "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
"GIT_REPOSITORY_URL": "ssh://host.xz:54321/path/to/repo/"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/dd-trace/test/plugins/util/ci-env/buddy.json
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@
"BUDDY_PIPELINE_ID": "456",
"BUDDY_PIPELINE_NAME": "Deploy to Production",
"BUDDY_SCM_URL": "https://github.com/buddyworks/my-project",
"DD_TEST_CASE_NAME": "http-repository-url-no-git-suffix"
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "http-repository-url-no-git-suffix"
},
{
"ci.pipeline.id": "456/buddy-execution-id",
Expand Down Expand Up @@ -223,7 +223,7 @@
"BUDDY_PIPELINE_ID": "456",
"BUDDY_PIPELINE_NAME": "Deploy to Production",
"BUDDY_SCM_URL": "ssh://host.xz:54321/path/to/repo/",
"DD_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix"
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix"
},
{
"ci.pipeline.id": "456/buddy-execution-id",
Expand Down
4 changes: 2 additions & 2 deletions packages/dd-trace/test/plugins/util/ci-env/buildkite.json
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@
"BUILDKITE_PULL_REQUEST_BASE_BRANCH": "",
"BUILDKITE_REPO": "https://github.com/DataDog/dogweb",
"BUILDKITE_TAG": "",
"DD_TEST_CASE_NAME": "http-repository-url-no-git-suffix"
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "http-repository-url-no-git-suffix"
},
{
"_dd.ci.env_vars": "{\"BUILDKITE_BUILD_ID\":\"buildkite-pipeline-id\",\"BUILDKITE_JOB_ID\":\"buildkite-job-id\"}",
Expand Down Expand Up @@ -762,7 +762,7 @@
"BUILDKITE_PULL_REQUEST_BASE_BRANCH": "",
"BUILDKITE_REPO": "ssh://host.xz:54321/path/to/repo/",
"BUILDKITE_TAG": "",
"DD_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix"
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix"
},
{
"_dd.ci.env_vars": "{\"BUILDKITE_BUILD_ID\":\"buildkite-pipeline-id\",\"BUILDKITE_JOB_ID\":\"buildkite-job-id\"}",
Expand Down
4 changes: 2 additions & 2 deletions packages/dd-trace/test/plugins/util/ci-env/circleci.json
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@
"CIRCLE_REPOSITORY_URL": "https://github.com/DataDog/dogweb",
"CIRCLE_SHA1": "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
"CIRCLE_WORKFLOW_ID": "circleci-pipeline-id",
"DD_TEST_CASE_NAME": "http-repository-url-no-git-suffix"
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "http-repository-url-no-git-suffix"
},
{
"_dd.ci.env_vars": "{\"CIRCLE_WORKFLOW_ID\":\"circleci-pipeline-id\",\"CIRCLE_BUILD_NUM\":\"circleci-pipeline-number\"}",
Expand All @@ -553,7 +553,7 @@
"CIRCLE_REPOSITORY_URL": "ssh://host.xz:54321/path/to/repo/",
"CIRCLE_SHA1": "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
"CIRCLE_WORKFLOW_ID": "circleci-pipeline-id",
"DD_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix"
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix"
},
{
"_dd.ci.env_vars": "{\"CIRCLE_WORKFLOW_ID\":\"circleci-pipeline-id\",\"CIRCLE_BUILD_NUM\":\"circleci-pipeline-number\"}",
Expand Down
4 changes: 2 additions & 2 deletions packages/dd-trace/test/plugins/util/ci-env/gitlab.json
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@
"CI_PROJECT_PATH": "gitlab-pipeline-name",
"CI_PROJECT_URL": "https://gitlab.com/repo",
"CI_REPOSITORY_URL": "http://hostname.com/repo",
"DD_TEST_CASE_NAME": "http-repository-url-no-git-suffix",
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "http-repository-url-no-git-suffix",
"GITLAB_CI": "gitlab"
},
{
Expand Down Expand Up @@ -510,7 +510,7 @@
"CI_PROJECT_PATH": "gitlab-pipeline-name",
"CI_PROJECT_URL": "https://gitlab.com/repo",
"CI_REPOSITORY_URL": "ssh://host.xz:54321/path/to/repo/",
"DD_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix",
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix",
"GITLAB_CI": "gitlab"
},
{
Expand Down
4 changes: 2 additions & 2 deletions packages/dd-trace/test/plugins/util/ci-env/jenkins.json
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@
"BUILD_TAG": "jenkins-pipeline-id",
"BUILD_URL": "https://jenkins.com/pipeline",
"DD_CUSTOM_TRACE_ID": "jenkins-custom-trace-id",
"DD_TEST_CASE_NAME": "http-repository-url-no-git-suffix",
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "http-repository-url-no-git-suffix",
"GIT_COMMIT": "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
"GIT_URL_1": "https://github.com/DataDog/dogweb",
"JENKINS_URL": "jenkins",
Expand All @@ -694,7 +694,7 @@
"BUILD_TAG": "jenkins-pipeline-id",
"BUILD_URL": "https://jenkins.com/pipeline",
"DD_CUSTOM_TRACE_ID": "jenkins-custom-trace-id",
"DD_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix",
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix",
"GIT_COMMIT": "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
"GIT_URL_1": "ssh://host.xz:54321/path/to/repo/",
"JENKINS_URL": "jenkins",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@
"DD_GIT_COMMIT_MESSAGE": "usersupplied-message",
"DD_GIT_COMMIT_SHA": "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
"DD_GIT_REPOSITORY_URL": "https://github.com/DataDog/dogweb",
"DD_TEST_CASE_NAME": "http-repository-url-no-git-suffix"
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "http-repository-url-no-git-suffix"
},
{
"git.commit.author.date": "usersupplied-authordate",
Expand All @@ -191,7 +191,7 @@
"DD_GIT_COMMIT_MESSAGE": "usersupplied-message",
"DD_GIT_COMMIT_SHA": "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
"DD_GIT_REPOSITORY_URL": "ssh://host.xz:54321/path/to/repo/",
"DD_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix"
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix"
},
{
"git.commit.author.date": "usersupplied-authordate",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('test environment data', () => {
assertions.forEach(([env, expectedSpanTags], index) => {
it(`reads env info for spec ${index} from ${ciProvider}`, () => {
process.env = env
const { DD_TEST_CASE_NAME: testCaseName } = env
const { TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME: testCaseName } = env
const { [CI_ENV_VARS]: envVars, [CI_NODE_LABELS]: nodeLabels, ...restOfTags } = getTestEnvironmentMetadata()
const {
[CI_ENV_VARS]: expectedEnvVars,
Expand Down
Loading