Validate 'dir=' parameters

We reject any path element starting with a '.' (mostly to reject '.'
and '..').
This commit is contained in:
Eelco Dolstra 2019-05-01 20:23:39 +02:00
parent ab9e47284a
commit fa88f71520

View file

@ -34,6 +34,11 @@ const static std::string pathRegex = "/?" + segmentRegex + "(?:/" + segmentRegex
// Note: '/' is not a valid query parameter, but so what...
const static std::string paramRegex = "[a-z]+=[/a-zA-Z0-9._-]*";
// 'dir' path elements cannot start with a '.'. We also reject
// potentially dangerous characters like ';'.
const static std::string subDirElemRegex = "(?:[a-zA-Z0-9_-]+[a-zA-Z0-9._-]*)";
const static std::string subDirRegex = subDirElemRegex + "(?:/" + subDirElemRegex + ")*";
FlakeRef::FlakeRef(const std::string & uri, bool allowRelative)
{
// FIXME: could combine this into one regex.
@ -55,6 +60,8 @@ FlakeRef::FlakeRef(const std::string & uri, bool allowRelative)
static std::regex refRegex2(refRegex, std::regex::ECMAScript);
static std::regex subDirRegex2(subDirRegex, std::regex::ECMAScript);
std::cmatch match;
if (std::regex_match(uri.c_str(), match, flakeRegex)) {
IsAlias d;
@ -100,7 +107,8 @@ FlakeRef::FlakeRef(const std::string & uri, bool allowRelative)
throw Error("invalid Git ref '%s'", value);
ref = value;
} else if (name == "dir") {
// FIXME: validate value; should not contain relative paths
if (!std::regex_match(value, subDirRegex2))
throw Error("flake '%s' has invalid subdirectory '%s'", uri, value);
subdir = value;
} else
// FIXME: should probably pass through unknown parameters
@ -124,6 +132,7 @@ FlakeRef::FlakeRef(const std::string & uri, bool allowRelative)
std::string FlakeRef::to_string() const
{
std::string string;
if (auto refData = std::get_if<FlakeRef::IsAlias>(&data))
string = refData->alias;
@ -142,9 +151,12 @@ std::string FlakeRef::to_string() const
else abort();
// FIXME: need to use ?rev etc. for IsGit URIs.
string += (ref ? "/" + *ref : "") +
(rev ? "/" + rev->to_string(Base16, false) : "");
if (subdir != "") string += "?dir=" + subdir;
return string;
}