-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace manual.keep tag usage with an specific method to keep the tra…
…ce (#4739) * Allow to set sampling priority * Span.keep method * Replace manual.keep tag usage with Span.keep() * Update standalone integration tests * use PrioritySampler.keepTrace * Lint * PrioritySampler.keepTrace test
- Loading branch information
Showing
10 changed files
with
192 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,8 @@ const { addVulnerability, sendVulnerabilities, clearCache, start, stop } = | |
require('../../../src/appsec/iast/vulnerability-reporter') | ||
const VulnerabilityAnalyzer = require('../../../../dd-trace/src/appsec/iast/analyzers/vulnerability-analyzer') | ||
const appsecStandalone = require('../../../src/appsec/standalone') | ||
const { APPSEC_PROPAGATION_KEY } = require('../../../src/constants') | ||
const { APPSEC_PROPAGATION_KEY, SAMPLING_MECHANISM_APPSEC } = require('../../../src/constants') | ||
const { USER_KEEP } = require('../../../../../ext/priority') | ||
|
||
describe('vulnerability-reporter', () => { | ||
let vulnerabilityAnalyzer | ||
|
@@ -82,9 +83,14 @@ describe('vulnerability-reporter', () => { | |
describe('without rootSpan', () => { | ||
let fakeTracer | ||
let onTheFlySpan | ||
let prioritySampler | ||
|
||
beforeEach(() => { | ||
prioritySampler = { | ||
setPriority: sinon.stub() | ||
} | ||
onTheFlySpan = { | ||
_prioritySampler: prioritySampler, | ||
finish: sinon.spy(), | ||
addTags: sinon.spy(), | ||
context () { | ||
|
@@ -120,10 +126,11 @@ describe('vulnerability-reporter', () => { | |
'_dd.iast.enabled': 1 | ||
}) | ||
expect(onTheFlySpan.addTags.secondCall).to.have.been.calledWithExactly({ | ||
'manual.keep': 'true', | ||
'_dd.iast.json': '{"sources":[],"vulnerabilities":[{"type":"INSECURE_HASHING","hash":3410512655,' + | ||
'"evidence":{"value":"sha1"},"location":{"spanId":42,"path":"filename.js","line":73}}]}' | ||
}) | ||
expect(prioritySampler.setPriority) | ||
.to.have.been.calledOnceWithExactly(onTheFlySpan, USER_KEEP, SAMPLING_MECHANISM_APPSEC) | ||
expect(onTheFlySpan.finish).to.have.been.calledOnce | ||
}) | ||
|
||
|
@@ -140,10 +147,15 @@ describe('vulnerability-reporter', () => { | |
describe('sendVulnerabilities', () => { | ||
let span | ||
let context | ||
let prioritySampler | ||
|
||
beforeEach(() => { | ||
context = { _trace: { tags: {} } } | ||
prioritySampler = { | ||
setPriority: sinon.stub() | ||
} | ||
span = { | ||
_prioritySampler: prioritySampler, | ||
addTags: sinon.stub(), | ||
context: sinon.stub().returns(context) | ||
} | ||
|
@@ -178,10 +190,10 @@ describe('vulnerability-reporter', () => { | |
vulnerabilityAnalyzer._createVulnerability('INSECURE_HASHING', { value: 'sha1' }, 888)) | ||
sendVulnerabilities(iastContext.vulnerabilities, span) | ||
expect(span.addTags).to.have.been.calledOnceWithExactly({ | ||
'manual.keep': 'true', | ||
'_dd.iast.json': '{"sources":[],"vulnerabilities":[{"type":"INSECURE_HASHING","hash":3254801297,' + | ||
'"evidence":{"value":"sha1"},"location":{"spanId":888}}]}' | ||
}) | ||
expect(prioritySampler.setPriority).to.have.been.calledOnceWithExactly(span, USER_KEEP, SAMPLING_MECHANISM_APPSEC) | ||
}) | ||
|
||
it('should send only valid vulnerabilities', () => { | ||
|
@@ -191,10 +203,10 @@ describe('vulnerability-reporter', () => { | |
iastContext.vulnerabilities.push({ invalid: 'vulnerability' }) | ||
sendVulnerabilities(iastContext.vulnerabilities, span) | ||
expect(span.addTags).to.have.been.calledOnceWithExactly({ | ||
'manual.keep': 'true', | ||
'_dd.iast.json': '{"sources":[],"vulnerabilities":[{"type":"INSECURE_HASHING","hash":3254801297,' + | ||
'"evidence":{"value":"sha1"},"location":{"spanId":888}}]}' | ||
}) | ||
expect(prioritySampler.setPriority).to.have.been.calledOnceWithExactly(span, USER_KEEP, SAMPLING_MECHANISM_APPSEC) | ||
}) | ||
|
||
it('should send vulnerabilities with evidence, ranges and sources', () => { | ||
|
@@ -239,7 +251,6 @@ describe('vulnerability-reporter', () => { | |
|
||
sendVulnerabilities(iastContext.vulnerabilities, span) | ||
expect(span.addTags).to.have.been.calledOnceWithExactly({ | ||
'manual.keep': 'true', | ||
'_dd.iast.json': '{"sources":[{"origin":"ORIGIN_TYPE_1","name":"PARAMETER_NAME_1","value":"joe"},' + | ||
'{"origin":"ORIGIN_TYPE_2","name":"PARAMETER_NAME_2","value":"[email protected]"}],' + | ||
'"vulnerabilities":[{"type":"SQL_INJECTION","hash":4676753086,' + | ||
|
@@ -249,6 +260,7 @@ describe('vulnerability-reporter', () => { | |
'[{"value":"SELECT id FROM u WHERE email = \'"},{"value":"[email protected]","source":1},{"value":"\';"}]},' + | ||
'"location":{"spanId":888,"path":"filename.js","line":99}}]}' | ||
}) | ||
expect(prioritySampler.setPriority).to.have.been.calledOnceWithExactly(span, USER_KEEP, SAMPLING_MECHANISM_APPSEC) | ||
}) | ||
|
||
it('should send multiple vulnerabilities with same tainted source', () => { | ||
|
@@ -293,7 +305,6 @@ describe('vulnerability-reporter', () => { | |
|
||
sendVulnerabilities(iastContext.vulnerabilities, span) | ||
expect(span.addTags).to.have.been.calledOnceWithExactly({ | ||
'manual.keep': 'true', | ||
'_dd.iast.json': '{"sources":[{"origin":"ORIGIN_TYPE_1","name":"PARAMETER_NAME_1","value":"joe"}],' + | ||
'"vulnerabilities":[{"type":"SQL_INJECTION","hash":4676753086,' + | ||
'"evidence":{"valueParts":[{"value":"SELECT * FROM u WHERE name = \'"},{"value":"joe","source":0},' + | ||
|
@@ -302,6 +313,7 @@ describe('vulnerability-reporter', () => { | |
'[{"value":"UPDATE u SET name=\'"},{"value":"joe","source":0},{"value":"\' WHERE id=1;"}]},' + | ||
'"location":{"spanId":888,"path":"filename.js","line":99}}]}' | ||
}) | ||
expect(prioritySampler.setPriority).to.have.been.calledOnceWithExactly(span, USER_KEEP, SAMPLING_MECHANISM_APPSEC) | ||
}) | ||
|
||
it('should send once with multiple vulnerabilities', () => { | ||
|
@@ -314,7 +326,6 @@ describe('vulnerability-reporter', () => { | |
{ path: '/path/to/file3.js', line: 3 })) | ||
sendVulnerabilities(iastContext.vulnerabilities, span) | ||
expect(span.addTags).to.have.been.calledOnceWithExactly({ | ||
'manual.keep': 'true', | ||
'_dd.iast.json': '{"sources":[],"vulnerabilities":[' + | ||
'{"type":"INSECURE_HASHING","hash":1697980169,"evidence":{"value":"sha1"},' + | ||
'"location":{"spanId":888,"path":"/path/to/file1.js","line":1}},' + | ||
|
@@ -323,6 +334,7 @@ describe('vulnerability-reporter', () => { | |
'{"type":"INSECURE_HASHING","hash":1755238473,"evidence":{"value":"md5"},' + | ||
'"location":{"spanId":-5,"path":"/path/to/file3.js","line":3}}]}' | ||
}) | ||
expect(prioritySampler.setPriority).to.have.been.calledOnceWithExactly(span, USER_KEEP, SAMPLING_MECHANISM_APPSEC) | ||
}) | ||
|
||
it('should send once vulnerability with one vulnerability', () => { | ||
|
@@ -332,10 +344,10 @@ describe('vulnerability-reporter', () => { | |
{ path: 'filename.js', line: 88 })) | ||
sendVulnerabilities(iastContext.vulnerabilities, span) | ||
expect(span.addTags).to.have.been.calledOnceWithExactly({ | ||
'manual.keep': 'true', | ||
'_dd.iast.json': '{"sources":[],"vulnerabilities":[{"type":"INSECURE_HASHING","hash":3410512691,' + | ||
'"evidence":{"value":"sha1"},"location":{"spanId":888,"path":"filename.js","line":88}}]}' | ||
}) | ||
expect(prioritySampler.setPriority).to.have.been.calledOnceWithExactly(span, USER_KEEP, SAMPLING_MECHANISM_APPSEC) | ||
}) | ||
|
||
it('should not send duplicated vulnerabilities', () => { | ||
|
@@ -348,10 +360,10 @@ describe('vulnerability-reporter', () => { | |
{ path: 'filename.js', line: 88 })) | ||
sendVulnerabilities(iastContext.vulnerabilities, span) | ||
expect(span.addTags).to.have.been.calledOnceWithExactly({ | ||
'manual.keep': 'true', | ||
'_dd.iast.json': '{"sources":[],"vulnerabilities":[{"type":"INSECURE_HASHING","hash":3410512691,' + | ||
'"evidence":{"value":"sha1"},"location":{"spanId":888,"path":"filename.js","line":88}}]}' | ||
}) | ||
expect(prioritySampler.setPriority).to.have.been.calledOnceWithExactly(span, USER_KEEP, SAMPLING_MECHANISM_APPSEC) | ||
}) | ||
|
||
it('should not send duplicated vulnerabilities in multiple sends', () => { | ||
|
@@ -365,10 +377,10 @@ describe('vulnerability-reporter', () => { | |
sendVulnerabilities(iastContext.vulnerabilities, span) | ||
sendVulnerabilities(iastContext.vulnerabilities, span) | ||
expect(span.addTags).to.have.been.calledOnceWithExactly({ | ||
'manual.keep': 'true', | ||
'_dd.iast.json': '{"sources":[],"vulnerabilities":[{"type":"INSECURE_HASHING","hash":3410512691,' + | ||
'"evidence":{"value":"sha1"},"location":{"spanId":888,"path":"filename.js","line":88}}]}' | ||
}) | ||
expect(prioritySampler.setPriority).to.have.been.calledOnceWithExactly(span, USER_KEEP, SAMPLING_MECHANISM_APPSEC) | ||
}) | ||
|
||
it('should not deduplicate vulnerabilities if not enabled', () => { | ||
|
@@ -384,12 +396,12 @@ describe('vulnerability-reporter', () => { | |
{ value: 'sha1' }, 888, { path: 'filename.js', line: 88 })) | ||
sendVulnerabilities(iastContext.vulnerabilities, span) | ||
expect(span.addTags).to.have.been.calledOnceWithExactly({ | ||
'manual.keep': 'true', | ||
'_dd.iast.json': '{"sources":[],"vulnerabilities":[{"type":"INSECURE_HASHING","hash":3410512691,' + | ||
'"evidence":{"value":"sha1"},"location":{"spanId":888,"path":"filename.js","line":88}},' + | ||
'{"type":"INSECURE_HASHING","hash":3410512691,"evidence":{"value":"sha1"},"location":' + | ||
'{"spanId":888,"path":"filename.js","line":88}}]}' | ||
}) | ||
expect(prioritySampler.setPriority).to.have.been.calledOnceWithExactly(span, USER_KEEP, SAMPLING_MECHANISM_APPSEC) | ||
}) | ||
|
||
it('should add _dd.p.appsec trace tag with standalone enabled', () => { | ||
|
@@ -401,11 +413,12 @@ describe('vulnerability-reporter', () => { | |
sendVulnerabilities(iastContext.vulnerabilities, span) | ||
|
||
expect(span.addTags).to.have.been.calledOnceWithExactly({ | ||
'manual.keep': 'true', | ||
'_dd.iast.json': '{"sources":[],"vulnerabilities":[{"type":"INSECURE_HASHING","hash":3254801297,' + | ||
'"evidence":{"value":"sha1"},"location":{"spanId":999}}]}' | ||
}) | ||
|
||
expect(prioritySampler.setPriority).to.have.been.calledOnceWithExactly(span, USER_KEEP, SAMPLING_MECHANISM_APPSEC) | ||
|
||
expect(span.context()._trace.tags).to.have.property(APPSEC_PROPAGATION_KEY) | ||
}) | ||
|
||
|
@@ -418,11 +431,12 @@ describe('vulnerability-reporter', () => { | |
sendVulnerabilities(iastContext.vulnerabilities, span) | ||
|
||
expect(span.addTags).to.have.been.calledOnceWithExactly({ | ||
'manual.keep': 'true', | ||
'_dd.iast.json': '{"sources":[],"vulnerabilities":[{"type":"INSECURE_HASHING","hash":3254801297,' + | ||
'"evidence":{"value":"sha1"},"location":{"spanId":999}}]}' | ||
}) | ||
|
||
expect(prioritySampler.setPriority).to.have.been.calledOnceWithExactly(span, USER_KEEP, SAMPLING_MECHANISM_APPSEC) | ||
|
||
expect(span.context()._trace.tags).to.not.have.property(APPSEC_PROPAGATION_KEY) | ||
}) | ||
}) | ||
|
@@ -441,7 +455,8 @@ describe('vulnerability-reporter', () => { | |
global.setInterval = sinon.spy(global.setInterval) | ||
global.clearInterval = sinon.spy(global.clearInterval) | ||
span = { | ||
addTags: sinon.stub() | ||
addTags: sinon.stub(), | ||
keep: sinon.stub() | ||
} | ||
}) | ||
|
||
|
Oops, something went wrong.