Skip to content

Enhance command replacer #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 37 additions & 13 deletions build/wiki-command-replacer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,40 @@ cd "$(dirname "$0")/.."

MARKER_START='{{COMMAND-OUTPUT "'
MARKER_END='"}}'
ALLOWED_COMMANDS=("phpcs" "phpcbf")

tokenize_command() {
read -ra TOKENS <<< "$1"
}

check_allowed_commands() {
local cmd="${TOKENS[0]}"
for allowed in "${ALLOWED_COMMANDS[@]}"; do
[[ "${cmd}" == "${allowed}" ]] && return 0
done

echo >&2 " ERROR: refusing to run arbitrary command: ${cmd}"
exit 1
}

validate_tokens() {
for token in "${TOKENS[@]}"; do
if [[ "${token}" =~ [\;\|\&\$\<\>\`\\] ]]; then
echo >&2 " ERROR: refusing unsafe token: ${token}"
exit 1
fi
done
}

execute_command() {
tokenize_command "$1"
check_allowed_commands
validate_tokens

EXECUTABLE_COMMAND=("${TOKENS[0]}" "${TOKENS[@]:1}")
echo >&2 " INFO: running: " "${EXECUTABLE_COMMAND[@]}"
"${EXECUTABLE_COMMAND[@]}" </dev/null || true
}

if [[ -z "${CI:-}" ]]; then
# The `_wiki` directory is created in a previous GitHub Action step.
Expand All @@ -19,20 +53,10 @@ grep -lrF "${MARKER_START}" _wiki | while read -r file_to_process; do

while IFS=$'\n' read -r line; do
if [[ ${line} = ${MARKER_START}*${MARKER_END} ]]; then
COMMAND="${line##"${MARKER_START}"}"
COMMAND="${COMMAND%%"${MARKER_END}"}"

if [[ "${COMMAND}" != "phpcs "* ]] && [[ "${COMMAND}" != "phpcbf "* ]]; then
echo >&2 " ERROR: refusing to run arbitrary command: ${COMMAND}"
exit 1
fi

#FIXME refuse to run commands with a semicolon / pipe / ampersand / sub-shell
USER_COMMAND="${line##"${MARKER_START}"}"
USER_COMMAND="${USER_COMMAND%%"${MARKER_END}"}"

echo >&2 " INFO: running: ${COMMAND}"
(
eval "${COMMAND}" </dev/null || true
)
execute_command "${USER_COMMAND}"
else
echo "${line}"
fi
Expand Down
Loading