-
Notifications
You must be signed in to change notification settings - Fork 7
HTML-encode the JSON response based on the content type #7385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
4712db5
2016278
780e22b
5e258a3
489f330
df0abbe
c7d3be7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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. | ||
|
|
@@ -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); | ||
| } | ||
|
|
||
| @Override | ||
| protected void writeObject(Object value) throws IOException | ||
| { | ||
| if (value instanceof String s && sendHtmlJsonResponse) | ||
| { | ||
| super.writeObject(PageFlowUtil.filter(s)); | ||
| } | ||
| else | ||
| { | ||
| super.writeObject(value); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So all non-String values are safe to render without encoding?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I refactored to an encoding |
||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public JSONObject toJSON(ValidationException e) | ||
| { | ||
|
|
@@ -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); | ||
|
|
@@ -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); | ||
|
|
@@ -174,10 +188,7 @@ protected Writer getWriter() | |
| { | ||
| w.write("<html><body><textarea>"); | ||
| } | ||
| catch (IOException x) | ||
| { | ||
|
|
||
| } | ||
| catch (IOException ignored) {} | ||
| } | ||
| return w; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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