Skip to content

Commit 01ace93

Browse files
committed
frontend: validate JSON in migration request
Motivation: if user provides bad JSON we should respond with BAD_REQUEST. Modification: Try to parse provided json and throw BadRequestException if invalid. Result: BAD_REQUEST instead of failure.` Fixes: #7983 Acked-by: Paul Millar Target: master Require-book: no Require-notes: yes
1 parent 766f7b3 commit 01ace93

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

.ci/migrationEndpoint.http

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,23 @@ Authorization: Basic {{username}} {{password}}
272272
});
273273
%}
274274

275+
### copy endpoint - bad request on invalid json format
276+
POST {{frontend-door}}{{endpoint}}{{migrations}}/copy
277+
Content-Type: application/json
278+
Authorization: Basic {{username}} {{password}}
279+
280+
{
281+
"sourcePool": ,
282+
"targetPools": ["]
283+
}
284+
285+
> {%
286+
client.test("Response is Bad request", function() {
287+
client.assert(response.status === 400, "Expected 400, got " + response.status);
288+
});
289+
%}
290+
291+
275292
### copy endpoint - bad request when cocurrency is provided
276293
//FYI concurrency should be a whole number -- may need to modify this test in the future!
277294
POST {{frontend-door}}{{endpoint}}{{migrations}}/copy

modules/dcache-frontend/src/main/java/org/dcache/restful/resources/migration/MigrationResources.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.dcache.restful.providers.migrations.MigrationInfo;
3636
import org.dcache.restful.util.RequestUser;
3737
import org.json.JSONArray;
38+
import org.json.JSONException;
3839
import org.json.JSONObject;
3940
import org.slf4j.Logger;
4041
import org.slf4j.LoggerFactory;
@@ -120,7 +121,13 @@ public Response submitMigrationCopy(@ApiParam(
120121
}
121122

122123
// First convert to JSON.
123-
JSONObject jsonPayload = new JSONObject(requestPayload);
124+
JSONObject jsonPayload;
125+
try {
126+
jsonPayload = new JSONObject(requestPayload);
127+
} catch (JSONException e) {
128+
throw new BadRequestException("The request payload is not valid JSON.", e);
129+
}
130+
124131
LOGGER.info("JSON Request: {}", jsonPayload);
125132

126133
if (!jsonPayload.has("sourcePool")) {

0 commit comments

Comments
 (0)