Skip to content

Commit b67a05d

Browse files
committed
generate_codeowners: disable path validation by default
1 parent 11ce98e commit b67a05d

3 files changed

Lines changed: 29 additions & 3 deletions

File tree

README_DOCS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ A codeowners-rule represents one or many rows in a CODEOWNERS file.
3232
## generate_codeowners
3333

3434
<pre>
35-
generate_codeowners(<a href="#generate_codeowners-name">name</a>, <a href="#generate_codeowners-generated_comment">generated_comment</a>, <a href="#generate_codeowners-owners">owners</a>)
35+
generate_codeowners(<a href="#generate_codeowners-name">name</a>, <a href="#generate_codeowners-generated_comment">generated_comment</a>, <a href="#generate_codeowners-owners">owners</a>, <a href="#generate_codeowners-validate">validate</a>)
3636
</pre>
3737

3838
Creates a GitHub-compatible CODEOWNERS file based on the `owners`.
@@ -45,5 +45,6 @@ Creates a GitHub-compatible CODEOWNERS file based on the `owners`.
4545
| <a id="generate_codeowners-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
4646
| <a id="generate_codeowners-generated_comment"></a>generated_comment | A comment to insert at the top of the generated file | String | optional | `"# This file was generated by rules_codeowners / Bazel. Don't edit it directly"` |
4747
| <a id="generate_codeowners-owners"></a>owners | A list of codeowners and generate_codeowners. One generate_codeowners can include another generate_codeowners to achieve nested rules. | <a href="https://bazel.build/concepts/labels">List of labels</a> | required | |
48+
| <a id="generate_codeowners-validate"></a>validate | Set to True to enable strict validation of codeowners values. Disabled by default. | Boolean | optional | `False` |
4849

4950

tests/BUILD

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ generate_codeowners(
1616
],
1717
)
1818

19+
generate_codeowners(
20+
name = "github_codeowners_strict",
21+
owners = [
22+
"//tests/hey/sub:codeowners",
23+
"//tests/hey:codeowners",
24+
"//tests/heyoo:codeowners",
25+
":single_pattern_single_team",
26+
":single_pattern_multi_team",
27+
":multi_pattern_single_team",
28+
":multi_pattern_multi_team",
29+
":no_pattern_single_team",
30+
":no_pattern_multi_team",
31+
],
32+
validate=True,
33+
)
34+
1935
codeowners(
2036
name = "single_pattern_single_team",
2137
pattern = "*.a",

tools/codeowners.bzl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def _generate_codeowners_impl(ctx):
7373
arguments = args,
7474
env = {
7575
"OUTFILE": ctx.outputs.outfile.path,
76+
"VALIDATE": "1" if ctx.attr.validate else "0",
7677
},
7778
command = """
7879
set -euo pipefail
@@ -82,9 +83,16 @@ echoerr() {
8283
exit 1
8384
}
8485
85-
prevent_malicios_input () {
86+
prevent_malicious_input () {
8687
must_have_prefix=$1
8788
INPUT=$(cat)
89+
90+
# Skip validation
91+
if [ "$VALIDATE" -eq 0 ]; then
92+
echo "$INPUT"
93+
return 0
94+
fi
95+
8896
set +e
8997
echo -n "$INPUT" | grep -E "${must_have_prefix}" || echoerr "Potentially malicious input detected, path did not match '${must_have_prefix}' (input = '${INPUT}')"
9098
set -e
@@ -110,7 +118,7 @@ while [ "$#" -gt 0 ]; do
110118
cat "$file" | \
111119
skip_comments | \
112120
skip_empty_rows | \
113-
prevent_malicios_input "$must_have_prefix" >> "$OUTFILE"
121+
prevent_malicious_input "$must_have_prefix" >> "$OUTFILE"
114122
done
115123
""".replace("_GENERATED_COMMENT_", ctx.attr.generated_comment),
116124
)
@@ -126,6 +134,7 @@ Creates a GitHub-compatible CODEOWNERS file based on the `owners`.
126134
default = "# This file was generated by rules_codeowners / Bazel. Don't edit it directly",
127135
),
128136
"owners": attr.label_list(mandatory = True, doc = "A list of codeowners and generate_codeowners. One generate_codeowners can include another generate_codeowners to achieve nested rules."),
137+
"validate": attr.bool(default=False, doc = "Set to True to enable strict validation of codeowners values. Disabled by default.", mandatory=False),
129138
},
130139
outputs = {
131140
"outfile": "%{name}.out",

0 commit comments

Comments
 (0)