From 6d200ed115e7d21991d94da5ff70692d7e1472ba Mon Sep 17 00:00:00 2001 From: YvesCesar Date: Tue, 17 Nov 2026 17:19:41 -0400 Subject: [PATCH 1/2] fix: accept annotated tag object SHAs as git pins Signed-off-by: YvesCesar --- .docker/wordpress/entrypoint.sh | 24 +++++++++++++++++------- README.md | 3 ++- tests/security/pinned-setup-sources.bats | 20 ++++++++++++++++++++ 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/.docker/wordpress/entrypoint.sh b/.docker/wordpress/entrypoint.sh index d79ae2c..707109c 100644 --- a/.docker/wordpress/entrypoint.sh +++ b/.docker/wordpress/entrypoint.sh @@ -212,10 +212,14 @@ allow_unpinned_entry() { return 0 } -is_commit_sha() { +is_full_sha() { [[ "$1" =~ ^[0-9a-f]{40}$ ]] } +commit_of_ref() { + runuser -u www-data -- git -C "$1" rev-parse --verify --quiet "$2^{commit}" 2>/dev/null +} + install_plugin() { local plugin_slug="$1" local plugin_version="$2" @@ -306,13 +310,15 @@ checkout_git_ref() { local git_url="$2" local git_ref="$3" local head_commit + local pinned_commit runuser -u www-data -- git -C "$target_dir" fetch --quiet "$git_url" "$git_ref" || return 1 runuser -u www-data -- git -C "$target_dir" checkout --quiet --detach FETCH_HEAD || return 1 - if is_commit_sha "$git_ref"; then + if is_full_sha "$git_ref"; then head_commit="$(runuser -u www-data -- git -C "$target_dir" rev-parse HEAD)" - if [ "$head_commit" != "$git_ref" ]; then + pinned_commit="$(commit_of_ref "$target_dir" "$git_ref")" + if [ -z "$pinned_commit" ] || [ "$head_commit" != "$pinned_commit" ]; then echo " ✗ $target_dir is at $head_commit instead of $git_ref" return 1 fi @@ -324,10 +330,11 @@ sync_git_checkout() { local target_dir="$2" local git_url="$3" local git_ref="$4" + local pinned_commit if [ -z "$git_ref" ]; then allow_unpinned_entry "$resource_label" || return 1 - elif ! is_commit_sha "$git_ref"; then + elif ! is_full_sha "$git_ref"; then allow_unpinned_entry "$resource_label" "uses $git_ref, which can move to another commit" || return 1 fi @@ -336,9 +343,12 @@ sync_git_checkout() { echo " ✓ $resource_label already exists" return 0 fi - if [ "$(runuser -u www-data -- git -C "$target_dir" rev-parse HEAD 2>/dev/null)" = "$git_ref" ]; then - echo " ✓ $resource_label is already at $git_ref" - return 0 + if is_full_sha "$git_ref"; then + pinned_commit="$(commit_of_ref "$target_dir" "$git_ref")" + if [ -n "$pinned_commit" ] && [ "$(runuser -u www-data -- git -C "$target_dir" rev-parse HEAD 2>/dev/null)" = "$pinned_commit" ]; then + echo " ✓ $resource_label is already at $git_ref" + return 0 + fi fi echo " ↻ Updating $resource_label to $git_ref..." else diff --git a/README.md b/README.md index c477c11..8e6dd02 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,8 @@ services: # (and on every container start if the plugin directory already exists). # Commands run as www-data from the WordPress root (/var/www/html). # The env var PLUGIN_DIR is set to the absolute plugin directory path. - # Optional: ref pins a full commit SHA. A plugin whose commit does not match is not activated. + # Optional: ref pins a full commit SHA, or the SHA of an annotated tag, which Renovate records for those tags. + # A plugin whose commit does not match is not activated. # A tag or branch is also accepted, but it can move, so the checkout follows it on every start and is reported as unpinned. wordpress_custom_plugins: - slug: my-plugin diff --git a/tests/security/pinned-setup-sources.bats b/tests/security/pinned-setup-sources.bats index 01e511a..1b5a0aa 100644 --- a/tests/security/pinned-setup-sources.bats +++ b/tests/security/pinned-setup-sources.bats @@ -13,6 +13,8 @@ setup() { first_commit="$(git -C "${repository}" rev-parse HEAD)" git -C "${repository}" -c user.name=test -c user.email=test@example.com commit -q --allow-empty -m second second_commit="$(git -C "${repository}" rev-parse HEAD)" + git -C "${repository}" -c user.name=test -c user.email=test@example.com tag -a v2.0.0 -m v2.0.0 + annotated_tag_object="$(git -C "${repository}" rev-parse v2.0.0)" git clone -q --bare "${repository}" "${entrypoint_tmp}/sources/plugin.git" printf '%s\n' 'archive' > "${entrypoint_tmp}/sources/plugin.zip" @@ -37,6 +39,24 @@ plugin_head() { [[ "$(entrypoint_wp_calls)" == *"plugin activate pinned"* ]] } +@test "a custom plugin pinned to an annotated tag object is checked out at the tagged commit" { + local config="WORDPRESS_SETUP_CONFIG_YAML=wordpress_custom_plugins: + - slug: annotated + source: file:///sources/plugin.git + ref: ${annotated_tag_object}" + + [ "${annotated_tag_object}" != "${second_commit}" ] + + run entrypoint_run "${config}" + [ "${status}" -eq 0 ] + [ "$(plugin_head annotated)" = "${second_commit}" ] + [[ "$(entrypoint_wp_calls)" == *"plugin activate annotated"* ]] + + run entrypoint_run "${config}" + [ "${status}" -eq 0 ] + [[ "${output}" == *"annotated is already at ${annotated_tag_object}"* ]] +} + @test "a custom plugin that references a tag follows the tag and is reported as movable" { local config="WORDPRESS_SETUP_CONFIG_YAML=wordpress_custom_plugins: - slug: tagged From 00eeb1bfc7c0b80318e8fa8ff9dfec1b3940e004 Mon Sep 17 00:00:00 2001 From: YvesCesar Date: Tue, 17 Nov 2026 17:37:03 -0400 Subject: [PATCH 2/2] fix: remove fresh clones whose pin fails and keep the pinned object after gc Signed-off-by: YvesCesar --- .docker/wordpress/entrypoint.sh | 34 ++++++++-------- README.md | 1 + tests/security/pinned-setup-sources.bats | 49 ++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 17 deletions(-) diff --git a/.docker/wordpress/entrypoint.sh b/.docker/wordpress/entrypoint.sh index 707109c..e5218a5 100644 --- a/.docker/wordpress/entrypoint.sh +++ b/.docker/wordpress/entrypoint.sh @@ -216,8 +216,13 @@ is_full_sha() { [[ "$1" =~ ^[0-9a-f]{40}$ ]] } -commit_of_ref() { - runuser -u www-data -- git -C "$1" rev-parse --verify --quiet "$2^{commit}" 2>/dev/null +is_at_pinned_commit() { + local target_dir="$1" + local git_ref="$2" + local pinned_commit + + pinned_commit="$(runuser -u www-data -- git -C "$target_dir" rev-parse --verify --quiet "$git_ref^{commit}" 2>/dev/null)" || return 1 + [ "$(runuser -u www-data -- git -C "$target_dir" rev-parse HEAD 2>/dev/null)" = "$pinned_commit" ] } install_plugin() { @@ -309,19 +314,13 @@ checkout_git_ref() { local target_dir="$1" local git_url="$2" local git_ref="$3" - local head_commit - local pinned_commit runuser -u www-data -- git -C "$target_dir" fetch --quiet "$git_url" "$git_ref" || return 1 runuser -u www-data -- git -C "$target_dir" checkout --quiet --detach FETCH_HEAD || return 1 if is_full_sha "$git_ref"; then - head_commit="$(runuser -u www-data -- git -C "$target_dir" rev-parse HEAD)" - pinned_commit="$(commit_of_ref "$target_dir" "$git_ref")" - if [ -z "$pinned_commit" ] || [ "$head_commit" != "$pinned_commit" ]; then - echo " ✗ $target_dir is at $head_commit instead of $git_ref" - return 1 - fi + is_at_pinned_commit "$target_dir" "$git_ref" || return 1 + runuser -u www-data -- git -C "$target_dir" update-ref refs/pinned "$git_ref" fi } @@ -330,7 +329,7 @@ sync_git_checkout() { local target_dir="$2" local git_url="$3" local git_ref="$4" - local pinned_commit + local fresh_clone=0 if [ -z "$git_ref" ]; then allow_unpinned_entry "$resource_label" || return 1 @@ -343,12 +342,9 @@ sync_git_checkout() { echo " ✓ $resource_label already exists" return 0 fi - if is_full_sha "$git_ref"; then - pinned_commit="$(commit_of_ref "$target_dir" "$git_ref")" - if [ -n "$pinned_commit" ] && [ "$(runuser -u www-data -- git -C "$target_dir" rev-parse HEAD 2>/dev/null)" = "$pinned_commit" ]; then - echo " ✓ $resource_label is already at $git_ref" - return 0 - fi + if is_full_sha "$git_ref" && is_at_pinned_commit "$target_dir" "$git_ref"; then + echo " ✓ $resource_label is already at $git_ref" + return 0 fi echo " ↻ Updating $resource_label to $git_ref..." else @@ -361,10 +357,14 @@ sync_git_checkout() { echo " ✓ $resource_label cloned" return 0 fi + fresh_clone=1 fi if ! checkout_git_ref "$target_dir" "$git_url" "$git_ref"; then echo " ✗ Failed to check out $git_ref for $resource_label" + if [ "$fresh_clone" = 1 ]; then + rm -rf "$target_dir" + fi return 1 fi echo " ✓ $resource_label is at $git_ref" diff --git a/README.md b/README.md index 8e6dd02..5a7e7d5 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ services: # Commands run as www-data from the WordPress root (/var/www/html). # The env var PLUGIN_DIR is set to the absolute plugin directory path. # Optional: ref pins a full commit SHA, or the SHA of an annotated tag, which Renovate records for those tags. + # A tag SHA is not a commit SHA: the log shows the tag SHA, and the source must keep that tag to clone it again. # A plugin whose commit does not match is not activated. # A tag or branch is also accepted, but it can move, so the checkout follows it on every start and is reported as unpinned. wordpress_custom_plugins: diff --git a/tests/security/pinned-setup-sources.bats b/tests/security/pinned-setup-sources.bats index 1b5a0aa..3f0bb80 100644 --- a/tests/security/pinned-setup-sources.bats +++ b/tests/security/pinned-setup-sources.bats @@ -57,6 +57,55 @@ plugin_head() { [[ "${output}" == *"annotated is already at ${annotated_tag_object}"* ]] } +@test "an existing checkout moved to an annotated tag pin stays there after gc without its source" { + local plugin_dir="${entrypoint_tmp}/document-root/wp-content/plugins/existing" + local config="WORDPRESS_SETUP_CONFIG_YAML=wordpress_custom_plugins: + - slug: existing + source: file:///sources/plugin.git + ref: ${annotated_tag_object}" + + git clone -q --no-tags "${entrypoint_tmp}/sources/plugin.git" "${plugin_dir}" + git -C "${plugin_dir}" checkout -q --detach "${first_commit}" + + run entrypoint_run "${config}" + [ "${status}" -eq 0 ] + [ "$(plugin_head existing)" = "${second_commit}" ] + + git -C "${plugin_dir}" -c gc.reflogExpire=now -c gc.reflogExpireUnreachable=now reflog expire --all + git -C "${plugin_dir}" gc -q --prune=now + mv "${entrypoint_tmp}/sources/plugin.git" "${entrypoint_tmp}/sources/offline.git" + + : > "${entrypoint_tmp}/log/wp-calls" + run entrypoint_run "${config}" + [ "${status}" -eq 0 ] + [[ "${output}" == *"existing is already at ${annotated_tag_object}"* ]] + [[ "$(entrypoint_wp_calls)" == *"plugin activate existing"* ]] +} + +@test "a fresh clone whose pinned SHA is not a commit is removed and not activated" { + local repository="${entrypoint_tmp}/tree-work" + git init -q -b main "${repository}" + printf '%s\n' ' "${repository}/plugin.php" + git -C "${repository}" add plugin.php + git -C "${repository}" -c user.name=test -c user.email=test@example.com commit -q -m tree + local tree_sha + tree_sha="$(git -C "${repository}" rev-parse 'HEAD^{tree}')" + git clone -q --bare "${repository}" "${entrypoint_tmp}/sources/tree.git" + + run entrypoint_run "WORDPRESS_SETUP_CONFIG_YAML=wordpress_custom_plugins: + - slug: tree + source: file:///sources/tree.git + ref: ${tree_sha} +wordpress_custom_themes: + - slug: tree-theme + source: file:///sources/tree.git + ref: ${tree_sha}" + [ "${status}" -eq 0 ] + [ ! -e "${entrypoint_tmp}/document-root/wp-content/plugins/tree" ] + [ ! -e "${entrypoint_tmp}/document-root/wp-content/themes/tree-theme" ] + [[ "$(entrypoint_wp_calls)" != *"plugin activate tree"* ]] +} + @test "a custom plugin that references a tag follows the tag and is reported as movable" { local config="WORDPRESS_SETUP_CONFIG_YAML=wordpress_custom_plugins: - slug: tagged