Skip to content

Commit 1eb5d73

Browse files
committed
Fix missing project filter for /api/v1/violation/project
Regression from #2658 Signed-off-by: nscuro <nscuro@protonmail.com>
1 parent eeec9f8 commit 1eb5d73

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/main/java/org/dependencytrack/persistence/PolicyQueryManager.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -269,16 +269,17 @@ public List<PolicyViolation> getAllPolicyViolations(final Project project) {
269269
@SuppressWarnings("unchecked")
270270
public PaginatedResult getPolicyViolations(final Project project, boolean includeSuppressed) {
271271
PaginatedResult result;
272-
final String projectFilter = includeSuppressed ? "project.id == :pid" : "project.id == :pid && (analysis.suppressed == false || analysis.suppressed == null)";
272+
final String queryFilter = includeSuppressed ? "project.id == :pid" : "project.id == :pid && (analysis.suppressed == false || analysis.suppressed == null)";
273273
final Query<PolicyViolation> query = pm.newQuery(PolicyViolation.class);
274274
if (orderBy == null) {
275275
query.setOrdering("timestamp desc, component.name, component.version");
276276
}
277277
if (filter != null) {
278-
query.setFilter(projectFilter + " && (policyCondition.policy.name.toLowerCase().matches(:filter) || component.name.toLowerCase().matches(:filter))");
278+
query.setFilter(queryFilter + " && (policyCondition.policy.name.toLowerCase().matches(:filter) || component.name.toLowerCase().matches(:filter))");
279279
final String filterString = ".*" + filter.toLowerCase() + ".*";
280280
result = execute(query, project.getId(), filterString);
281281
} else {
282+
query.setFilter(queryFilter);
282283
result = execute(query, project.getId());
283284
}
284285
for (final PolicyViolation violation: result.getList(PolicyViolation.class)) {

src/test/java/org/dependencytrack/resources/v1/PolicyViolationResourceTest.java

+40
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,46 @@ public void getViolationsByProjectTest() {
179179
assertThat(jsonObject1.getJsonObject("component").getString("name")).isEqualTo("Acme Component 0");
180180
}
181181

182+
@Test
183+
public void getViolationsByProjectIssue2766() {
184+
initializeWithPermissions(Permissions.VIEW_POLICY_VIOLATION);
185+
186+
final Project projectA = qm.createProject("acme-app-a", null, "1.0", null, null, null, true, false);
187+
final var componentA = new Component();
188+
componentA.setProject(projectA);
189+
componentA.setName("acme-lib-a");
190+
componentA.setVersion("1.0.1");
191+
qm.persist(componentA);
192+
193+
final Project projectB = qm.createProject("acme-app-b", null, "2.0", null, null, null, true, false);
194+
final var componentB = new Component();
195+
componentB.setProject(projectB);
196+
componentB.setName("acme-lib-b");
197+
componentB.setVersion("2.0.1");
198+
qm.persist(componentB);
199+
200+
final Policy policy = qm.createPolicy("policy", Policy.Operator.ALL, Policy.ViolationState.FAIL);
201+
final PolicyCondition condition = qm.createPolicyCondition(policy, PolicyCondition.Subject.VERSION, PolicyCondition.Operator.NUMERIC_EQUAL, "1.0.1");
202+
final var violation = new PolicyViolation();
203+
violation.setPolicyCondition(condition);
204+
violation.setComponent(componentA);
205+
violation.setType(PolicyViolation.Type.OPERATIONAL);
206+
violation.setTimestamp(new Date());
207+
qm.persist(violation);
208+
209+
// Requesting violations for projectB must not yield violations for projectA.
210+
final Response response = target(V1_POLICY_VIOLATION)
211+
.path("/project/" + projectB.getUuid())
212+
.request()
213+
.header(X_API_KEY, apiKey)
214+
.get();
215+
assertThat(response.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
216+
assertThat(response.getHeaderString(TOTAL_COUNT_HEADER)).isEqualTo("0");
217+
218+
final JsonArray jsonArray = parseJsonArray(response);
219+
assertThat(jsonArray).hasSize(0);
220+
}
221+
182222
@Test
183223
public void getViolationsByProjectUnauthorizedTest() {
184224
final Response response = target(V1_POLICY_VIOLATION)

0 commit comments

Comments
 (0)