Skip to content

Commit aa0265f

Browse files
authored
Merge pull request #14656 from NixOS/cleanup-github-attrs
Move GitHub input attribute validation into inputFromAttrs()
2 parents 3c2d5a1 + e7f9578 commit aa0265f

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

src/libfetchers/github.cc

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,16 @@ struct GitArchiveInputScheme : InputScheme
4141
/* This ignores empty path segments for back-compat. Older versions used a tokenizeString here. */
4242
auto path = url.pathSegments(/*skipEmpty=*/true) | std::ranges::to<std::vector<std::string>>();
4343

44-
std::optional<Hash> rev;
44+
std::optional<std::string> rev;
4545
std::optional<std::string> ref;
4646
std::optional<std::string> host_url;
4747

4848
auto size = path.size();
4949
if (size == 3) {
5050
if (std::regex_match(path[2], revRegex))
51-
rev = Hash::parseAny(path[2], HashAlgorithm::SHA1);
52-
else if (isLegalRefName(path[2]))
53-
ref = path[2];
51+
rev = path[2];
5452
else
55-
throw BadURL("in URL '%s', '%s' is not a commit hash or branch/tag name", url, path[2]);
53+
ref = path[2];
5654
} else if (size > 3) {
5755
std::string rs;
5856
for (auto i = std::next(path.begin(), 2); i != path.end(); i++) {
@@ -61,53 +59,40 @@ struct GitArchiveInputScheme : InputScheme
6159
rs += "/";
6260
}
6361
}
64-
65-
if (isLegalRefName(rs)) {
66-
ref = rs;
67-
} else {
68-
throw BadURL("in URL '%s', '%s' is not a branch/tag name", url, rs);
69-
}
62+
ref = rs;
7063
} else if (size < 2)
7164
throw BadURL("URL '%s' is invalid", url);
7265

7366
for (auto & [name, value] : url.query) {
7467
if (name == "rev") {
7568
if (rev)
7669
throw BadURL("URL '%s' contains multiple commit hashes", url);
77-
rev = Hash::parseAny(value, HashAlgorithm::SHA1);
70+
rev = value;
7871
} else if (name == "ref") {
79-
if (!isLegalRefName(value))
80-
throw BadURL("URL '%s' contains an invalid branch/tag name", url);
8172
if (ref)
8273
throw BadURL("URL '%s' contains multiple branch/tag names", url);
8374
ref = value;
84-
} else if (name == "host") {
85-
if (!std::regex_match(value, hostRegex))
86-
throw BadURL("URL '%s' contains an invalid instance host", url);
75+
} else if (name == "host")
8776
host_url = value;
88-
}
8977
// FIXME: barf on unsupported attributes
9078
}
9179

92-
if (ref && rev)
93-
throw BadURL("URL '%s' contains both a commit hash and a branch/tag name %s %s", url, *ref, rev->gitRev());
94-
95-
Input input{};
96-
input.attrs.insert_or_assign("type", std::string{schemeName()});
97-
input.attrs.insert_or_assign("owner", path[0]);
98-
input.attrs.insert_or_assign("repo", path[1]);
80+
Attrs attrs;
81+
attrs.insert_or_assign("type", std::string{schemeName()});
82+
attrs.insert_or_assign("owner", path[0]);
83+
attrs.insert_or_assign("repo", path[1]);
9984
if (rev)
100-
input.attrs.insert_or_assign("rev", rev->gitRev());
85+
attrs.insert_or_assign("rev", *rev);
10186
if (ref)
102-
input.attrs.insert_or_assign("ref", *ref);
87+
attrs.insert_or_assign("ref", *ref);
10388
if (host_url)
104-
input.attrs.insert_or_assign("host", *host_url);
89+
attrs.insert_or_assign("host", *host_url);
10590

10691
auto narHash = url.query.find("narHash");
10792
if (narHash != url.query.end())
108-
input.attrs.insert_or_assign("narHash", narHash->second);
93+
attrs.insert_or_assign("narHash", narHash->second);
10994

110-
return input;
95+
return inputFromAttrs(settings, attrs);
11196
}
11297

11398
const std::map<std::string, AttributeInfo> & allowedAttrs() const override
@@ -154,6 +139,24 @@ struct GitArchiveInputScheme : InputScheme
154139
getStrAttr(attrs, "owner");
155140
getStrAttr(attrs, "repo");
156141

142+
auto ref = maybeGetStrAttr(attrs, "ref");
143+
auto rev = maybeGetStrAttr(attrs, "rev");
144+
if (ref && rev)
145+
throw BadURL(
146+
"input %s contains both a commit hash ('%s') and a branch/tag name ('%s')",
147+
attrsToJSON(attrs),
148+
*rev,
149+
*ref);
150+
151+
if (rev)
152+
Hash::parseAny(*rev, HashAlgorithm::SHA1);
153+
154+
if (ref && !isLegalRefName(*ref))
155+
throw BadURL("input %s contains an invalid branch/tag name", attrsToJSON(attrs));
156+
157+
if (auto host = maybeGetStrAttr(attrs, "host"); host && !std::regex_match(*host, hostRegex))
158+
throw BadURL("input %s contains an invalid instance host", attrsToJSON(attrs));
159+
157160
Input input{};
158161
input.attrs = attrs;
159162
return input;

0 commit comments

Comments
 (0)