Skip to content

Commit debfc35

Browse files
swissspidyclaude
andcommitted
Validate the cached WordPress versions data before using it
The fetch path checked that a response was a non-empty object before caching it, but the read path only checked that the file was not empty. An unparsable cache was therefore handed straight to jq, which meant a run inside the TTL made no request at all, leaked a jq parse error, and continued with an empty WP_VERSION -- silently dropping the @require-wp-* filtering that the cache was added to protect. Every subsequent run repeated it until the entry aged out. Both paths now share one check, so an unusable cache counts as a miss and the network gets a chance to replace it. The TTL validation moves into get_wp_versions along with it, so that a typo is only reported when a lookup is actually going to happen rather than on every run, and its warning joins the other one on STDERR. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GjJi6F76YZBt7cRrvXS6s6
1 parent 93b00e7 commit debfc35

1 file changed

Lines changed: 22 additions & 10 deletions

File tree

‎bin/run-behat-tests‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,11 @@ WP_VERSIONS_URL="https://raw.githubusercontent.com/wp-cli/wp-cli-tests/artifacts
108108
WP_VERSIONS_CACHE_FILE="${TMPDIR:-/tmp}/wp-cli-test-wp-version-cache/wp-versions.json"
109109
WP_VERSIONS_CACHE_TTL="${WP_CLI_TEST_WP_VERSION_CACHE_TTL:-86400}"
110110

111-
# A typo must not turn into a cache that never expires: the age comparison below
112-
# errors out on a non-numeric TTL, which would then accept the cached copy at
113-
# any age.
114-
if ! is_numeric "${WP_VERSIONS_CACHE_TTL}"; then
115-
echo "Warning: WP_CLI_TEST_WP_VERSION_CACHE_TTL is not a number of seconds, falling back to 86400."
116-
WP_VERSIONS_CACHE_TTL=86400
117-
fi
111+
# The versions data is a non-empty object mapping every release to its status.
112+
# An error page, or a copy that was truncated on its way to disk, is not one.
113+
is_valid_versions_json() {
114+
jq -e 'type == "object" and length > 0' > /dev/null 2>&1
115+
}
118116

119117
# Print the cached versions file if it is younger than the given number of
120118
# seconds. A negative TTL accepts it at any age.
@@ -134,6 +132,11 @@ read_versions_cache() {
134132
[ "${age}" -lt "${ttl}" ] || return 1
135133
fi
136134

135+
# Held to the same standard as a fetched copy, so that an unusable cache
136+
# counts as a miss and the network gets a chance to replace it, instead of
137+
# being served unchecked for the rest of its lifetime.
138+
is_valid_versions_json < "${WP_VERSIONS_CACHE_FILE}" || return 1
139+
137140
cat "${WP_VERSIONS_CACHE_FILE}"
138141
}
139142

@@ -167,8 +170,17 @@ write_versions_cache() {
167170
# to STDERR so that they cannot end up inside the returned JSON.
168171
get_wp_versions() {
169172
local json
173+
local ttl="${WP_VERSIONS_CACHE_TTL}"
174+
175+
# A typo must not turn into a cache that never expires: the age comparison
176+
# errors out on a non-numeric TTL, which would then accept the cached copy
177+
# at any age.
178+
if ! is_numeric "${ttl}"; then
179+
echo "Warning: WP_CLI_TEST_WP_VERSION_CACHE_TTL is not a number of seconds, falling back to 86400." >&2
180+
ttl=86400
181+
fi
170182

171-
json=$( read_versions_cache "${WP_VERSIONS_CACHE_TTL}" )
183+
json=$( read_versions_cache "${ttl}" )
172184
if [ -n "${json}" ]; then
173185
printf '%s' "${json}"
174186
return 0
@@ -179,7 +191,7 @@ get_wp_versions() {
179191
json=$( curl -s --connect-timeout 10 --max-time 30 "${WP_VERSIONS_URL}" )
180192

181193
# Only cache a well-formed response; an error page is not one.
182-
if echo "${json}" | jq -e 'type == "object" and length > 0' > /dev/null 2>&1; then
194+
if printf '%s' "${json}" | is_valid_versions_json; then
183195
write_versions_cache "${json}"
184196
printf '%s' "${json}"
185197
return 0
@@ -201,7 +213,7 @@ if [ "${WP_VERSION-latest}" = "latest" ]; then
201213
WP_VERSION=$( get_wp_versions | jq -r 'to_entries | map( select( .value == "latest" ) ) | last | .key // empty' )
202214

203215
if [ -z "${WP_VERSION}" ]; then
204-
echo "Warning: Could not determine the latest WordPress version. Version-specific tags will not be filtered."
216+
echo "Warning: Could not determine the latest WordPress version. Version-specific tags will not be filtered." >&2
205217
fi
206218

207219
export WP_VERSION

0 commit comments

Comments
 (0)