Skip to content

Commit 00ac22b

Browse files
committed
protect manual sync
1 parent 57efa10 commit 00ac22b

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

src/main/java/com/cronos/onlinereview/actions/project/SyncProjectAction.java

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
*/
44
package com.cronos.onlinereview.actions.project;
55

6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
import org.apache.http.HttpHeaders;
11+
12+
import com.auth0.jwt.interfaces.Claim;
13+
import com.auth0.jwt.interfaces.DecodedJWT;
14+
import com.cronos.onlinereview.util.AuthorizationHelper;
615
import com.topcoder.onlinereview.component.exception.BaseException;
716
import com.topcoder.onlinereview.component.grpcclient.GrpcHelper;
817

@@ -26,11 +35,47 @@ public SyncProjectAction() {
2635

2736
public String execute() throws BaseException {
2837
String projectId = request.getParameter("projectId");
38+
String tables = request.getParameter("tables");
39+
List<String> tableNames = new ArrayList<>();
40+
if (tables != null && !tables.isEmpty()) {
41+
tableNames = Arrays.asList(tables.split(","));
42+
}
43+
if (projectId.isEmpty() || tableNames.isEmpty()) {
44+
return NONE;
45+
}
46+
47+
String authHeader = request.getHeader(HttpHeaders.AUTHORIZATION);
48+
if (authHeader == null || authHeader.isEmpty()) {
49+
return NONE;
50+
}
51+
String[] headerParts = authHeader.split(" ");
52+
if (headerParts.length < 2) {
53+
return NONE;
54+
}
55+
String token = headerParts[1];
2956

30-
GrpcHelper.getSyncServiceRpc().saveProjectSync(Long.valueOf(projectId), false, false,
31-
false, false, true, false, false, false);
57+
DecodedJWT jwt;
58+
try {
59+
jwt = AuthorizationHelper.validateJWTToken(token);
60+
} catch (Exception e) {
61+
return NONE;
62+
}
63+
boolean hasAccess = false;
64+
for (String claimName : jwt.getClaims().keySet()) {
65+
if (claimName.endsWith("/roles")) {
66+
Claim claim = jwt.getClaim(claimName);
67+
for (String role : claim.asArray(String.class)) {
68+
if (role.equals("administrator")) {
69+
hasAccess = true;
70+
}
71+
}
72+
}
73+
}
74+
if (!hasAccess) {
75+
return NONE;
76+
}
3277

33-
// Signal about successful execution of the Action
34-
return "syncResult";
78+
GrpcHelper.getSyncServiceRpc().manualSync(Long.valueOf(projectId), tableNames);
79+
return NONE;
3580
}
3681
}

src/main/resources/struts.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,6 @@
520520
</action>
521521

522522
<action name="SyncProject" class="syncProjectAction">
523-
<result name="success">/jsp/syncResult.jsp</result>
524523
</action>
525524

526525
<!-- Redirect for old-style Struts1 requests -->

0 commit comments

Comments
 (0)