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: Fix json escaping in header values #4031

Merged
merged 3 commits into from
Dec 9, 2024
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
22 changes: 22 additions & 0 deletions e2e/tests-dfx/assetscanister.bash
Original file line number Diff line number Diff line change
Expand Up @@ -1897,6 +1897,28 @@ WARN: {
assert_match '/somedir/upload-me.txt 1/1 \(8 bytes\) sha [0-9a-z]* \(with cache and 1 header\)'
}

@test "asset configuration via .ic-assets.json5 - respects weird characters" {
install_asset assetscanister
touch src/e2e_project_frontend/assets/thing.txt
cat <<'EOF' >src/e2e_project_frontend/assets/.ic-assets.json5
Copy link
Contributor

Choose a reason for hiding this comment

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

I suppose it's not super useful to add a json ic-assets test also since it seems the json and json5 code paths are the same.

[
{
"match": "thing.txt",
"headers": {
"X-Dummy-Header": "\"\'@%({[~$?\\"
}
}
]
EOF
dfx_start
assert_command dfx deploy
ID=$(dfx canister id e2e_project_frontend)
PORT=$(get_webserver_port)
assert_command curl --head "http://localhost:$PORT/thing.txt?canisterId=$ID"
# shellcheck disable=SC1003
assert_contains 'x-dummy-header: "'"'"'@%({[~$?\'
}

@test "uses selected canister wasm" {
dfx_start
use_asset_wasm 0.12.1
Expand Down
31 changes: 24 additions & 7 deletions src/canisters/frontend/ic-asset/src/asset/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ mod rule_utils {
use crate::error::LoadRuleError;
use globset::{Glob, GlobMatcher};
use itertools::Itertools;
use serde::{Deserialize, Serializer};
use serde::{de::Error as _, Deserialize, Serializer};
use serde_json::Value;
use std::collections::BTreeMap;
use std::fmt;
Expand Down Expand Up @@ -387,12 +387,29 @@ mod rule_utils {
where
D: serde::Deserializer<'de>,
{
match serde_json::value::Value::deserialize(deserializer)? {
Value::Object(v) => Ok(Maybe::Value(
v.into_iter()
.map(|(k, v)| (k, v.to_string().trim_matches('"').to_string()))
.collect::<BTreeMap<String, String>>(),
)),
match Value::deserialize(deserializer)? {
Value::Object(v) => {
Ok(Maybe::Value(
v.into_iter()
.map(|(k, v)| {
Ok((
k,
match v {
Value::Bool(b) => b.to_string(),
Value::Number(n) => n.to_string(),
Value::String(s) => s, // v.to_string() would json-escape this
Value::Null => String::new(),
v => {
return Err(D::Error::custom(format!(
"headers must be strings, numbers, or bools (was {v:?})"
)))
}
},
))
})
.collect::<Result<BTreeMap<String, String>, D::Error>>()?,
))
}
Value::Null => Ok(Maybe::Null),
_ => Err(serde::de::Error::custom(
"wrong data format for field `headers` (only map or null are allowed)",
Expand Down
Loading