mirror of
https://github.com/coreos/fedora-coreos-config.git
synced 2026-11-03 22:59:47 -09:00
ci: detect unbracketed if expressions in shell scripts
Fail if `if $` or `if "$` appears in a shell script, since `if [ $` may have been intended and the test will always return false if so. This will generate false positives if a command is being run indirectly via a shell variable.
This commit is contained in:
parent
e05d480a82
commit
656af75c40
1 changed files with 5 additions and 0 deletions
|
|
@ -2,6 +2,7 @@
|
|||
# Validate basic syntax of shell script and yaml.
|
||||
|
||||
import os
|
||||
import re
|
||||
import stat
|
||||
import subprocess
|
||||
import yaml
|
||||
|
|
@ -31,8 +32,12 @@ def validate_initrd_service(rootfd, name):
|
|||
)
|
||||
|
||||
|
||||
BASH_UNBRACKETED_IF = re.compile(r'\sif\s+"?\$')
|
||||
def validate_shell(rootfd, name):
|
||||
subprocess.check_call(['bash', '-n', name], preexec_fn=lambda: os.fchdir(rootfd))
|
||||
with openat(rootfd, name) as fh:
|
||||
if BASH_UNBRACKETED_IF.search(fh.read()):
|
||||
raise Exception(f'Possible unbracketed conditional in {name}')
|
||||
global validated
|
||||
validated +=1
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue