mirror of
https://github.com/NixOS/nix.git
synced 2025-11-16 07:22:43 +01:00
Remove validation of URLs passed to FileTransferRequest verbatim
CURL is not very strict about validation of URLs passed to it. We should reflect this in our handling of URLs that we get from the user in <nix/fetchurl.nix> or builtins.fetchurl. ValidURL was an attempt to rectify this, but it turned out to be too strict. The only good way to resolve this is to pass (in some cases) the user-provided string verbatim to CURL. Other usages in libfetchers still benefit from using structured ParsedURL and validation though. nix store prefetch-file --name foo 'https://cdn.skypack.dev/big.js@^5.2.2' error: 'https://cdn.skypack.dev/big.js@^5.2.2' is not a valid URL: leftover
This commit is contained in:
parent
0f85ef3677
commit
47f427a172
7 changed files with 44 additions and 37 deletions
|
|
@ -6,6 +6,9 @@
|
|||
|
||||
#include "nix/util/error.hh"
|
||||
#include "nix/util/canon-path.hh"
|
||||
#include "nix/util/split.hh"
|
||||
#include "nix/util/util.hh"
|
||||
#include "nix/util/variant-wrapper.hh"
|
||||
|
||||
namespace nix {
|
||||
|
||||
|
|
@ -342,8 +345,7 @@ ParsedURL fixGitURL(const std::string & url);
|
|||
bool isValidSchemeName(std::string_view scheme);
|
||||
|
||||
/**
|
||||
* Either a ParsedURL or a verbatim string, but the string must be a valid
|
||||
* ParsedURL. This is necessary because in certain cases URI must be passed
|
||||
* Either a ParsedURL or a verbatim string. This is necessary because in certain cases URI must be passed
|
||||
* verbatim (e.g. in builtin fetchers), since those are specified by the user.
|
||||
* In those cases normalizations performed by the ParsedURL might be surprising
|
||||
* and undesirable, since Nix must be a universal client that has to work with
|
||||
|
|
@ -354,23 +356,23 @@ bool isValidSchemeName(std::string_view scheme);
|
|||
*
|
||||
* Though we perform parsing and validation for internal needs.
|
||||
*/
|
||||
struct ValidURL : private ParsedURL
|
||||
struct VerbatimURL
|
||||
{
|
||||
std::optional<std::string> encoded;
|
||||
using Raw = std::variant<std::string, ParsedURL>;
|
||||
Raw raw;
|
||||
|
||||
ValidURL(std::string str)
|
||||
: ParsedURL(parseURL(str, /*lenient=*/false))
|
||||
, encoded(std::move(str))
|
||||
VerbatimURL(std::string_view s)
|
||||
: raw(std::string{s})
|
||||
{
|
||||
}
|
||||
|
||||
ValidURL(std::string_view str)
|
||||
: ValidURL(std::string{str})
|
||||
VerbatimURL(std::string s)
|
||||
: raw(std::move(s))
|
||||
{
|
||||
}
|
||||
|
||||
ValidURL(ParsedURL parsed)
|
||||
: ParsedURL{std::move(parsed)}
|
||||
VerbatimURL(ParsedURL url)
|
||||
: raw(std::move(url))
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -379,25 +381,35 @@ struct ValidURL : private ParsedURL
|
|||
*/
|
||||
std::string to_string() const
|
||||
{
|
||||
return encoded.or_else([&]() -> std::optional<std::string> { return ParsedURL::to_string(); }).value();
|
||||
return std::visit(
|
||||
overloaded{
|
||||
[](const std::string & str) { return str; }, [](const ParsedURL & url) { return url.to_string(); }},
|
||||
raw);
|
||||
}
|
||||
|
||||
const ParsedURL & parsed() const &
|
||||
const ParsedURL parsed() const
|
||||
{
|
||||
return *this;
|
||||
return std::visit(
|
||||
overloaded{
|
||||
[](const std::string & str) { return parseURL(str); }, [](const ParsedURL & url) { return url; }},
|
||||
raw);
|
||||
}
|
||||
|
||||
std::string_view scheme() const &
|
||||
{
|
||||
return ParsedURL::scheme;
|
||||
}
|
||||
|
||||
const auto & path() const &
|
||||
{
|
||||
return ParsedURL::path;
|
||||
return std::visit(
|
||||
overloaded{
|
||||
[](std::string_view str) {
|
||||
auto scheme = splitPrefixTo(str, ':');
|
||||
if (!scheme)
|
||||
throw BadURL("URL '%s' doesn't have a scheme", str);
|
||||
return *scheme;
|
||||
},
|
||||
[](const ParsedURL & url) -> std::string_view { return url.scheme; }},
|
||||
raw);
|
||||
}
|
||||
};
|
||||
|
||||
std::ostream & operator<<(std::ostream & os, const ValidURL & url);
|
||||
std::ostream & operator<<(std::ostream & os, const VerbatimURL & url);
|
||||
|
||||
} // namespace nix
|
||||
|
|
|
|||
|
|
@ -434,7 +434,7 @@ bool isValidSchemeName(std::string_view s)
|
|||
return std::regex_match(s.begin(), s.end(), regex, std::regex_constants::match_default);
|
||||
}
|
||||
|
||||
std::ostream & operator<<(std::ostream & os, const ValidURL & url)
|
||||
std::ostream & operator<<(std::ostream & os, const VerbatimURL & url)
|
||||
{
|
||||
os << url.to_string();
|
||||
return os;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue