Skip to content

Commit 4d98b9c

Browse files
ppom0lovasoa
andauthored
Remove url-encoded surrounding quotes to URL parameters (#904)
* Remove url-encoded surrounding quotes to URL parameters Fix #879 * fix special character handling --------- Co-authored-by: ppom <> Co-authored-by: lovasoa <[email protected]>
1 parent 29f6447 commit 4d98b9c

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/webserver/database/sqlpage_functions/url_parameter_deserializer.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use percent_encoding::{percent_encode, NON_ALPHANUMERIC};
22
use serde::{Deserialize, Deserializer};
3+
use serde_json::Value;
34
use std::borrow::Cow;
45
use std::fmt;
56

@@ -59,7 +60,12 @@ impl<'de> Deserialize<'de> for URLParameters {
5960
out.encode_and_push(&key);
6061
out.0.push_str("[]");
6162
out.0.push('=');
62-
out.encode_and_push(&val.to_string());
63+
64+
let val = match val {
65+
Value::String(s) => s,
66+
other => other.to_string(),
67+
};
68+
out.encode_and_push(&val);
6369
}
6470
} else {
6571
out.push_kv(&key, value);
@@ -89,3 +95,30 @@ fn test_url_parameters_deserializer() {
8995
"x=hello%20world&num=123&arr[]=1&arr[]=2&arr[]=3"
9096
);
9197
}
98+
99+
#[test]
100+
fn test_url_parameters_deserializer_special_chars() {
101+
use serde_json::json;
102+
let json = json!({
103+
"chars": ["\n", " ", "\""],
104+
});
105+
106+
let url_parameters: URLParameters = serde_json::from_value(json).unwrap();
107+
assert_eq!(url_parameters.0, "chars[]=%0A&chars[]=%20&chars[]=%22");
108+
}
109+
110+
#[test]
111+
fn test_url_parameters_deserializer_issue_879() {
112+
use serde_json::json;
113+
let json = json!({
114+
"name": "John Doe & Son's",
115+
"items": [1, "item 2 & 3", true],
116+
"special_char": "%&=+ ",
117+
});
118+
119+
let url_parameters: URLParameters = serde_json::from_value(json).unwrap();
120+
assert_eq!(
121+
url_parameters.0,
122+
"name=John%20Doe%20%26%20Son%27s&items[]=1&items[]=item%202%20%26%203&items[]=true&special%5Fchar=%25%26%3D%2B%20"
123+
);
124+
}

0 commit comments

Comments
 (0)