forked from jl777/SuperNET
-
Notifications
You must be signed in to change notification settings - Fork 94
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(native-rpc): remove escaped response body #2219
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,14 +28,11 @@ use futures::future::{join_all, FutureExt}; | |
use http::header::{HeaderValue, ACCESS_CONTROL_ALLOW_ORIGIN, CONTENT_TYPE}; | ||
use http::request::Parts; | ||
use http::{Method, Request, Response, StatusCode}; | ||
use lazy_static::lazy_static; | ||
use mm2_core::mm_ctx::MmArc; | ||
use mm2_err_handle::prelude::*; | ||
use mm2_rpc::mm_protocol::{MmRpcBuilder, MmRpcResponse, MmRpcVersion}; | ||
use regex::Regex; | ||
use serde::Serialize; | ||
use serde_json::{self as json, Value as Json}; | ||
use std::borrow::Cow; | ||
use std::net::SocketAddr; | ||
|
||
cfg_native! { | ||
|
@@ -178,35 +175,6 @@ fn response_from_dispatcher_error( | |
response.serialize_http_response() | ||
} | ||
|
||
pub fn escape_answer<'a, S: Into<Cow<'a, str>>>(input: S) -> Cow<'a, str> { | ||
lazy_static! { | ||
static ref REGEX: Regex = Regex::new("[<>&]").unwrap(); | ||
} | ||
|
||
let input = input.into(); | ||
let mut last_match = 0; | ||
|
||
if REGEX.is_match(&input) { | ||
let matches = REGEX.find_iter(&input); | ||
let mut output = String::with_capacity(input.len()); | ||
for mat in matches { | ||
let (begin, end) = (mat.start(), mat.end()); | ||
output.push_str(&input[last_match..begin]); | ||
match &input[begin..end] { | ||
"<" => output.push_str("<"), | ||
">" => output.push_str(">"), | ||
"&" => output.push_str("&"), | ||
_ => unreachable!(), | ||
} | ||
last_match = end; | ||
} | ||
output.push_str(&input[last_match..]); | ||
Cow::Owned(output) | ||
} else { | ||
input | ||
} | ||
} | ||
Comment on lines
-181
to
-208
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. Any idea what was the motivation for this? 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. |
||
|
||
async fn process_single_request(ctx: MmArc, req: Json, client: SocketAddr) -> Result<Response<Vec<u8>>, String> { | ||
let local_only = ctx.conf["rpc_local_only"].as_bool().unwrap_or(true); | ||
if req["mmrpc"].is_null() { | ||
|
@@ -314,23 +282,9 @@ async fn rpc_service(req: Request<Body>, ctx_h: u32, client: SocketAddr) -> Resp | |
|
||
let res = try_sf!(process_rpc_request(ctx, req, req_json, client).await, ACCESS_CONTROL_ALLOW_ORIGIN => rpc_cors); | ||
let (mut parts, body) = res.into_parts(); | ||
let body_escaped = { | ||
let body_utf8 = match std::str::from_utf8(&body) { | ||
Ok(body_utf8) => body_utf8, | ||
Err(_) => { | ||
return Response::builder() | ||
.status(500) | ||
.header(ACCESS_CONTROL_ALLOW_ORIGIN, rpc_cors) | ||
.header(CONTENT_TYPE, APPLICATION_JSON) | ||
.body(Body::from(err_to_rpc_json_string("Non UTF-8 output"))) | ||
.unwrap(); | ||
}, | ||
}; | ||
let escaped = escape_answer(body_utf8); | ||
escaped.as_bytes().to_vec() | ||
}; | ||
parts.headers.insert(ACCESS_CONTROL_ALLOW_ORIGIN, rpc_cors); | ||
Response::from_parts(parts, Body::from(body_escaped)) | ||
|
||
Response::from_parts(parts, Body::from(body)) | ||
} | ||
|
||
// TODO: This should exclude TCP internals, as including them results in having to | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
we dropped the only usage of regex here,
we can also drop it in:
komodo-defi-framework/mm2src/mm2_main/Cargo.toml
Lines 80 to 82 in 079ea5e