|
| 1 | +package core |
| 2 | + |
| 3 | +import datadog.trace.agent.test.AgentTestRunner |
| 4 | +import datadog.trace.api.iast.IastContext |
| 5 | +import datadog.trace.api.iast.InstrumentationBridge |
| 6 | +import datadog.trace.api.iast.SourceTypes |
| 7 | +import datadog.trace.api.iast.Taintable |
| 8 | +import datadog.trace.api.iast.propagation.PropagationModule |
| 9 | +import datadog.trace.bootstrap.instrumentation.api.AgentTracer |
| 10 | +import datadog.trace.bootstrap.instrumentation.api.TagContext |
| 11 | +import groovy.transform.CompileDynamic |
| 12 | +import io.netty.handler.codec.http.DefaultHttpHeaders |
| 13 | +import io.netty.handler.codec.http2.DefaultHttp2Headers |
| 14 | +import io.vertx.core.MultiMap |
| 15 | +import io.vertx.core.http.impl.headers.HeadersAdaptor |
| 16 | +import io.vertx.core.http.impl.headers.HeadersMultiMap |
| 17 | +import io.vertx.core.http.impl.headers.Http2HeadersAdaptor |
| 18 | +import spock.lang.IgnoreIf |
| 19 | + |
| 20 | +import static datadog.trace.api.iast.SourceTypes.namedSource |
| 21 | + |
| 22 | +@CompileDynamic |
| 23 | +class MultiMapInstrumentationTest extends AgentTestRunner { |
| 24 | + |
| 25 | + private Object iastCtx |
| 26 | + |
| 27 | + @Override |
| 28 | + protected void configurePreAgent() { |
| 29 | + injectSysConfig('dd.iast.enabled', 'true') |
| 30 | + } |
| 31 | + |
| 32 | + void setup() { |
| 33 | + iastCtx = Stub(IastContext) |
| 34 | + } |
| 35 | + |
| 36 | + void 'test that #name get() is instrumented'() { |
| 37 | + given: |
| 38 | + final origin = SourceTypes.REQUEST_PARAMETER_VALUE |
| 39 | + addAll([key: 'value'], instance) |
| 40 | + final module = Mock(PropagationModule) |
| 41 | + InstrumentationBridge.registerIastModule(module) |
| 42 | +
|
| 43 | + when: |
| 44 | + runUnderIastTrace { instance.get('key') } |
| 45 | +
|
| 46 | + then: |
| 47 | + 1 * module.findSource(iastCtx, instance) >> { null } |
| 48 | + 0 * _ |
| 49 | +
|
| 50 | + when: |
| 51 | + runUnderIastTrace { instance.get('key') } |
| 52 | +
|
| 53 | + then: |
| 54 | + 1 * module.findSource(iastCtx, instance) >> { mockedSource(origin) } |
| 55 | + 1 * module.taintString(iastCtx, 'value', origin, 'key') |
| 56 | +
|
| 57 | + where: |
| 58 | + instance << multiMaps() |
| 59 | + name = instance.getClass().simpleName |
| 60 | + } |
| 61 | +
|
| 62 | + void 'test that #name getAll() is instrumented'() { |
| 63 | + given: |
| 64 | + final origin = SourceTypes.REQUEST_PARAMETER_VALUE |
| 65 | + addAll([[key: 'value1'], [key: 'value2']], instance) |
| 66 | + final module = Mock(PropagationModule) |
| 67 | + InstrumentationBridge.registerIastModule(module) |
| 68 | +
|
| 69 | + when: |
| 70 | + runUnderIastTrace { instance.getAll('key') } |
| 71 | +
|
| 72 | + then: |
| 73 | + 1 * module.findSource(iastCtx, instance) >> { null } |
| 74 | + 0 * _ |
| 75 | +
|
| 76 | + when: |
| 77 | + runUnderIastTrace { instance.getAll('key') } |
| 78 | +
|
| 79 | + then: |
| 80 | + 1 * module.findSource(iastCtx, instance) >> { mockedSource(origin) } |
| 81 | + 1 * module.taintString(iastCtx, 'value1', origin, 'key') |
| 82 | + 1 * module.taintString(iastCtx, 'value2', origin, 'key') |
| 83 | +
|
| 84 | + where: |
| 85 | + instance << multiMaps() |
| 86 | + name = instance.getClass().simpleName |
| 87 | + } |
| 88 | +
|
| 89 | + void 'test that #name names() is instrumented'() { |
| 90 | + given: |
| 91 | + final origin = SourceTypes.REQUEST_PARAMETER_VALUE |
| 92 | + addAll([[key: 'value1'], [key: 'value2']], instance) |
| 93 | + final module = Mock(PropagationModule) |
| 94 | + InstrumentationBridge.registerIastModule(module) |
| 95 | +
|
| 96 | + when: |
| 97 | + runUnderIastTrace { instance.names() } |
| 98 | +
|
| 99 | + then: |
| 100 | + 1 * module.findSource(iastCtx, instance) >> { null } |
| 101 | + 0 * _ |
| 102 | +
|
| 103 | + when: |
| 104 | + runUnderIastTrace { instance.names() } |
| 105 | +
|
| 106 | + then: |
| 107 | + 1 * module.findSource(iastCtx, instance) >> { mockedSource(origin) } |
| 108 | + 1 * module.taintString(iastCtx, 'key', namedSource(origin), 'key') |
| 109 | +
|
| 110 | + where: |
| 111 | + instance << multiMaps() |
| 112 | + name = instance.getClass().simpleName |
| 113 | + } |
| 114 | +
|
| 115 | + // some implementations do not override the entries() method so we will lose propagation in those cases |
| 116 | + @IgnoreIf({ !MultiMapInstrumentationTest.hasMethod(data['instance'].class, 'entries')}) |
| 117 | + void 'test that #name entries() is instrumented'() { |
| 118 | + given: |
| 119 | + final origin = SourceTypes.REQUEST_PARAMETER_VALUE |
| 120 | + addAll([[key: 'value1'], [key: 'value2']], instance) |
| 121 | + final module = Mock(PropagationModule) |
| 122 | + InstrumentationBridge.registerIastModule(module) |
| 123 | +
|
| 124 | + when: |
| 125 | + runUnderIastTrace { instance.entries() } |
| 126 | +
|
| 127 | + then: |
| 128 | + 1 * module.findSource(iastCtx, instance) >> { null } |
| 129 | + 0 * _ |
| 130 | +
|
| 131 | + when: |
| 132 | + runUnderIastTrace { instance.entries() } |
| 133 | +
|
| 134 | + then: |
| 135 | + 1 * module.findSource(iastCtx, instance) >> { mockedSource(origin) } |
| 136 | + 1 * module.taintString(iastCtx, 'key', namedSource(origin), 'key') |
| 137 | + 1 * module.taintString(iastCtx, 'value1', origin, 'key') |
| 138 | + 1 * module.taintString(iastCtx, 'value2', origin, 'key') |
| 139 | +
|
| 140 | + where: |
| 141 | + instance << multiMaps() |
| 142 | + name = instance.getClass().simpleName |
| 143 | + } |
| 144 | +
|
| 145 | + protected <E> E runUnderIastTrace(Closure<E> cl) { |
| 146 | + final ddctx = new TagContext().withRequestContextDataIast(iastCtx) |
| 147 | + final span = TEST_TRACER.startSpan("test", "test-iast-span", ddctx) |
| 148 | + try { |
| 149 | + return AgentTracer.activateSpan(span).withCloseable(cl) |
| 150 | + } finally { |
| 151 | + span.finish() |
| 152 | + } |
| 153 | + } |
| 154 | +
|
| 155 | + private mockedSource(final byte origin) { |
| 156 | + return Mock(Taintable.Source) { |
| 157 | + getOrigin() >> origin |
| 158 | + } |
| 159 | + } |
| 160 | +
|
| 161 | + private static boolean hasMethod(final Class<?> target, final String name) { |
| 162 | + try { |
| 163 | + return target.getDeclaredMethods().any { it.name == name } |
| 164 | + } catch (Throwable e) { |
| 165 | + return false |
| 166 | + } |
| 167 | + } |
| 168 | +
|
| 169 | + private List<MultiMap> multiMaps() { |
| 170 | + return [ |
| 171 | + new HeadersMultiMap(), |
| 172 | + new HeadersAdaptor(new DefaultHttpHeaders()), |
| 173 | + new Http2HeadersAdaptor(new DefaultHttp2Headers()) |
| 174 | + ] |
| 175 | + } |
| 176 | +
|
| 177 | + private static void addAll(final Map<String, String> map, final MultiMap headers) { |
| 178 | + map.each { key, value -> headers.add(key, value) } |
| 179 | + } |
| 180 | +
|
| 181 | + private static void addAll(final List<Map<String, String>> list, final MultiMap headers) { |
| 182 | + list.each { addAll(it, headers) } |
| 183 | + } |
| 184 | +} |
0 commit comments