mirror of
https://github.com/NixOS/nix
synced 2025-06-24 22:11:15 +02:00
fromJSON/fromTOML: throw if string contains null byte
This commit is contained in:
parent
ab5a9cf2db
commit
3a9d64b8e3
12 changed files with 60 additions and 3 deletions
|
@ -3178,5 +3178,14 @@ std::ostream & operator << (std::ostream & str, const ExternalValueBase & v) {
|
|||
return v.print(str);
|
||||
}
|
||||
|
||||
void forceNoNullByte(std::string_view s)
|
||||
{
|
||||
if (s.find('\0') != s.npos) {
|
||||
using namespace std::string_view_literals;
|
||||
auto str = replaceStrings(std::string(s), "\0"sv, "␀"sv);
|
||||
throw Error("input string '%s' cannot be represented as Nix string because it contains null bytes", str);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -50,6 +50,7 @@ class JSONSax : nlohmann::json_sax<json> {
|
|||
public:
|
||||
void key(string_t & name, EvalState & state)
|
||||
{
|
||||
forceNoNullByte(name);
|
||||
attrs.insert_or_assign(state.symbols.create(name), &value(state));
|
||||
}
|
||||
};
|
||||
|
@ -122,6 +123,7 @@ public:
|
|||
|
||||
bool string(string_t & val) override
|
||||
{
|
||||
forceNoNullByte(val);
|
||||
rs->value(state).mkString(val);
|
||||
rs->add();
|
||||
return true;
|
||||
|
|
|
@ -28,8 +28,10 @@ static void prim_fromTOML(EvalState & state, const PosIdx pos, Value * * args, V
|
|||
|
||||
auto attrs = state.buildBindings(size);
|
||||
|
||||
for(auto & elem : table)
|
||||
for(auto & elem : table) {
|
||||
forceNoNullByte(elem.first);
|
||||
visit(attrs.alloc(elem.first), elem.second);
|
||||
}
|
||||
|
||||
v.mkAttrs(attrs);
|
||||
}
|
||||
|
@ -54,7 +56,11 @@ static void prim_fromTOML(EvalState & state, const PosIdx pos, Value * * args, V
|
|||
v.mkFloat(toml::get<NixFloat>(t));
|
||||
break;;
|
||||
case toml::value_t::string:
|
||||
v.mkString(toml::get<std::string>(t));
|
||||
{
|
||||
auto s = toml::get<std::string_view>(t);
|
||||
forceNoNullByte(s);
|
||||
v.mkString(s);
|
||||
}
|
||||
break;;
|
||||
case toml::value_t::local_datetime:
|
||||
case toml::value_t::offset_datetime:
|
||||
|
@ -66,7 +72,9 @@ static void prim_fromTOML(EvalState & state, const PosIdx pos, Value * * args, V
|
|||
attrs.alloc("_type").mkString("timestamp");
|
||||
std::ostringstream s;
|
||||
s << t;
|
||||
attrs.alloc("value").mkString(toView(s));
|
||||
auto str = toView(s);
|
||||
forceNoNullByte(str);
|
||||
attrs.alloc("value").mkString(str);
|
||||
v.mkAttrs(attrs);
|
||||
} else {
|
||||
throw std::runtime_error("Dates and times are not supported");
|
||||
|
|
|
@ -510,4 +510,6 @@ typedef std::shared_ptr<Value *> RootValue;
|
|||
|
||||
RootValue allocRootValue(Value * v);
|
||||
|
||||
void forceNoNullByte(std::string_view s);
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
error:
|
||||
… while calling the 'fromJSON' builtin
|
||||
at /pwd/lang/eval-fail-fromJSON-keyWithNullByte.nix:1:1:
|
||||
1| builtins.fromJSON ''{"a\u0000b": 1}''
|
||||
| ^
|
||||
2|
|
||||
|
||||
error: input string 'a␀b' cannot be represented as Nix string because it contains null bytes
|
|
@ -0,0 +1 @@
|
|||
builtins.fromJSON ''{"a\u0000b": 1}''
|
|
@ -0,0 +1,8 @@
|
|||
error:
|
||||
… while calling the 'fromJSON' builtin
|
||||
at /pwd/lang/eval-fail-fromJSON-valueWithNullByte.nix:1:1:
|
||||
1| builtins.fromJSON ''"a\u0000b"''
|
||||
| ^
|
||||
2|
|
||||
|
||||
error: input string 'a␀b' cannot be represented as Nix string because it contains null bytes
|
|
@ -0,0 +1 @@
|
|||
builtins.fromJSON ''"a\u0000b"''
|
|
@ -0,0 +1,8 @@
|
|||
error:
|
||||
… while calling the 'fromTOML' builtin
|
||||
at /pwd/lang/eval-fail-fromTOML-keyWithNullByte.nix:1:1:
|
||||
1| builtins.fromTOML ''"a\u0000b" = 1''
|
||||
| ^
|
||||
2|
|
||||
|
||||
error: while parsing TOML: error: input string 'a␀b' cannot be represented as Nix string because it contains null bytes
|
|
@ -0,0 +1 @@
|
|||
builtins.fromTOML ''"a\u0000b" = 1''
|
|
@ -0,0 +1,8 @@
|
|||
error:
|
||||
… while calling the 'fromTOML' builtin
|
||||
at /pwd/lang/eval-fail-fromTOML-valueWithNullByte.nix:1:1:
|
||||
1| builtins.fromTOML ''k = "a\u0000b"''
|
||||
| ^
|
||||
2|
|
||||
|
||||
error: while parsing TOML: error: input string 'a␀b' cannot be represented as Nix string because it contains null bytes
|
|
@ -0,0 +1 @@
|
|||
builtins.fromTOML ''k = "a\u0000b"''
|
Loading…
Add table
Add a link
Reference in a new issue