Skip to content

Commit a408cd7

Browse files
authored
Detect destructuring process.env and use the config-helper instead (#5901)
* Detect destructuring process.env and use the config-helper instead * Add missing configuration * Add two more missing configurations and rename testing env
1 parent 66dede6 commit a408cd7

File tree

13 files changed

+55
-27
lines changed

13 files changed

+55
-27
lines changed

eslint-rules/eslint-process-env.mjs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export default {
22
meta: {
33
type: 'problem',
44
docs: {
5-
description: 'Disallow usage of process.env outside config.js'
5+
description: 'Disallow usage of process.env outside config-helper.js'
66
},
77
schema: []
88
},
@@ -23,27 +23,52 @@ export default {
2323
return {
2424
// Handle direct member expressions: process.env.FOO
2525
MemberExpression (node) {
26-
if (node.object?.type === 'MemberExpression' &&
27-
isProcessEnvObject(node.object)) {
26+
// direct `process.env` or nested `process.env.FOO`
27+
if (isProcessEnvObject(node) ||
28+
node.object?.type === 'MemberExpression' && isProcessEnvObject(node.object)) {
2829
report(node)
2930
}
3031
},
3132

3233
// Handle destructuring: const { FOO } = process.env
3334
VariableDeclarator (node) {
34-
if (isProcessEnvObject(node.init)) {
35+
if (
36+
node.init?.type === 'MemberExpression' &&
37+
isProcessEnvObject(node.init) &&
38+
node.id.type === 'Identifier'
39+
) {
40+
// const env = process.env
41+
report(node)
42+
} else if (
43+
node.init?.type === 'Identifier' &&
44+
node.init.name === 'process' &&
45+
node.id.type === 'ObjectPattern'
46+
) {
47+
// const { env } = process
48+
for (const prop of node.id.properties) {
49+
if (prop.type === 'Property' && prop.key.name === 'env') {
50+
report(node)
51+
break
52+
}
53+
}
54+
}
55+
// const { FOO } = process.env
56+
if (
57+
node.init?.type === 'MemberExpression' &&
58+
isProcessEnvObject(node.init)
59+
) {
3560
report(node)
3661
}
3762
},
3863

39-
// Handle spread operator: { ...process.env }
64+
// Spread usage: { ...process.env } or { ...envAlias }
4065
SpreadElement (node) {
4166
if (isProcessEnvObject(node.argument)) {
4267
report(node)
4368
}
4469
},
4570

46-
// Handle any function call with process.env as an argument
71+
// Any function call receiving process.env
4772
CallExpression (node) {
4873
for (const arg of node.arguments) {
4974
if (isProcessEnvObject(arg)) {

packages/dd-trace/src/plugins/util/ci.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const {
2828
PR_NUMBER
2929
} = require('./tags')
3030
const { filterSensitiveInfoFromRepository } = require('./url')
31-
const { getEnvironmentVariable } = require('../../config-helper')
31+
const { getEnvironmentVariable, getEnvironmentVariables } = require('../../config-helper')
3232

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

9898
let tags = {}
9999

packages/dd-trace/src/supported-configurations.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"supportedConfigurations": {
33
"DD_AAS_DOTNET_EXTENSION_VERSION": ["A"],
4+
"DD_ACTION_EXECUTION_ID": ["A"],
45
"DD_AGENT_HOST": ["A"],
56
"DD_AGENTLESS_LOG_SUBMISSION_ENABLED": ["A"],
67
"DD_AGENTLESS_LOG_SUBMISSION_URL": ["A"],
@@ -49,6 +50,7 @@
4950
"DD_CODE_ORIGIN_FOR_SPANS_ENABLED": ["A"],
5051
"DD_CODE_ORIGIN_FOR_SPANS_EXPERIMENTAL_EXIT_SPANS_ENABLED": ["A"],
5152
"DD_CRASHTRACKING_ENABLED": ["A"],
53+
"DD_CUSTOM_TRACE_ID": ["A"],
5254
"DD_DATA_STREAMS_ENABLED": ["A"],
5355
"DD_DBM_PROPAGATION_MODE": ["A"],
5456
"DD_DOGSTATSD_HOST": ["A"],
@@ -108,6 +110,7 @@
108110
"DD_MINI_AGENT_PATH": ["A"],
109111
"DD_OPENAI_LOGS_ENABLED": ["A"],
110112
"DD_OPENAI_SPAN_CHAR_LIMIT": ["A"],
113+
"DD_PIPELINE_EXECUTION_ID": ["A"],
111114
"DD_PLAYWRIGHT_WORKER": ["A"],
112115
"DD_PROFILING_CODEHOTSPOTS_ENABLED": ["A"],
113116
"DD_PROFILING_CPU_ENABLED": ["A"],

packages/dd-trace/test/plugins/util/ci-env/azurepipelines.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@
658658
"BUILD_REQUESTEDFOREMAIL": "azure-pipelines-commit-author-email@datadoghq.com",
659659
"BUILD_REQUESTEDFORID": "azure-pipelines-commit-author",
660660
"BUILD_SOURCEVERSIONMESSAGE": "azure-pipelines-commit-message",
661-
"DD_TEST_CASE_NAME": "http-repository-url-no-git-suffix",
661+
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "http-repository-url-no-git-suffix",
662662
"SYSTEM_JOBID": "azure-pipelines-job-id",
663663
"SYSTEM_TASKINSTANCEID": "azure-pipelines-task-id",
664664
"SYSTEM_TEAMFOUNDATIONSERVERURI": "https://azure-pipelines-server-uri.com/",
@@ -687,7 +687,7 @@
687687
"BUILD_REQUESTEDFOREMAIL": "azure-pipelines-commit-author-email@datadoghq.com",
688688
"BUILD_REQUESTEDFORID": "azure-pipelines-commit-author",
689689
"BUILD_SOURCEVERSIONMESSAGE": "azure-pipelines-commit-message",
690-
"DD_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix",
690+
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix",
691691
"SYSTEM_JOBID": "azure-pipelines-job-id",
692692
"SYSTEM_TASKINSTANCEID": "azure-pipelines-task-id",
693693
"SYSTEM_TEAMFOUNDATIONSERVERURI": "https://azure-pipelines-server-uri.com/",

packages/dd-trace/test/plugins/util/ci-env/bitbucket.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@
407407
"BITBUCKET_GIT_HTTP_ORIGIN": "https://bitbucket.org/DataDog/dogweb",
408408
"BITBUCKET_PIPELINE_UUID": "{bitbucket-uuid}",
409409
"BITBUCKET_REPO_FULL_NAME": "bitbucket-repo",
410-
"DD_TEST_CASE_NAME": "http-repository-url-no-git-suffix"
410+
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "http-repository-url-no-git-suffix"
411411
},
412412
{
413413
"ci.job.url": "https://bitbucket.org/bitbucket-repo/addon/pipelines/home#!/results/bitbucket-build-num",
@@ -427,7 +427,7 @@
427427
"BITBUCKET_GIT_HTTP_ORIGIN": "ssh://host.xz:54321/path/to/repo/",
428428
"BITBUCKET_PIPELINE_UUID": "{bitbucket-uuid}",
429429
"BITBUCKET_REPO_FULL_NAME": "bitbucket-repo",
430-
"DD_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix"
430+
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix"
431431
},
432432
{
433433
"ci.job.url": "https://bitbucket.org/bitbucket-repo/addon/pipelines/home#!/results/bitbucket-build-num",

packages/dd-trace/test/plugins/util/ci-env/bitrise.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@
486486
"BITRISE_BUILD_URL": "https://bitrise-build-url.com//",
487487
"BITRISE_GIT_MESSAGE": "bitrise-git-commit-message",
488488
"BITRISE_TRIGGERED_WORKFLOW_ID": "bitrise-pipeline-name",
489-
"DD_TEST_CASE_NAME": "http-repository-url-no-git-suffix",
489+
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "http-repository-url-no-git-suffix",
490490
"GIT_CLONE_COMMIT_HASH": "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
491491
"GIT_REPOSITORY_URL": "https://github.com/DataDog/dogweb"
492492
},
@@ -508,7 +508,7 @@
508508
"BITRISE_BUILD_URL": "https://bitrise-build-url.com//",
509509
"BITRISE_GIT_MESSAGE": "bitrise-git-commit-message",
510510
"BITRISE_TRIGGERED_WORKFLOW_ID": "bitrise-pipeline-name",
511-
"DD_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix",
511+
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix",
512512
"GIT_CLONE_COMMIT_HASH": "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
513513
"GIT_REPOSITORY_URL": "ssh://host.xz:54321/path/to/repo/"
514514
},

packages/dd-trace/test/plugins/util/ci-env/buddy.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@
192192
"BUDDY_PIPELINE_ID": "456",
193193
"BUDDY_PIPELINE_NAME": "Deploy to Production",
194194
"BUDDY_SCM_URL": "https://github.com/buddyworks/my-project",
195-
"DD_TEST_CASE_NAME": "http-repository-url-no-git-suffix"
195+
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "http-repository-url-no-git-suffix"
196196
},
197197
{
198198
"ci.pipeline.id": "456/buddy-execution-id",
@@ -223,7 +223,7 @@
223223
"BUDDY_PIPELINE_ID": "456",
224224
"BUDDY_PIPELINE_NAME": "Deploy to Production",
225225
"BUDDY_SCM_URL": "ssh://host.xz:54321/path/to/repo/",
226-
"DD_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix"
226+
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix"
227227
},
228228
{
229229
"ci.pipeline.id": "456/buddy-execution-id",

packages/dd-trace/test/plugins/util/ci-env/buildkite.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@
728728
"BUILDKITE_PULL_REQUEST_BASE_BRANCH": "",
729729
"BUILDKITE_REPO": "https://github.com/DataDog/dogweb",
730730
"BUILDKITE_TAG": "",
731-
"DD_TEST_CASE_NAME": "http-repository-url-no-git-suffix"
731+
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "http-repository-url-no-git-suffix"
732732
},
733733
{
734734
"_dd.ci.env_vars": "{\"BUILDKITE_BUILD_ID\":\"buildkite-pipeline-id\",\"BUILDKITE_JOB_ID\":\"buildkite-job-id\"}",
@@ -762,7 +762,7 @@
762762
"BUILDKITE_PULL_REQUEST_BASE_BRANCH": "",
763763
"BUILDKITE_REPO": "ssh://host.xz:54321/path/to/repo/",
764764
"BUILDKITE_TAG": "",
765-
"DD_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix"
765+
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix"
766766
},
767767
{
768768
"_dd.ci.env_vars": "{\"BUILDKITE_BUILD_ID\":\"buildkite-pipeline-id\",\"BUILDKITE_JOB_ID\":\"buildkite-job-id\"}",

packages/dd-trace/test/plugins/util/ci-env/circleci.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@
529529
"CIRCLE_REPOSITORY_URL": "https://github.com/DataDog/dogweb",
530530
"CIRCLE_SHA1": "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
531531
"CIRCLE_WORKFLOW_ID": "circleci-pipeline-id",
532-
"DD_TEST_CASE_NAME": "http-repository-url-no-git-suffix"
532+
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "http-repository-url-no-git-suffix"
533533
},
534534
{
535535
"_dd.ci.env_vars": "{\"CIRCLE_WORKFLOW_ID\":\"circleci-pipeline-id\",\"CIRCLE_BUILD_NUM\":\"circleci-pipeline-number\"}",
@@ -553,7 +553,7 @@
553553
"CIRCLE_REPOSITORY_URL": "ssh://host.xz:54321/path/to/repo/",
554554
"CIRCLE_SHA1": "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
555555
"CIRCLE_WORKFLOW_ID": "circleci-pipeline-id",
556-
"DD_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix"
556+
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix"
557557
},
558558
{
559559
"_dd.ci.env_vars": "{\"CIRCLE_WORKFLOW_ID\":\"circleci-pipeline-id\",\"CIRCLE_BUILD_NUM\":\"circleci-pipeline-number\"}",

packages/dd-trace/test/plugins/util/ci-env/gitlab.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@
469469
"CI_PROJECT_PATH": "gitlab-pipeline-name",
470470
"CI_PROJECT_URL": "https://gitlab.com/repo",
471471
"CI_REPOSITORY_URL": "http://hostname.com/repo",
472-
"DD_TEST_CASE_NAME": "http-repository-url-no-git-suffix",
472+
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "http-repository-url-no-git-suffix",
473473
"GITLAB_CI": "gitlab"
474474
},
475475
{
@@ -510,7 +510,7 @@
510510
"CI_PROJECT_PATH": "gitlab-pipeline-name",
511511
"CI_PROJECT_URL": "https://gitlab.com/repo",
512512
"CI_REPOSITORY_URL": "ssh://host.xz:54321/path/to/repo/",
513-
"DD_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix",
513+
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix",
514514
"GITLAB_CI": "gitlab"
515515
},
516516
{

packages/dd-trace/test/plugins/util/ci-env/jenkins.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@
672672
"BUILD_TAG": "jenkins-pipeline-id",
673673
"BUILD_URL": "https://jenkins.com/pipeline",
674674
"DD_CUSTOM_TRACE_ID": "jenkins-custom-trace-id",
675-
"DD_TEST_CASE_NAME": "http-repository-url-no-git-suffix",
675+
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "http-repository-url-no-git-suffix",
676676
"GIT_COMMIT": "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
677677
"GIT_URL_1": "https://github.com/DataDog/dogweb",
678678
"JENKINS_URL": "jenkins",
@@ -694,7 +694,7 @@
694694
"BUILD_TAG": "jenkins-pipeline-id",
695695
"BUILD_URL": "https://jenkins.com/pipeline",
696696
"DD_CUSTOM_TRACE_ID": "jenkins-custom-trace-id",
697-
"DD_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix",
697+
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix",
698698
"GIT_COMMIT": "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
699699
"GIT_URL_1": "ssh://host.xz:54321/path/to/repo/",
700700
"JENKINS_URL": "jenkins",

packages/dd-trace/test/plugins/util/ci-env/usersupplied.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@
166166
"DD_GIT_COMMIT_MESSAGE": "usersupplied-message",
167167
"DD_GIT_COMMIT_SHA": "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
168168
"DD_GIT_REPOSITORY_URL": "https://github.com/DataDog/dogweb",
169-
"DD_TEST_CASE_NAME": "http-repository-url-no-git-suffix"
169+
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "http-repository-url-no-git-suffix"
170170
},
171171
{
172172
"git.commit.author.date": "usersupplied-authordate",
@@ -191,7 +191,7 @@
191191
"DD_GIT_COMMIT_MESSAGE": "usersupplied-message",
192192
"DD_GIT_COMMIT_SHA": "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
193193
"DD_GIT_REPOSITORY_URL": "ssh://host.xz:54321/path/to/repo/",
194-
"DD_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix"
194+
"TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME": "ssh-repository-url-no-git-suffix"
195195
},
196196
{
197197
"git.commit.author.date": "usersupplied-authordate",

packages/dd-trace/test/plugins/util/test-environment.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ describe('test environment data', () => {
8484
assertions.forEach(([env, expectedSpanTags], index) => {
8585
it(`reads env info for spec ${index} from ${ciProvider}`, () => {
8686
process.env = env
87-
const { DD_TEST_CASE_NAME: testCaseName } = env
87+
const { TESTING_TEST_OPTIMIZATION_TEST_CASE_NAME: testCaseName } = env
8888
const { [CI_ENV_VARS]: envVars, [CI_NODE_LABELS]: nodeLabels, ...restOfTags } = getTestEnvironmentMetadata()
8989
const {
9090
[CI_ENV_VARS]: expectedEnvVars,

0 commit comments

Comments
 (0)