Skip to content

Commit 14f029d

Browse files
authored
Merge pull request #11489 from bryanhonof/bryanhonof.warn-on-malformed-uri-query
fix: warn on malformed URI query parameter
2 parents c116030 + 5150a96 commit 14f029d

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

src/libflake/flake/flakeref.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ std::pair<FlakeRef, std::string> parsePathFlakeRefWithFragment(
8888
if (fragmentStart != std::string::npos) {
8989
fragment = percentDecode(url.substr(fragmentStart+1));
9090
}
91-
if (pathEnd != std::string::npos && fragmentStart != std::string::npos) {
91+
if (pathEnd != std::string::npos && fragmentStart != std::string::npos && url[pathEnd] == '?') {
9292
query = decodeQuery(url.substr(pathEnd+1, fragmentStart-pathEnd-1));
9393
}
9494

src/libutil/url.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,14 @@ std::map<std::string, std::string> decodeQuery(const std::string & query)
7979

8080
for (auto s : tokenizeString<Strings>(query, "&")) {
8181
auto e = s.find('=');
82-
if (e != std::string::npos)
83-
result.emplace(
84-
s.substr(0, e),
85-
percentDecode(std::string_view(s).substr(e + 1)));
82+
if (e == std::string::npos) {
83+
warn("dubious URI query '%s' is missing equal sign '%s', ignoring", s, "=");
84+
continue;
85+
}
86+
87+
result.emplace(
88+
s.substr(0, e),
89+
percentDecode(std::string_view(s).substr(e + 1)));
8690
}
8791

8892
return result;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env bash
2+
3+
source ./common.sh
4+
5+
requireGit
6+
7+
repoDir="$TEST_ROOT/repo"
8+
createGitRepo "$repoDir"
9+
createSimpleGitFlake "$repoDir"
10+
11+
# Check that a flakeref without a query is accepted correctly.
12+
expectStderr 0 nix --offline build --dry-run "git+file://$repoDir#foo"
13+
14+
# Check that a flakeref with a good query is accepted correctly.
15+
expectStderr 0 nix --offline build --dry-run "git+file://$repoDir?foo=bar#foo"
16+
17+
# Check that we get the dubious query warning, when passing in a query without an equal sign.
18+
expectStderr 0 nix --offline build --dry-run "git+file://$repoDir?bar#foo" \
19+
| grepQuiet "warning: dubious URI query 'bar' is missing equal sign '=', ignoring"
20+
21+
# Check that the anchor (#) is taken as a whole, not split, and throws an error.
22+
expectStderr 1 nix --offline build --dry-run "git+file://$repoDir#foo?bar" \
23+
| grepQuiet "error: flake 'git+file://$repoDir' does not provide attribute 'packages.$system.foo?bar', 'legacyPackages.$system.foo?bar' or 'foo?bar'"
24+
25+
# Check that a literal `?` in the query doesn't print dubious query warning.
26+
expectStderr 0 nix --offline build --dry-run "git+file://$repoDir?#foo" \
27+
| grepInverse "warning: dubious URI query "
28+
29+
# Check that a literal `?=` in the query doesn't print dubious query warning.
30+
expectStderr 0 nix --offline build --dry-run "git+file://$repoDir?=#foo" \
31+
| grepInverse "warning: dubious URI query "

tests/functional/flakes/local.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ flake-tests := \
1919
$(d)/eval-cache.sh \
2020
$(d)/search-root.sh \
2121
$(d)/config.sh \
22-
$(d)/show.sh
22+
$(d)/show.sh \
23+
$(d)/dubious-query.sh
2324

2425
install-tests-groups += flake

tests/functional/flakes/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ suites += {
2323
'search-root.sh',
2424
'config.sh',
2525
'show.sh',
26+
'dubious-query.sh',
2627
],
2728
'workdir': meson.current_build_dir(),
2829
}

0 commit comments

Comments
 (0)