[Close #1077] fix!: protect secrets and enforce config precedence
Bash / bash-unittest (push) Successful in 19s
Bash / shellcheck (push) Successful in 26s
changelog / changelog (push) Successful in 12s
Helm / helm-lint (push) Successful in 16s
Helm / helm-unittest (push) Successful in 16s
Markdown linter / markdown-lint (push) Successful in 15s
Markdown linter / markdown-link-checker (push) Successful in 53s
Bash / bash-unittest (push) Successful in 19s
Bash / shellcheck (push) Successful in 26s
changelog / changelog (push) Successful in 12s
Helm / helm-lint (push) Successful in 16s
Helm / helm-unittest (push) Successful in 16s
Markdown linter / markdown-lint (push) Successful in 15s
Markdown linter / markdown-link-checker (push) Successful in 53s
Equivalent environment variable encodings could overwrite existing secrets or make configuration precedence depend on environment order. BREAKING CHANGE: Duplicate preset environment variables targeting the same setting now cause initialization to fail. Remove duplicates. Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
3 files changed
+163
-11
No files matched your search
@@ -39,18 +39,19 @@ function env2ini::read_config_to_env() {
|
||||
masked_setting="${masked_setting//-/_0X2D_}"
|
||||
|
||||
if [[ -z "${section}" ]]; then
|
||||
export "GITEA____${masked_setting}=${value}"
|
||||
env2ini::set_config_env "GITEA____${masked_setting}" "${value}"
|
||||
return
|
||||
fi
|
||||
|
||||
local masked_section="${section//./_0X2E_}" # '//' instructs to replace all matches
|
||||
masked_section="${masked_section//-/_0X2D_}"
|
||||
|
||||
export "GITEA__${masked_section^^}__${masked_setting}=${value}"
|
||||
env2ini::set_config_env "GITEA__${masked_section^^}__${masked_setting}" "${value}"
|
||||
}
|
||||
|
||||
function env2ini::reload_preset_envs() {
|
||||
env2ini::log "Reloading preset envs..."
|
||||
local -A preset_names=()
|
||||
|
||||
while read -r line; do
|
||||
if [[ -z "${line}" ]]; then
|
||||
@@ -78,14 +79,22 @@ function env2ini::reload_preset_envs() {
|
||||
|
||||
env2ini::log " + '${setting}'"
|
||||
|
||||
if env2ini::decode_config_env "${setting}"; then
|
||||
local identity="${#gitea_decoded_section}:${gitea_decoded_section}${gitea_decoded_key}"
|
||||
if [[ -v 'preset_names["${identity}"]' ]]; then
|
||||
env2ini::log " ! conflicting preset environment variables: ${preset_names["${identity}"]} and ${setting}"
|
||||
return 1
|
||||
fi
|
||||
preset_names["${identity}"]="${setting}"
|
||||
fi
|
||||
|
||||
local section_and_key="${setting#GITEA__}"
|
||||
if [[ "${section_and_key}" == *'__'* ]]; then
|
||||
local section="${section_and_key%%__*}"
|
||||
local key="${section_and_key#*__}"
|
||||
unset "${setting}"
|
||||
export "GITEA__${section^^}__${key}=${value}"
|
||||
env2ini::set_config_env "GITEA__${section^^}__${key}" "${value}"
|
||||
else
|
||||
export "${setting}=${value}"
|
||||
env2ini::set_config_env "${setting}" "${value}"
|
||||
fi
|
||||
done < "$TMP_EXISTING_ENVS_FILE"
|
||||
|
||||
@@ -141,8 +150,85 @@ function env2ini::generate_initial_secrets() {
|
||||
env2ini::log "...Initial secrets generated\n"
|
||||
}
|
||||
|
||||
function env2ini::decode_config_env() {
|
||||
local encoded="${1#GITEA__}"
|
||||
encoded="${encoded%__FILE}"
|
||||
local escape_regex='_0[xX]([0-9a-fA-F][0-9a-fA-F])+_'
|
||||
local section='' key='' before escape hex byte
|
||||
local in_key=false
|
||||
|
||||
while [[ "${encoded}" =~ ${escape_regex} ]]; do
|
||||
escape="${BASH_REMATCH[0]}"
|
||||
before="${encoded%%"${escape}"*}"
|
||||
if [[ "${in_key}" == false && "${before}" == *'__'* ]]; then
|
||||
section+="${before%%__*}"
|
||||
key+="${before#*__}"
|
||||
in_key=true
|
||||
elif [[ "${in_key}" == false ]]; then
|
||||
section+="${before}"
|
||||
else
|
||||
key+="${before}"
|
||||
fi
|
||||
|
||||
hex="${escape:3:${#escape}-4}"
|
||||
while [[ -n "${hex}" ]]; do
|
||||
if [[ "${hex:0:2}" == 00 ]]; then
|
||||
return 1
|
||||
fi
|
||||
printf -v byte '%b' "\\x${hex:0:2}"
|
||||
if [[ "${in_key}" == false ]]; then
|
||||
section+="${byte}"
|
||||
else
|
||||
key+="${byte}"
|
||||
fi
|
||||
hex="${hex:2}"
|
||||
done
|
||||
encoded="${encoded#*"${escape}"}"
|
||||
done
|
||||
|
||||
if [[ "${in_key}" == false && "${encoded}" == *'__'* ]]; then
|
||||
section+="${encoded%%__*}"
|
||||
key+="${encoded#*__}"
|
||||
elif [[ "${in_key}" == true ]]; then
|
||||
key+="${encoded}"
|
||||
fi
|
||||
|
||||
[[ -n "${key}" ]] || return 1
|
||||
gitea_decoded_section="${section,,}"
|
||||
gitea_decoded_key="${key}"
|
||||
}
|
||||
|
||||
function env2ini::set_config_env() {
|
||||
local name="${1}" value="${2}" existing
|
||||
if env2ini::decode_config_env "${name}"; then
|
||||
local target_section="${gitea_decoded_section}" target_key="${gitea_decoded_key}"
|
||||
while IFS= read -r existing; do
|
||||
if env2ini::decode_config_env "${existing}" &&
|
||||
[[ "${gitea_decoded_section}" == "${target_section}" && "${gitea_decoded_key}" == "${target_key}" ]]; then
|
||||
unset "${existing}"
|
||||
fi
|
||||
done < <(compgen -e | grep '^GITEA__' || true)
|
||||
fi
|
||||
export "${name}=${value}"
|
||||
}
|
||||
|
||||
function env2ini::is_protected_secret() {
|
||||
env2ini::decode_config_env "${1}" || return 1
|
||||
case "${gitea_decoded_section}__${gitea_decoded_key}" in
|
||||
security__INTERNAL_TOKEN|security__SECRET_KEY|oauth2__JWT_SECRET|server__LFS_JWT_SECRET)
|
||||
return 0
|
||||
;;
|
||||
*)
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
# save existing envs prior to script execution. Necessary to keep order of preexisting and custom envs
|
||||
env | (grep -e '^GITEA__' || [[ $? == 1 ]]) > "$TMP_EXISTING_ENVS_FILE"
|
||||
while IFS= read -r setting; do
|
||||
unset "${setting}"
|
||||
done < <(compgen -e | grep '^GITEA__' || true)
|
||||
|
||||
# MUST BE CALLED BEFORE OTHER CONFIGURATION
|
||||
env2ini::generate_initial_secrets
|
||||
@@ -163,10 +249,11 @@ if [ -f "${GITEA_APP_INI}" ]; then
|
||||
env2ini::log ' - oauth2.JWT_SECRET'
|
||||
env2ini::log ' - server.LFS_JWT_SECRET'
|
||||
|
||||
unset GITEA__SECURITY__INTERNAL_TOKEN
|
||||
unset GITEA__SECURITY__SECRET_KEY
|
||||
unset GITEA__OAUTH2__JWT_SECRET
|
||||
unset GITEA__SERVER__LFS_JWT_SECRET
|
||||
while IFS= read -r setting; do
|
||||
if env2ini::is_protected_secret "${setting}"; then
|
||||
unset "${setting}"
|
||||
fi
|
||||
done < <(compgen -e | grep '^GITEA__' || true)
|
||||
fi
|
||||
|
||||
if [[ "${GITEA_REBUILD_APP_INI:-false}" == "true" && -f "${GITEA_APP_INI}" ]]; then
|
||||
|
||||
Reference in new issue
Block a user