-
Notifications
You must be signed in to change notification settings - Fork 284
152 lines (122 loc) · 5.53 KB
/
Copy pathufs_repo_checks.yaml
File metadata and controls
152 lines (122 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
name: UFS Custom Checks
on:
push:
branches: ['**']
pull_request:
branches: [develop]
defaults:
run:
shell: bash -eo pipefail {0}
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
enforce-standards:
name: Enforce Repo Standards
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 #v7.0.1
- name: Check for Bash Octal Traps
run: |
echo "Scanning for potential octal traps in tests/default_vars.sh and tests/tests/*"
mapfile -d '' CHECK_FILES < <(find tests/default_vars.sh tests/tests -type f -print0)
if [[ ${#CHECK_FILES[@]} -eq 0 ]]; then
echo "::error::No files found to scan for octal traps."
exit 1
fi
# Build the set of zero-padded variables from assignments.
# SHOUR is intentionally zero-padded because tests/fv3_conf and tests/parm
# templates consume it as a two-character hour token (e.g., 06). Arithmetic
# uses of SHOUR are handled explicitly in code (10#... or printf), so SHOUR
# is excluded from this generic trap check.
PADDED_VARS=$(perl -ne '
next if /^\s*#/;
if (/^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(?:"|\047)?0[0-9]+(?:"|\047)?\s*(?:#.*)?$/) {
print "$1\n";
}
' "${CHECK_FILES[@]}" | sort -u | grep -v '^SHOUR$' || true)
if [[ -z "${PADDED_VARS}" ]]; then
echo "✅ No zero-padded arithmetic candidate variables found (other than SHOUR)."
exit 0
fi
PADDED_RE=$(printf '%s\n' "${PADDED_VARS}" | paste -sd'|' -)
# Flag uses of zero-padded vars in arithmetic contexts unless explicitly protected by 10#.
UNPROTECTED=$(PADDED_RE="${PADDED_RE}" perl -ne '
our $re;
BEGIN { $re = $ENV{PADDED_RE} // q||; }
next if $re eq q||;
next if /^\s*#/;
my $line = $_;
my $flagged = 0;
while ($line =~ /\$\(\((.*?)\)\)/g) {
my $expr = $1;
while ($expr =~ /(\$\{?([A-Za-z_][A-Za-z0-9_]*)\}?|\b([A-Za-z_][A-Za-z0-9_]*)\b)/g) {
my $tok = $1;
my $var = defined($2) ? $2 : $3;
next unless defined($var) && $var =~ /^(?:$re)$/;
my $start = $-[1];
my $before = substr($expr, 0, $start);
# If explicitly prefixed with 10#, this use is safe.
next if $before =~ /10#\s*$/;
# Ignore bare-token submatches that are actually part of $VAR or ${VAR}.
if (defined $3) {
my $prev1 = $start > 0 ? substr($expr, $start - 1, 1) : q||;
my $prev2 = $start > 1 ? substr($expr, $start - 2, 2) : q||;
next if $prev1 eq q|$| || $prev2 eq q|${|;
}
$flagged = 1;
last;
}
last if $flagged;
}
print "$ARGV:$.:$_" if $flagged;
' "${CHECK_FILES[@]}")
if [[ -n "${UNPROTECTED}" ]]; then
echo "::error::🚨 OCTAL TRAP DETECTED! Zero-padded vars used in arithmetic must be prefixed with 10#."
echo "Zero-padded vars under check: ${PADDED_VARS//$'\n'/, }"
echo "The following lines are missing 10# protection:"
echo "${UNPROTECTED}"
exit 1
fi
echo "✅ No unprotected arithmetic uses found for zero-padded vars: ${PADDED_VARS//$'\n'/, }"
- name: Ban expr in test configuration files
run: |
echo "Scanning for illegal 'expr' usage in tests/default_vars.sh and tests/tests/*..."
mapfile -d '' CHECK_FILES < <(find tests/default_vars.sh tests/tests -type f -print0)
if [[ ${#CHECK_FILES[@]} -eq 0 ]]; then
echo "::error::No files found to scan for expr usage."
exit 1
fi
ILLEGAL_EXPR=$(perl -ne 'next if /^\s*#/; print "$ARGV:$.:$_" if /\bexpr\b/' "${CHECK_FILES[@]}")
if [[ -n "${ILLEGAL_EXPR}" ]]; then
echo "::error::🚨 EXPR DETECTED! Do not use 'expr' in tests/default_vars.sh or tests/tests/*."
echo "The following lines contain illegal expr usage:"
echo "${ILLEGAL_EXPR}"
exit 1
fi
echo "✅ No illegal expr usage found!"
- name: Ban 'export' in configuration files
run: |
echo "Scanning for illegal 'export' declarations in tests/default_vars.sh and tests/tests/*..."
mapfile -d '' CHECK_FILES < <(find tests/default_vars.sh tests/tests -type f -print0)
if [[ ${#CHECK_FILES[@]} -eq 0 ]]; then
echo "::error::No files found to scan for export declarations."
exit 1
fi
# Strip inline comments before evaluating for the word 'export'
ILLEGAL_EXPORTS=$(perl -ne '
next if /^\s*#/;
my $clean_line = $_;
$clean_line =~ s/(^|\s)#.*//;
print "$ARGV:$.:$_" if $clean_line =~ /\bexport\b(?=\s)/;
' "${CHECK_FILES[@]}")
if [[ -n "$ILLEGAL_EXPORTS" ]]; then
echo "::error::🚨 EXPORT DETECTED! Do not use 'export' in tests/default_vars.sh or tests/tests/*."
echo "The following lines contain illegal exports:"
echo "$ILLEGAL_EXPORTS"
exit 1
else
echo "✅ No illegal exports found!"
exit 0
fi