Skip to content

Commit 639b250

Browse files
ericlazjuan-fernandez
authored andcommitted
validate CI tags before sending them (#3310)
* ciapp-3571: add url validator * ciapp-3571: add url validator and change tests from ci-spec * fix variables to follow camelcase * Remove valid import Co-authored-by: Juan Antonio Fernández de Alba <[email protected]> * Remove unnecessary check Co-authored-by: Juan Antonio Fernández de Alba <[email protected]> * Remove unnecessary check for CI_PIPELINE_URL Co-authored-by: Juan Antonio Fernández de Alba <[email protected]> * Format typo Co-authored-by: Juan Antonio Fernández de Alba <[email protected]> * Use validateMetadata return directly Co-authored-by: Juan Antonio Fernández de Alba <[email protected]> * use new ci-spec JSONs * fix spacing and sort imports * add test to validateMetadata * Update JSONs and refactor URL checking * Update JSONs and refactor URL checking * Add URL validation tests * Apply suggestions from code review Co-authored-by: Juan Antonio Fernández de Alba <[email protected]> * Add tag to test suite --------- Co-authored-by: Juan Antonio Fernández de Alba <[email protected]>
1 parent b13412f commit 639b250

File tree

15 files changed

+644
-574
lines changed

15 files changed

+644
-574
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function filterSensitiveInfoFromRepository (repositoryUrl) {
7777

7878
return `${protocol}//${hostname}${pathname}`
7979
} catch (e) {
80-
return repositoryUrl
80+
return ''
8181
}
8282
}
8383

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
const path = require('path')
22
const fs = require('fs')
3+
const { URL } = require('url')
4+
const log = require('../../log')
35

46
const istanbul = require('istanbul-lib-coverage')
57
const ignore = require('ignore')
68

79
const { getGitMetadata } = require('./git')
8-
const { getUserProviderGitMetadata } = require('./user-provided-git')
10+
const { getUserProviderGitMetadata, validateGitRepositoryUrl, validateGitCommitSha } = require('./user-provided-git')
911
const { getCIMetadata } = require('./ci')
1012
const { getRuntimeAndOSMetadata } = require('./env')
1113
const {
@@ -16,7 +18,8 @@ const {
1618
GIT_COMMIT_AUTHOR_EMAIL,
1719
GIT_COMMIT_AUTHOR_NAME,
1820
GIT_COMMIT_MESSAGE,
19-
CI_WORKSPACE_PATH
21+
CI_WORKSPACE_PATH,
22+
CI_PIPELINE_URL
2023
} = require('./tags')
2124
const id = require('../../id')
2225

@@ -104,7 +107,8 @@ module.exports = {
104107
mergeCoverage,
105108
fromCoverageMapToCoverage,
106109
getTestLineStart,
107-
getCallSites
110+
getCallSites,
111+
removeInvalidMetadata
108112
}
109113

110114
// Returns pkg manager and its version, separated by '-', e.g. npm-8.15.0 or yarn-1.22.19
@@ -116,6 +120,39 @@ function getPkgManager () {
116120
}
117121
}
118122

123+
function validateUrl (url) {
124+
try {
125+
const urlObject = new URL(url)
126+
return (urlObject.protocol === 'https:' || urlObject.protocol === 'http:')
127+
} catch (e) {
128+
return false
129+
}
130+
}
131+
132+
function removeInvalidMetadata (metadata) {
133+
return Object.keys(metadata).reduce((filteredTags, tag) => {
134+
if (tag === GIT_REPOSITORY_URL) {
135+
if (!validateGitRepositoryUrl(metadata[GIT_REPOSITORY_URL])) {
136+
log.error('DD_GIT_REPOSITORY_URL must be a valid URL')
137+
return filteredTags
138+
}
139+
}
140+
if (tag === GIT_COMMIT_SHA) {
141+
if (!validateGitCommitSha(metadata[GIT_COMMIT_SHA])) {
142+
log.error('DD_GIT_COMMIT_SHA must be a full-length git SHA')
143+
return filteredTags
144+
}
145+
}
146+
if (tag === CI_PIPELINE_URL) {
147+
if (!validateUrl(metadata[CI_PIPELINE_URL])) {
148+
return filteredTags
149+
}
150+
}
151+
filteredTags[tag] = metadata[tag]
152+
return filteredTags
153+
}, {})
154+
}
155+
119156
function getTestEnvironmentMetadata (testFramework, config) {
120157
// TODO: eventually these will come from the tracer (generally available)
121158
const ciMetadata = getCIMetadata()
@@ -155,7 +192,7 @@ function getTestEnvironmentMetadata (testFramework, config) {
155192
if (config && config.service) {
156193
metadata['service.name'] = config.service
157194
}
158-
return metadata
195+
return removeInvalidMetadata(metadata)
159196
}
160197

161198
function getTestParametersString (parametersByTestName, testName) {

packages/dd-trace/src/plugins/util/user-provided-git.js

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const {
1313
} = require('./tags')
1414

1515
const { normalizeRef } = require('./ci')
16-
const log = require('../../log')
1716
const { URL } = require('url')
1817

1918
function removeEmptyValues (tags) {
@@ -53,25 +52,6 @@ function validateGitCommitSha (gitCommitSha) {
5352
return isValidSha1 || isValidSha256
5453
}
5554

56-
function removeInvalidGitMetadata (metadata) {
57-
return Object.keys(metadata).reduce((filteredTags, tag) => {
58-
if (tag === GIT_REPOSITORY_URL) {
59-
if (!validateGitRepositoryUrl(metadata[GIT_REPOSITORY_URL])) {
60-
log.error('DD_GIT_REPOSITORY_URL must be a valid URL')
61-
return filteredTags
62-
}
63-
}
64-
if (tag === GIT_COMMIT_SHA) {
65-
if (!validateGitCommitSha(metadata[GIT_COMMIT_SHA])) {
66-
log.error('DD_GIT_COMMIT_SHA must be a full-length git SHA')
67-
return filteredTags
68-
}
69-
}
70-
filteredTags[tag] = metadata[tag]
71-
return filteredTags
72-
}, {})
73-
}
74-
7555
function getUserProviderGitMetadata () {
7656
const {
7757
DD_GIT_COMMIT_SHA,
@@ -95,7 +75,7 @@ function getUserProviderGitMetadata () {
9575
tag = normalizeRef(DD_GIT_BRANCH)
9676
}
9777

98-
const metadata = removeEmptyValues({
78+
return removeEmptyValues({
9979
[GIT_COMMIT_SHA]: DD_GIT_COMMIT_SHA,
10080
[GIT_BRANCH]: branch,
10181
[GIT_REPOSITORY_URL]: filterSensitiveInfoFromRepository(DD_GIT_REPOSITORY_URL),
@@ -108,7 +88,6 @@ function getUserProviderGitMetadata () {
10888
[GIT_COMMIT_AUTHOR_EMAIL]: DD_GIT_COMMIT_AUTHOR_EMAIL,
10989
[GIT_COMMIT_AUTHOR_DATE]: DD_GIT_COMMIT_AUTHOR_DATE
11090
})
111-
return removeInvalidGitMetadata(metadata)
11291
}
11392

11493
module.exports = { getUserProviderGitMetadata, validateGitRepositoryUrl, validateGitCommitSha }

0 commit comments

Comments
 (0)