diff --git a/.docker/wordpress/entrypoint.sh b/.docker/wordpress/entrypoint.sh index d79ae2c..e5218a5 100644 --- a/.docker/wordpress/entrypoint.sh +++ b/.docker/wordpress/entrypoint.sh @@ -212,10 +212,19 @@ allow_unpinned_entry() { return 0 } -is_commit_sha() { +is_full_sha() { [[ "$1" =~ ^[0-9a-f]{40}$ ]] } +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() { local plugin_slug="$1" local plugin_version="$2" @@ -305,17 +314,13 @@ checkout_git_ref() { local target_dir="$1" local git_url="$2" local git_ref="$3" - local head_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 - head_commit="$(runuser -u www-data -- git -C "$target_dir" rev-parse HEAD)" - if [ "$head_commit" != "$git_ref" ]; then - echo " ✗ $target_dir is at $head_commit instead of $git_ref" - return 1 - fi + if is_full_sha "$git_ref"; then + 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 } @@ -324,10 +329,11 @@ sync_git_checkout() { local target_dir="$2" local git_url="$3" local git_ref="$4" + local fresh_clone=0 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,7 +342,7 @@ 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 + 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 @@ -351,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 c477c11..5a7e7d5 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,9 @@ 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 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: - slug: my-plugin diff --git a/tests/security/pinned-setup-sources.bats b/tests/security/pinned-setup-sources.bats index 01e511a..3f0bb80 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,73 @@ 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 "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