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:
Benjamin Gilbert 2022-10-18 00:44:37 -04:00 committed by Benjamin Gilbert
parent e05d480a82
commit 656af75c40

View file

@ -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