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: warn on malformed URI query parameter #11489

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/libflake/flake/flakeref.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ std::pair<FlakeRef, std::string> parsePathFlakeRefWithFragment(
if (fragmentStart != std::string::npos) {
fragment = percentDecode(url.substr(fragmentStart+1));
}
if (pathEnd != std::string::npos && fragmentStart != std::string::npos) {
if (pathEnd != std::string::npos && fragmentStart != std::string::npos && url[pathEnd] == '?') {
Copy link
Member Author

Choose a reason for hiding this comment

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

It was possible to pass the fragment as a "query" with the following flakeRef.

.#bla

I don't think this was expected behavior, since the above doesn't have a query.

query = decodeQuery(url.substr(pathEnd+1, fragmentStart-pathEnd-1));
}

Expand Down
12 changes: 8 additions & 4 deletions src/libutil/url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,14 @@ std::map<std::string, std::string> decodeQuery(const std::string & query)

for (auto s : tokenizeString<Strings>(query, "&")) {
auto e = s.find('=');
if (e != std::string::npos)
result.emplace(
s.substr(0, e),
percentDecode(std::string_view(s).substr(e + 1)));
if (e == std::string::npos) {
warn("dubious URI query '%s' is missing equal sign '%s', ignoring", s, "=");
continue;
}

result.emplace(
s.substr(0, e),
percentDecode(std::string_view(s).substr(e + 1)));
}

return result;
Expand Down
23 changes: 23 additions & 0 deletions tests/functional/flakes/dubious-query.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

source ./common.sh

requireGit

nixpkgsDir="$TEST_ROOT/nixpkgs"
createGitRepo "$nixpkgsDir"
createSimpleGitFlake "$nixpkgsDir"

# Check that a flakeref without a query is accepted correctly.
expectStderr 0 nix --offline build --dry-run "git+file://$nixpkgsDir#foo"

# Check that a flakeref with a good query is accepted correctly.
expectStderr 0 nix --offline build --dry-run "git+file://$nixpkgsDir?foo=bar#foo"

# Check that we get the dubious query warning, when passing in a query without an equal sign.
expectStderr 0 nix --offline build --dry-run "git+file://$nixpkgsDir?bar#foo" \
| grepQuiet "warning: dubious URI query 'bar' is missing equal sign '=', ignoring"

# Check that the anchor (#) is taken as a whole, not split, and throws an error.
expectStderr 1 nix --offline build --dry-run "git+file://$nixpkgsDir#foo?bar" \
| grepQuiet "error: flake 'git+file://$nixpkgsDir' does not provide attribute 'packages.$system.foo?bar', 'legacyPackages.$system.foo?bar' or 'foo?bar'"
3 changes: 2 additions & 1 deletion tests/functional/flakes/local.mk
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ flake-tests := \
$(d)/eval-cache.sh \
$(d)/search-root.sh \
$(d)/config.sh \
$(d)/show.sh
$(d)/show.sh \
$(d)/dubious-query.sh

install-tests-groups += flake
1 change: 1 addition & 0 deletions tests/functional/flakes/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ suites += {
'search-root.sh',
'config.sh',
'show.sh',
'dubious-query.sh',
],
'workdir': meson.current_build_dir(),
}
Loading