Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions api/src/org/labkey/api/action/ExtFormResponseWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.labkey.api.query.PropertyValidationError;
import org.labkey.api.query.ValidationError;
import org.labkey.api.query.ValidationException;
import org.labkey.api.util.PageFlowUtil;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
Expand All @@ -29,12 +30,6 @@
import java.io.IOException;
import java.io.Writer;

/*
* User: Dave
* Date: Sep 3, 2008
* Time: 11:03:32 AM
*/

/**
* This writer extends ApiJsonWriter by writing validation errors in the format
* that Ext forms require.
Expand Down Expand Up @@ -92,6 +87,25 @@ public ExtFormResponseWriter(HttpServletRequest request, HttpServletResponse res
response.setContentType(contentTypeOverride);
}

@Override
public void writeProperty(String name, Object value) throws IOException
{
super.writeProperty(sendHtmlJsonResponse ? PageFlowUtil.filter(name) : name, value);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a comment here to the effect that super.writeProperty() calls writeObject() which encodes

}

@Override
protected void writeObject(Object value) throws IOException
{
if (value instanceof String s && sendHtmlJsonResponse)
{
super.writeObject(PageFlowUtil.filter(s));
}
else
{
super.writeObject(value);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So all non-String values are safe to render without encoding?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In practice, yes. But there are other possible values that could end up rendering as strings. I was able to change the override approach to catch more of those theoretical pathways. I didn't see a way to intercept this line though:

        else if (isSerializeViaJacksonAnnotations() || value instanceof SimpleResponse<?>)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If everything is supposed to be encoded, could this be tackled from the stream side?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If everything is supposed to be encoded, could this be tackled from the stream side?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored to an encoding Writer

}
}

@Override
public JSONObject toJSON(ValidationException e)
{
Expand Down Expand Up @@ -121,8 +135,8 @@ public void toJSON(JSONObject jsonErrors, ValidationError error)
{
String msg = error.getMessage();
String key = "_form";
if (error instanceof PropertyValidationError)
key = ((PropertyValidationError)error).getProperty();
if (error instanceof PropertyValidationError pve)
key = pve.getProperty();
if (jsonErrors.has(key))
msg = jsonErrors.get(key) + "; " + msg;
jsonErrors.put(key, msg);
Expand All @@ -139,8 +153,8 @@ public void writeResponse(Errors errors) throws IOException
if (message == null)
message = msg;
String key = "_form";
if (error instanceof FieldError)
key = ((FieldError)error).getField();
if (error instanceof FieldError fieldError)
key = fieldError.getField();
if (jsonErrors.has(key))
msg = jsonErrors.get(key) + "; " + msg;
jsonErrors.put(key, msg);
Expand Down Expand Up @@ -174,10 +188,7 @@ protected Writer getWriter()
{
w.write("<html><body><textarea>");
}
catch (IOException x)
{

}
catch (IOException ignored) {}
}
return w;
}
Expand Down