Skip to content
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

Fix a bug where JSON content is not always validated #804

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

ikhoon
Copy link
Contributor

@ikhoon ikhoon commented Feb 14, 2023

Motivation:

If an invalid JSON content is submitted to Central Dogma Server using REST API, the JSON is not validated and it is saved as is.

The file content is formatted as a text so node.get("content") returns TextNode and it is regarded as a string although it is a JSON object.

if (changeType == ChangeType.UPSERT_JSON) {
return Change.ofJsonUpsert(path, node.get("content"));
}
if (changeType == ChangeType.REMOVE) {
return Change.ofRemoval(path);

Modifications:

  • Pass a raw text value to Change.ofJsonUpsert() and make the JSON validated.

Result:

Central Dogma server now correctly rejects an invalid JSON content.

Motivation:

If an invalid JSON content is submitted to Central Dogma Server using
REST API, the JSON is not validated and it is saved as is.

The file content is formatted as a text so `node.get("content")` returns
`TextNode` and it is regarded as a string although it is a JSON object.
https://github.com/line/centraldogma/blob/9030505a928006d32f3897dc08823dac0d5fd6c2/server/src/main/java/com/linecorp/centraldogma/server/internal/api/converter/ChangesRequestConverter.java#L91-L95

Modifications:

- Pass a raw text value to `Change.ofJsonUpsert()` and make an invalidid
  JSON validated.

Result:

Central Dogma server now correctly rejects an invalid JSON content.
@ikhoon ikhoon added the defect label Feb 14, 2023
@ikhoon ikhoon added this to the 0.60.2 milestone Feb 14, 2023
@ikhoon
Copy link
Contributor Author

ikhoon commented Feb 14, 2023

/cc @rainy789 This will fix the bug where the invalid JSON content is pushed that we talked about before.

@codecov
Copy link

codecov bot commented Feb 15, 2023

Codecov Report

Patch coverage: 88.88% and project coverage change: +0.08 🎉

Comparison is base (975bdda) 65.60% compared to head (52938e9) 65.68%.

Additional details and impacted files
@@             Coverage Diff              @@
##               main     #804      +/-   ##
============================================
+ Coverage     65.60%   65.68%   +0.08%     
- Complexity     3317     3323       +6     
============================================
  Files           355      355              
  Lines         13875    13878       +3     
  Branches       1498     1499       +1     
============================================
+ Hits           9102     9116      +14     
+ Misses         3918     3911       -7     
+ Partials        855      851       -4     
Impacted Files Coverage Δ
...nternal/api/converter/ChangesRequestConverter.java 79.48% <88.88%> (+1.70%) ⬆️

... and 5 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Member

@trustin trustin left a comment

Choose a reason for hiding this comment

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

👍

@trustin trustin changed the title Fix a bug where an JSON content is not validated Fix a bug where JSON content is not always validated Mar 8, 2023
Copy link
Member

@minwoox minwoox left a comment

Choose a reason for hiding this comment

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

Thanks! Left nits and a question. 😄

'{' +
" \"path\" : \"/string.json\"," +
" \"type\" : \"UPSERT_JSON\"," +
" \"content\" : \"json string\"," +
Copy link
Member

Choose a reason for hiding this comment

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

nit: for reviewers

Suggested change
" \"content\" : \"json string\"," +
" \"content\" : \"json string\"," + /* This string is not a JSON string but a plain text. */

return Change.ofJsonUpsert(path, node.get("content"));
if (content.isTextual()) {
// A content can be a serialized JSON so the text value needs to be parsed as JSON.
return Change.ofJsonUpsert(path, content.textValue());
Copy link
Member

Choose a reason for hiding this comment

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

Question: Can we apply this approach that checks if it's textual in the Change.ofJsonUpsert method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"string" is a valid JSON type. So I don't think we can generally apply it to Change.ofJsonUpsert().
This is a special case where a web application sends a stringfied JSON content after editing to the server.

ikhoon and others added 2 commits March 10, 2023 00:37
…/api/ContentServiceV1Test.java

Co-authored-by: minux <songmw725@gmail.com>
@ikhoon ikhoon marked this pull request as draft April 10, 2023 09:26
@ikhoon ikhoon modified the milestones: 0.61.0, 0.61.1 Apr 10, 2023
@ikhoon
Copy link
Contributor Author

ikhoon commented Apr 10, 2023

This PR is still WIP. Changed the milestone to the next one.

@minwoox minwoox modified the milestones: 0.61.1, 0.61.2 May 22, 2023
@ikhoon ikhoon modified the milestones: 0.61.2, 0.61.3 Jun 16, 2023
@jrhee17 jrhee17 modified the milestones: 0.61.3, 0.61.4, 0.61.5 Jun 21, 2023
@minwoox minwoox modified the milestones: 0.61.5, 0.61.6 Jul 26, 2023
@ikhoon ikhoon removed this from the 0.62.0 milestone Aug 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants