diff --git a/hack/tools/update-tls-profiles.sh b/hack/tools/update-tls-profiles.sh index 29b8d8790..01618a318 100755 --- a/hack/tools/update-tls-profiles.sh +++ b/hack/tools/update-tls-profiles.sh @@ -8,21 +8,43 @@ if [ -z "${JQ}" ]; then fi OUTPUT=internal/shared/util/tlsprofiles/mozilla_data.go -INPUT=https://ssl-config.mozilla.org/guidelines/5.8.json +INPUT=https://ssl-config.mozilla.org/guidelines/latest.json TMPFILE="$(mktemp)" trap 'rm -rf "$TMPFILE"' EXIT -curl -L -s ${INPUT} > ${TMPFILE} +if ! curl -L -s -f "${INPUT}" > "${TMPFILE}"; then + echo "ERROR: Failed to download ${INPUT} (HTTP error or connection failure)" >&2 + exit 1 +fi + +if ! ${JQ} empty "${TMPFILE}" 2>/dev/null; then + echo "ERROR: Downloaded data from ${INPUT} is not valid JSON" >&2 + exit 1 +fi -version=$(${JQ} -r '.version' ${TMPFILE}) +# Extract stored version from current output file (may be empty on first run) +STORED_VERSION=$(grep '^// DATA VERSION:' "${OUTPUT}" 2>/dev/null | awk '{print $4}' || true) -cat > ${OUTPUT} <&2 + exit 1 +fi + +if [ "${NEW_VERSION}" = "${STORED_VERSION}" ]; then + echo "Mozilla TLS data is already at version ${NEW_VERSION}, skipping regeneration." + exit 0 +fi +echo "Updating Mozilla TLS data from version ${STORED_VERSION:-unknown} to ${NEW_VERSION}" + +cat > "${OUTPUT}" <> ${OUTPUT} <&2 + echo "Available profiles: $(${JQ} -r '.configurations | keys | join(", ")' "${TMPFILE}")" >&2 + exit 1 + fi + + # Validate tls_versions is a non-empty array with a non-null first entry + if ! ${JQ} -e ".configurations.${profile}.tls_versions | type == \"array\" and length > 0 and .[0] != null" "${TMPFILE}" >/dev/null; then + echo "ERROR: Missing or empty .configurations.${profile}.tls_versions[0] in ${INPUT}" >&2 + exit 1 + fi + + # Validate that at least one cipher is present across ciphersuites and ciphers.iana + # (modern has only ciphersuites; intermediate has both; either alone is valid) + local cipher_count + cipher_count=$(${JQ} -r " + [ + (.configurations.${profile}.ciphersuites // []), + (.configurations.${profile}.ciphers.iana // []) + ] | add | length" "${TMPFILE}") + if [ "${cipher_count}" -eq 0 ] 2>/dev/null; then + echo "ERROR: Profile '${profile}' has no ciphers in ciphersuites or ciphers.iana" >&2 + exit 1 + fi + + # Validate tls_curves is non-empty + local curve_count + curve_count=$(${JQ} -r ".configurations.${profile}.tls_curves | length" "${TMPFILE}") + if [ "${curve_count}" -eq 0 ] 2>/dev/null; then + echo "ERROR: Profile '${profile}' has no entries in tls_curves" >&2 + exit 1 + fi + + cat >> "${OUTPUT}" <> ${OUTPUT} - ${JQ} -r ".configurations.$1.ciphers.iana[] | . |= \"tls.\" + . + \",\"" ${TMPFILE} >> ${OUTPUT} + ${JQ} -r "(.configurations.${profile}.ciphersuites // [])[] | . |= \"tls.\" + . + \",\"" "${TMPFILE}" >> "${OUTPUT}" + ${JQ} -r "(.configurations.${profile}.ciphers.iana // [])[] | . |= \"tls.\" + . + \",\"" "${TMPFILE}" >> "${OUTPUT}" - cat >> ${OUTPUT} <> "${OUTPUT}" <> ${OUTPUT} + ${JQ} -r ".configurations.${profile}.tls_curves[] | . |= . + \",\"" "${TMPFILE}" >> "${OUTPUT}" - version=$(${JQ} -r ".configurations.$1.tls_versions[0]" ${TMPFILE}) + version=$(${JQ} -r ".configurations.${profile}.tls_versions[0]" "${TMPFILE}") version=${version/TLSv1./tls.VersionTLS1} version=${version/TLSv1/tls.VersionTLS10} - cat >> ${OUTPUT} <> "${OUTPUT}" <