Skip to content

Commit 76cb67d

Browse files
authored
Fix handling of bad URL parameters in HDRL (#241)
1 parent 9c7486f commit 76cb67d

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

hdrl/src/org/labkey/hdrl/HDRLController.java

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,21 @@
1616

1717
package org.labkey.hdrl;
1818

19+
import jakarta.servlet.http.HttpServletResponse;
1920
import org.apache.commons.beanutils.BeanUtils;
2021
import org.apache.commons.io.IOUtils;
2122
import org.json.JSONArray;
2223
import org.json.JSONException;
2324
import org.json.JSONObject;
2425
import org.labkey.api.action.ApiSimpleResponse;
26+
import org.labkey.api.action.ApiUsageException;
2527
import org.labkey.api.action.ExportAction;
2628
import org.labkey.api.action.FormViewAction;
2729
import org.labkey.api.action.Marshal;
2830
import org.labkey.api.action.Marshaller;
2931
import org.labkey.api.action.MutatingApiAction;
3032
import org.labkey.api.action.ReadOnlyApiAction;
3133
import org.labkey.api.action.SimpleApiJsonForm;
32-
import org.labkey.api.action.SimpleErrorView;
3334
import org.labkey.api.action.SimpleViewAction;
3435
import org.labkey.api.action.SpringActionController;
3536
import org.labkey.api.admin.AdminUrls;
@@ -74,13 +75,13 @@
7475
import org.springframework.validation.Errors;
7576
import org.springframework.web.servlet.ModelAndView;
7677

77-
import jakarta.servlet.http.HttpServletResponse;
7878
import java.io.IOException;
7979
import java.util.ArrayList;
8080
import java.util.Collections;
8181
import java.util.Date;
8282
import java.util.List;
8383
import java.util.Map;
84+
import java.util.Objects;
8485

8586
public class HDRLController extends SpringActionController
8687
{
@@ -137,11 +138,11 @@ public static class RequestDetailsAction extends SimpleViewAction<Object>
137138
@Override
138139
public ModelAndView getView(Object o, BindException errors)
139140
{
140-
String requestId = getViewContext().getRequest().getParameter("requestId");
141-
if (requestId != null)
141+
try
142142
{
143-
InboundRequestBean bean = HDRLManager.get().getInboundRequest(getUser(), getContainer(), Integer.parseInt(requestId));
144-
JspView jsp = new JspView<>("/org/labkey/hdrl/view/requestDetails.jsp", bean);
143+
int requestId = Integer.parseInt(getViewContext().getRequest().getParameter("requestId"));
144+
InboundRequestBean bean = HDRLManager.get().getInboundRequest(getUser(), getContainer(), requestId);
145+
JspView<?> jsp = new JspView<>("/org/labkey/hdrl/view/requestDetails.jsp", bean);
145146
jsp.setTitle("Test Request");
146147

147148
UserSchema schema = QueryService.get().getUserSchema(getUser(), getContainer(), HDRLQuerySchema.NAME);
@@ -151,10 +152,9 @@ public ModelAndView getView(Object o, BindException errors)
151152
jsp.setView("queryView", queryView);
152153
return jsp;
153154
}
154-
else
155+
catch (NumberFormatException x)
155156
{
156-
errors.reject("RequestId is required");
157-
return new SimpleErrorView(errors);
157+
throw new ApiUsageException("RequestId is required");
158158
}
159159
}
160160

@@ -175,10 +175,18 @@ public ModelAndView getView(RequestForm form, BindException errors) throws Excep
175175
{
176176
if (form.getRequestId() != -1)
177177
{
178-
_navLabel = "Edit a Test Request";
179-
180178
TableSelector selector = new TableSelector(org.labkey.hdrl.HDRLSchema.getInstance().getTableInfoInboundRequest());
181-
BeanUtils.copyProperties(form, selector.getObject(form.getRequestId(), RequestForm.class));
179+
RequestForm object = selector.getObject(form.getRequestId(), RequestForm.class);
180+
if (object != null)
181+
{
182+
_navLabel = "Edit a Test Request";
183+
BeanUtils.copyProperties(form, object);
184+
}
185+
else
186+
{
187+
form.setTestTypeId(1); // default to first test type
188+
errors.reject(ERROR_MSG, "Request with id " + form.getRequestId() + " not found.");
189+
}
182190
}
183191
else
184192
{
@@ -265,7 +273,11 @@ public static class VerifySpecimenAction extends ReadOnlyApiAction<VerifyForm>
265273
public Object execute(VerifyForm form, BindException errors)
266274
{
267275
ApiSimpleResponse response = new ApiSimpleResponse();
268-
JSONArray rows = form.getJsonObject().getJSONArray("rows");
276+
JSONArray rows = Objects.requireNonNullElseGet(form.getJsonObject(), JSONObject::new).optJSONArray("rows");
277+
if (rows == null)
278+
{
279+
throw new ApiUsageException("rows not provided.");
280+
}
269281
List<Map<String, Object>> rowsToValidate = new ArrayList<>();
270282

271283
for (int idx = 0; idx < rows.length(); ++idx)
@@ -277,7 +289,7 @@ public Object execute(VerifyForm form, BindException errors)
277289
}
278290
catch (JSONException x)
279291
{
280-
throw new IllegalArgumentException("rows[" + idx + "] is not an object.");
292+
throw new ApiUsageException("rows[" + idx + "] is not an object.");
281293
}
282294
if (null != jsonObj)
283295
{

hdrl/src/org/labkey/hdrl/HDRLManager.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.labkey.api.query.QueryService;
3434
import org.labkey.api.query.UserSchema;
3535
import org.labkey.api.security.User;
36+
import org.labkey.api.view.NotFoundException;
3637
import org.labkey.hdrl.query.HDRLQuerySchema;
3738
import org.labkey.hdrl.query.LabWareQuerySchema;
3839
import org.labkey.hdrl.view.InboundRequestBean;
@@ -67,6 +68,9 @@ public static HDRLManager get()
6768
public InboundRequestBean getInboundRequest(User user, Container container, Integer requestId)
6869
{
6970
UserSchema schema = QueryService.get().getUserSchema(user, container, HDRLQuerySchema.NAME);
71+
if (schema == null)
72+
throw new NotFoundException(HDRLModule.NAME + " module is not enabled in this container.");
73+
7074
SQLFragment sql = new SQLFragment("SELECT r.RequestId, r.ShippingNumber, s.Name as RequestStatus, c.Name as ShippingCarrier, t.Name as TestType FROM ");
7175
sql.append("(SELECT * FROM hdrl.InboundRequest WHERE (Container = ?) AND (RequestId = ?)) r ");
7276
sql.add(container);
@@ -77,7 +81,10 @@ public InboundRequestBean getInboundRequest(User user, Container container, Inte
7781

7882

7983
SqlSelector sqlSelector = new SqlSelector(schema.getDbSchema(), sql);
80-
return sqlSelector.getObject(InboundRequestBean.class);
84+
InboundRequestBean inboundRequestBean = sqlSelector.getObject(InboundRequestBean.class);
85+
if (inboundRequestBean == null)
86+
throw new NotFoundException("Request %s not found.".formatted(requestId));
87+
return inboundRequestBean;
8188
}
8289

8390
public List<InboundSpecimenBean> getInboundSpecimen(int requestId)

0 commit comments

Comments
 (0)