Skip to content

Commit 52afc1d

Browse files
authored
Expand appsec Laminas testing (#4339)
1 parent 246fb8b commit 52afc1d

8 files changed

Lines changed: 512 additions & 317 deletions

File tree

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
package com.datadog.appsec.php.integration
2+
3+
import com.datadog.appsec.php.TelemetryHelpers
4+
import com.datadog.appsec.php.docker.AppSecContainer
5+
import com.datadog.appsec.php.model.Span
6+
import com.datadog.appsec.php.model.Trace
7+
import org.junit.jupiter.api.Order
8+
import org.junit.jupiter.api.Test
9+
10+
import java.net.http.HttpRequest
11+
import java.net.http.HttpResponse
12+
13+
import static java.net.http.HttpResponse.BodyHandlers.ofInputStream
14+
import static java.net.http.HttpResponse.BodyHandlers.ofString
15+
16+
/**
17+
* Shared AppSec tests for the Laminas MVC fixtures. Concrete subclasses supply
18+
* the {@code @Container} static field, declare {@code isExpectedVersion}, and
19+
* override the {@code is*} / {@code get*} hooks when a specific MVC version
20+
* exposes different route literals or a reduced endpoint set.
21+
*
22+
* Laminas 3.3 exposes a legacy {@code /authenticate} + {@code /behind-auth}
23+
* controller pair; newer fixtures standardise on {@code /login/auth} +
24+
* {@code /behind_auth}. The hooks absorb that divergence so the auth test
25+
* bodies remain shared.
26+
*/
27+
abstract class AbstractLaminasAppsecTests {
28+
29+
AppSecContainer getContainer() {
30+
getClass().CONTAINER
31+
}
32+
33+
/**
34+
* When {@code true} (default), {@link #getExpectedEndpoints} may be a
35+
* strict subset of the endpoints emitted by the fixture (extras are
36+
* accepted). Set to {@code false} in subclasses that enumerate every
37+
* expected route to also assert the exact total count.
38+
*/
39+
boolean isAllowUnenumeratedRoutes() { true }
40+
41+
/**
42+
* Routes that must appear in the endpoints telemetry. Entries are
43+
* {@code [path, method, resourceName]} triples; operation name is always
44+
* {@code http.request}. Must be exhaustive when
45+
* {@link #isAllowUnenumeratedRoutes} is {@code false}.
46+
*/
47+
List<List<String>> getExpectedEndpoints() { [] }
48+
49+
/**
50+
* Path prefix for the login authentication endpoint (no query string).
51+
* Legacy 3.3 fixtures use {@code /authenticate}; newer fixtures use
52+
* {@code /login/auth}.
53+
*/
54+
String getLoginAuthPath() { '/login/auth' }
55+
56+
/**
57+
* Path for the behind-auth endpoint. 3.3 fixtures expose {@code /behind-auth}
58+
* (dash); newer fixtures expose {@code /behind_auth} (underscore).
59+
*/
60+
String getBehindAuthPath() { '/behind_auth' }
61+
62+
/**
63+
* Expected {@code http.route} tag value for login auth responses. Mirrors
64+
* {@link #getLoginAuthPath} by default; subclasses override only if the
65+
* literal differs from the request path.
66+
*/
67+
String getLoginAuthHttpRoute() { getLoginAuthPath() }
68+
69+
/**
70+
* Expected {@code http.route} tag value for behind-auth responses.
71+
*/
72+
String getBehindAuthHttpRoute() { getBehindAuthPath() }
73+
74+
@Test
75+
@Order(1)
76+
void 'Endpoints are not collected before the first request to framework'() {
77+
HttpRequest req = container.buildReq('/outside_of_framework.php').GET().build()
78+
container.traceFromRequest(req, ofString()) { HttpResponse<String> re ->
79+
assert re.statusCode() == 200
80+
assert re.body().contains('are_endpoints_collected: false')
81+
}
82+
}
83+
84+
@Test
85+
@Order(2)
86+
void 'Endpoints are sent'() {
87+
Trace trace = container.traceFromRequest('/') { HttpResponse<InputStream> resp ->
88+
assert resp.statusCode() == 200
89+
}
90+
91+
assert trace.traceId != null
92+
assert trace.first().meta.'http.route' == '/'
93+
94+
List<TelemetryHelpers.Endpoint> endpoints
95+
TelemetryHelpers.waitForAppEndpoints(container, 30, { List<TelemetryHelpers.Endpoint> messages ->
96+
endpoints = messages.collectMany { it.endpoints }
97+
endpoints.size() > 0
98+
})
99+
100+
if (!allowUnenumeratedRoutes) {
101+
assert endpoints.size() == expectedEndpoints.size()
102+
}
103+
expectedEndpoints.each { List<String> entry ->
104+
String path = entry[0]
105+
String method = entry[1]
106+
String resource = entry[2]
107+
assert endpoints.find {
108+
it.path == path && it.method == method &&
109+
it.operationName == 'http.request' && it.resourceName == resource
110+
} != null, "Missing endpoint ${method} ${path}"
111+
}
112+
}
113+
114+
@Test
115+
@Order(3)
116+
void 'Endpoints are collected after the first request to framework'() {
117+
HttpRequest req = container.buildReq('/outside_of_framework.php').GET().build()
118+
container.traceFromRequest(req, ofString()) { HttpResponse<String> re ->
119+
assert re.statusCode() == 200
120+
assert re.body().contains('are_endpoints_collected: true')
121+
}
122+
}
123+
124+
@Test
125+
@Order(4)
126+
void 'Login failure automated event'() {
127+
Trace trace = container.traceFromRequest("${loginAuthPath}?email=nonExisiting@email.com") {
128+
HttpResponse<InputStream> resp ->
129+
assert resp.statusCode() == 403
130+
}
131+
132+
Span span = trace.first()
133+
assert span.meta.'appsec.events.users.login.failure.track' == 'true'
134+
assert span.meta.'_dd.appsec.events.users.login.failure.auto.mode' == 'identification'
135+
assert span.meta.'appsec.events.users.login.failure.usr.exists' == 'false'
136+
assert span.metrics._sampling_priority_v1 == 2.0d
137+
assert span.meta.'http.route' == loginAuthHttpRoute
138+
}
139+
140+
@Test
141+
@Order(5)
142+
void 'Login success automated event'() {
143+
def trace = container.traceFromRequest("${loginAuthPath}?email=ciuser@example.com") {
144+
HttpResponse<InputStream> resp ->
145+
assert resp.statusCode() == 200
146+
}
147+
148+
Span span = trace.first()
149+
assert span.meta.'usr.id' == '1'
150+
assert span.meta.'_dd.appsec.events.users.login.success.auto.mode' == 'identification'
151+
assert span.meta.'_dd.appsec.usr.login' == 'ciuser@example.com'
152+
assert span.meta.'appsec.events.users.login.success.track' == 'true'
153+
assert span.metrics._sampling_priority_v1 == 2.0d
154+
assert span.meta.'http.route' == loginAuthHttpRoute
155+
}
156+
157+
@Test
158+
@Order(6)
159+
void 'Authenticated user automated event after session login'() {
160+
HttpRequest loginReq = container.buildReq("${loginAuthPath}?email=ciuser@example.com").GET().build()
161+
HttpResponse<InputStream> loginResp = container.httpClient.send(loginReq, ofInputStream())
162+
assert loginResp.statusCode() == 200
163+
loginResp.body().close()
164+
165+
String cookieHeader = loginResp.headers().allValues('Set-Cookie')
166+
.collect { full -> full.split(';', 2)[0] }
167+
.join('; ')
168+
assert cookieHeader, 'login response should include session cookie'
169+
170+
container.nextCapturedTrace()
171+
172+
HttpRequest behindReq = container.buildReq(behindAuthPath)
173+
.header('Cookie', cookieHeader)
174+
.GET()
175+
.build()
176+
Trace trace = container.traceFromRequest(behindReq, ofInputStream()) { HttpResponse<InputStream> resp ->
177+
assert resp.statusCode() == 200
178+
}
179+
180+
Span span = trace.first()
181+
assert span.meta.'usr.id' == '1'
182+
assert span.meta.'_dd.appsec.usr.id' == '1'
183+
assert span.meta.'_dd.appsec.user.collection_mode' == 'identification'
184+
assert span.meta.'http.route' == behindAuthHttpRoute
185+
}
186+
}

0 commit comments

Comments
 (0)