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(chore): remove escape answer #2219

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open

Conversation

borngraced
Copy link
Member

@borngraced borngraced commented Sep 13, 2024

To Test

as discussed internally https://matrix.to/#/!yeLsKrHmWqEMEAILHs:matrix.org/$mAxamnv9Ga8rxAg24mXAUwRu9bKQArw2tB_aNaA177c?via=matrix.org, we need to test this doesn't break anything.

@borngraced borngraced self-assigned this Sep 13, 2024
Copy link
Collaborator

@mariocynicys mariocynicys left a comment

Choose a reason for hiding this comment

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

Thanks :)

Comment on lines 285 to 294
let body_bytes = match std::str::from_utf8(&body) {
Ok(body_utf8) => body_utf8.as_bytes().to_vec(),
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();
},
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we really need to check if the bytes could be utf-8ed at this point?
We should trust us.

use mm2_core::mm_ctx::MmArc;
use mm2_err_handle::prelude::*;
use mm2_rpc::mm_protocol::{MmRpcBuilder, MmRpcResponse, MmRpcVersion};
use regex::Regex;
Copy link
Collaborator

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:

# TODO: Reduce the size of regex by disabling the features we don't use.
# cf. https://github.com/rust-lang/regex/issues/583
regex = "1"

Comment on lines -181 to -208
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("&lt;"),
">" => output.push_str("&gt;"),
"&" => output.push_str("&amp;"),
_ => unreachable!(),
}
last_match = end;
}
output.push_str(&input[last_match..]);
Cow::Owned(output)
} else {
input
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Any idea what was the motivation for this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

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

Will wait for @onur-ozkan and @mariocynicys to complete their reviews before merging. Already discussed this with @smk762 in our internal chat so you can add this to the To Test description @borngraced in the PR opening comment so that @smk762 remembers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants