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: Add escape_json function to the serializer #162

Merged
merged 2 commits into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.9.3 (hotfix)

* Escape `args_json` on the fly to avoid null-byte issues

## 0.9.2 (hotfix)

* Change `receiver_id` field type to `String` to be compatible with `nearcore` `AccessKeyPermissionView` struct (it caused problems during AccessKey serialization)
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "indexer-explorer"
version = "0.9.2"
version = "0.9.3"
authors = ["Near Inc <hello@nearprotocol.com>"]
edition = "2018"

Expand Down
24 changes: 23 additions & 1 deletion src/models/serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ pub(crate) fn extract_action_type_and_value_from_action_view(
// args_base64
// See for reference https://github.com/near/near-indexer-for-explorer/issues/87
if let Ok(decoded_args) = base64::decode(args) {
if let Ok(args_json) = serde_json::from_slice(&decoded_args) {
if let Ok(mut args_json) = serde_json::from_slice(&decoded_args) {
escape_json(&mut args_json);
arguments["args_json"] = args_json;
}
}
Expand Down Expand Up @@ -138,3 +139,24 @@ pub(crate) fn extract_action_type_and_value_from_action_view(
),
}
}

/// This function will modify the JSON escaping the values
/// We can not store data with null-bytes in TEXT or JSONB fields
/// of PostgreSQL
/// ref: https://www.commandprompt.com/blog/null-characters-workarounds-arent-good-enough/
fn escape_json(object: &mut serde_json::Value) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add a comment here why this function is necessary (NULL-bytes limitation of PostgreSQL in TEXT and JSONB columns) and reference this post: https://www.commandprompt.com/blog/null-characters-workarounds-arent-good-enough/

match object {
serde_json::Value::Object(ref mut value) => {
for (_key, val) in value {
escape_json(val);
}
}
serde_json::Value::Array(ref mut values) => {
for element in values.iter_mut() {
escape_json(element)
}
}
serde_json::Value::String(ref mut value) => *value = value.escape_default().to_string(),
_ => {}
}
}