diff --git a/.github/scripts/gradle-distribution.sh b/.github/scripts/gradle-distribution.sh new file mode 100755 index 00000000..0a9cb740 --- /dev/null +++ b/.github/scripts/gradle-distribution.sh @@ -0,0 +1,106 @@ +#!/usr/bin/env bash +# The Gradle wrapper pin, gradle/wrapper/gradle-distribution.sha256, and the hash behind it. +# The wrapper checks distributionSha256Sum when it downloads the distribution, then deletes the +# zip; the pin lets gradle.sh check a distribution already unpacked in the Gradle user home, and +# the wrapper jar and start scripts, before every run. +# +# bash .github/scripts/gradle-distribution.sh pin [] +# After any change to the wrapper files: downloads distributionUrl (or takes ), refuses +# it unless its sha256 equals distributionSha256Sum, unpacks it, runs its own wrapper task in +# an empty project and refuses unless the committed gradle-wrapper.properties, +# gradle-wrapper.jar, gradlew and gradlew.bat are byte-identical to what it generates, then +# writes the pin. Review the diff and commit the pin with the wrapper files. +# bash .github/scripts/gradle-distribution.sh sum +# Prints the sha256 of (used by gradle.sh for the pinned wrapper files). +# bash .github/scripts/gradle-distribution.sh hash +# Prints the sha256 of the listing of : every directory, and every file with its own +# sha256, sorted by path. Symbolic links, other special files and names with control +# characters or backslashes are refused. +set -euo pipefail + +fail() { echo "::error::$1" >&2; exit 1; } +absolute() { case "$1" in /*) printf '%s' "$1" ;; *) printf '%s/%s' "$PWD" "$1" ;; esac; } +arg2="" +if [ $# -ge 2 ]; then arg2="$(absolute "$2")"; fi +if command -v sha256sum > /dev/null 2>&1; then sha256=(sha256sum); else sha256=(shasum -a 256); fi + +tree_hash() { + local dir="$1" + [ -d "$dir" ] && [ ! -L "$dir" ] || fail "$dir is not a directory" + ( + cd "$dir" + # find exits non-zero when it cannot read a folder; the listing is then refused. + find . > /dev/null || fail "$dir holds a folder that cannot be read" + [ -z "$(find . ! -type f ! -type d -print | head -1)" ] || + fail "$dir holds an entry that is neither a file nor a directory" + [ -z "$(find . \( -name '*[[:cntrl:]]*' -o -name '*\\*' \) -print | head -1)" ] || + fail "$dir holds a name with a control character or a backslash" + { + find . -mindepth 1 -type d -print | LC_ALL=C sort | sed 's/^/directory /' + find . -type f -print0 | LC_ALL=C sort -z | xargs -0 -r "${sha256[@]}" + } | "${sha256[@]}" | cut -d' ' -f1 + ) +} + +wrapper=gradle/wrapper/gradle-wrapper.properties +pin_file=gradle/wrapper/gradle-distribution.sha256 +# The properties file is compared, not pinned: gradle.sh checks its content itself. +wrapper_files=("$wrapper" gradle/wrapper/gradle-wrapper.jar gradlew gradlew.bat) + +case "${1:-}" in + hash) + [ $# -eq 2 ] || fail "usage: gradle-distribution.sh hash " + tree_hash "$arg2" + ;; + sum) + [ $# -eq 2 ] || fail "usage: gradle-distribution.sh sum " + [ -f "$arg2" ] && [ -r "$arg2" ] || fail "$arg2 cannot be read" + "${sha256[@]}" "$arg2" | cut -d' ' -f1 + ;; + pin) + [ $# -le 2 ] || fail "usage: gradle-distribution.sh pin []" + cd "$(git rev-parse --show-toplevel)" + url="$(sed -n 's/^distributionUrl=//p' "$wrapper" | sed 's#\\:#:#')" + sum="$(sed -n 's/^distributionSha256Sum=//p' "$wrapper")" + [[ "$url" =~ ^https://services\.gradle\.org/distributions/(gradle-([0-9]+(\.[0-9]+)+)-(all|bin))\.zip$ ]] || + fail "$wrapper: distributionUrl is not an official distribution" + name="${BASH_REMATCH[1]}" + version="${BASH_REMATCH[2]}" + type="${BASH_REMATCH[4]}" + top="gradle-$version" + [[ "$sum" =~ ^[0-9a-f]{64}$ ]] || fail "$wrapper: distributionSha256Sum is missing" + work="$(mktemp -d)" + trap 'rm -rf "$work"' EXIT + zip="$arg2" + if [ -z "$zip" ]; then + zip="$work/$name.zip" + curl -fsSL --proto '=https' --proto-redir '=https' --tlsv1.2 -o "$zip" "$url" + fi + [ "$("${sha256[@]}" "$zip" | cut -d' ' -f1)" = "$sum" ] || fail "$zip does not match distributionSha256Sum" + mkdir "$work/unpacked" + unzip -q "$zip" -d "$work/unpacked" + [ "$(ls -A "$work/unpacked")" = "$top" ] || fail "$zip does not hold exactly one $top directory" + tree="$(tree_hash "$work/unpacked/$top")" + # The committed wrapper files are the ones this release generates. + generated="$work/generated" + mkdir "$generated" + : > "$generated/settings.gradle" + (cd "$generated" && GRADLE_USER_HOME="$work/home" "$work/unpacked/$top/bin/gradle" --no-daemon --quiet \ + --offline wrapper --gradle-version "$version" --distribution-type "$type" \ + --gradle-distribution-sha256-sum "$sum") || fail "Gradle $version could not generate the wrapper files" + for file in "${wrapper_files[@]}"; do + cmp -s "$generated/$file" "$file" || + fail "$file is not the file Gradle $version generates: run ./gradlew wrapper --gradle-version $version --distribution-type $type --gradle-distribution-sha256-sum $sum (twice)" + done + { + printf '%s %s\n' "$tree" "$name" + for file in "${wrapper_files[@]:1}"; do + printf '%s %s\n' "$("${sha256[@]}" "$file" | cut -d' ' -f1)" "$file" + done + } > "$pin_file" + echo "$pin_file updated for $name; review 'git diff $pin_file'" + ;; + *) + fail "usage: gradle-distribution.sh pin [] | hash | sum " + ;; +esac diff --git a/.github/scripts/gradle-sh-test.sh b/.github/scripts/gradle-sh-test.sh new file mode 100644 index 00000000..4ef76b1b --- /dev/null +++ b/.github/scripts/gradle-sh-test.sh @@ -0,0 +1,360 @@ +#!/usr/bin/env bash +# Tests for gradle.sh: bash .github/scripts/gradle-sh-test.sh +# +# Each case runs this directory's gradle.sh in a throwaway git repository with a stand-in Java +# (JAVA_HOME) that records the Gradle user home the wrapper would use (resolved the way the +# wrapper resolves it) and answers the dependency verification probe, so no Gradle runs and +# nothing is downloaded. +# +# GRADLE_SH_TEST_REAL=1 also runs the real wrapper on a copy of this repository's working tree, +# in a fresh Gradle home, then in the same home again: needs network and a JDK, and takes a few +# minutes. +set -uo pipefail + +here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source_root="$(cd "$here/../.." && pwd)" +work="$(mktemp -d)" +trap 'chmod -R u+rwx "$work" 2> /dev/null; rm -rf "$work"' EXIT +passed=0 +failed=0 +ok() { passed=$((passed + 1)); echo "ok $1"; } +ko() { failed=$((failed + 1)); echo "FAIL $1"; [ -z "${2:-}" ] || sed 's/^/ | /' <<< "$2" | tail -5; } +# Permission cases need a user that file modes restrict; root is not. +if [ "$(id -u)" = 0 ]; then as_root=true; else as_root=false; fi +skip() { echo "skip $1 (running as root, file modes do not apply)"; } +sha256() { if command -v sha256sum > /dev/null 2>&1; then sha256sum "$@"; else shasum -a 256 "$@"; fi; } + +# ---- the stand-in Java +mkdir -p "$work/jdk/bin" +cat > "$work/jdk/bin/java" <<'JAVA' +#!/usr/bin/env bash +option="" property="" init="" previous="" +for arg in ${JAVA_TOOL_OPTIONS:-} ${JDK_JAVA_OPTIONS:-} ${_JAVA_OPTIONS:-} "$@"; do + case "$previous" in + --gradle-user-home | -g) option="$arg" ;; + --init-script) init="$arg" ;; + esac + case "$arg" in -Dgradle.user.home=*) property="${arg#-Dgradle.user.home=}" ;; esac + previous="$arg" +done +if [ -n "$option" ]; then home="$option" +elif [ -n "$property" ]; then home="$property" +elif [ -n "${GRADLE_USER_HOME+set}" ]; then home="$GRADLE_USER_HOME" +else home="ACCOUNT-HOME/.gradle" +fi +echo "$home" >> "$FAKE_JAVA_LOG" +daemon="daemon allowed" +for arg in "$@"; do [ "$arg" != "--no-daemon" ] || daemon="no daemon"; done +echo "$daemon" >> "$FAKE_JAVA_LOG.flags" +if [ -s "$init" ]; then echo "VERIFICATION-PROBE-REFUSED"; exit 1; fi +exit 0 +JAVA +chmod +x "$work/jdk/bin/java" + +# ---- a stand-in distribution, its zip and its pin +mkdir -p "$work/dist/gradle-8.14.8/lib/plugins" "$work/dist/gradle-8.14.8/bin" +echo "core" > "$work/dist/gradle-8.14.8/lib/gradle-core.jar" +echo "plugin" > "$work/dist/gradle-8.14.8/lib/plugins/gradle-plugin.jar" +echo "start" > "$work/dist/gradle-8.14.8/bin/gradle" +(cd "$work/dist" && zip -qr -X "$work/gradle-8.14.8-all.zip" gradle-8.14.8) +dist_sha="$(sha256 "$work/gradle-8.14.8-all.zip" | cut -d' ' -f1)" +echo "not the distribution" > "$work/other.zip" +tree_sha="$(bash "$here/gradle-distribution.sh" hash "$work/dist/gradle-8.14.8")" + +properties() { + printf '%s\n' \ + "distributionBase=GRADLE_USER_HOME" \ + "distributionPath=wrapper/dists" \ + "distributionSha256Sum=$dist_sha" \ + 'distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.8-all.zip' \ + "$@" \ + "zipStoreBase=GRADLE_USER_HOME" \ + "zipStorePath=wrapper/dists" +} + +# new_repo [extra properties lines...]: a repository holding this gradle.sh and a pin for the +# stand-in distribution, committed. +new_repo() { + repo="$work/case-$((passed + failed))" + rm -rf "$repo" + mkdir -p "$repo/.github/scripts" "$repo/gradle/wrapper" + cp "$here/gradle.sh" "$repo/.github/scripts/" + cp "$here/gradle-distribution.sh" "$repo/.github/scripts/" + cp "$source_root/gradle/verification-metadata.xml" "$repo/gradle/" + echo "jar" > "$repo/gradle/wrapper/gradle-wrapper.jar" + echo "start script" > "$repo/gradlew" + echo "start script for Windows" > "$repo/gradlew.bat" + properties "$@" > "$repo/gradle/wrapper/gradle-wrapper.properties" + echo "version=1.0.0" > "$repo/gradle.properties" + git -C "$repo" init -q + write_pin + git -C "$repo" add -A + git -C "$repo" -c user.name=t -c user.email=t@example.com commit -qm init +} + +# write_pin: the pin for the stand-in distribution and this repository's wrapper files. +write_pin() { + local file + { + printf '%s gradle-8.14.8-all\n' "$tree_sha" + for file in gradle/wrapper/gradle-wrapper.jar gradlew gradlew.bat; do + printf '%s %s\n' "$(sha256 "$repo/$file" | cut -d' ' -f1)" "$file" + done + } > "$repo/gradle/wrapper/gradle-distribution.sha256" +} + +commit_repo() { git -C "$repo" add -A && git -C "$repo" -c user.name=t -c user.email=t@example.com commit -q --allow-empty -m change; } + +# run_gradle_sh: runs gradle.sh help in $repo with a clean environment plus the given +# VAR=value pairs; sets $out, $status and $homes (the homes the stand-in saw, one per run). +run_gradle_sh() { + local log="$work/java.log" + : > "$log" + : > "$log.flags" + out="$(cd "$repo" && env -i PATH="$PATH" JAVA_HOME="$work/jdk" FAKE_JAVA_LOG="$log" "$@" \ + bash .github/scripts/gradle.sh help 2>&1)" + status=$? + homes="$(sort -u "$log")" + flags="$(sort -u "$log.flags")" +} + +# unpacked [zip]: the layout the wrapper leaves after unpacking (markers, no zip unless +# given). +unpacked() { + local dir="$1/wrapper/dists/gradle-8.14.8-all/3cc0wa1vaaf2m4b1ck6bwnhm0" + mkdir -p "$dir" + (cd "$dir" && unzip -q "$work/gradle-8.14.8-all.zip") + : > "$dir/gradle-8.14.8-all.zip.lck" + : > "$dir/gradle-8.14.8-all.zip.ok" + [ -z "${2:-}" ] || cp "$2" "$dir/gradle-8.14.8-all.zip" + dist_dir="$dir" +} + +expect_home() { # name, expected home + if [ "$status" -eq 0 ] && [ "$homes" = "$2" ]; then ok "$1"; else ko "$1 (exit $status, Gradle user home '$homes', expected '$2')" "$out"; fi +} +expect_refused() { # name, message part + if [ "$status" -ne 0 ] && [ -z "$homes" ] && grep -qF -- "$2" <<< "$out"; then ok "$1"; else ko "$1 (exit $status, Gradle ran: '${homes:-no}', expected a refusal naming '$2')" "$out"; fi +} + +# ---- the Gradle user home gradle.sh checks is the one Gradle uses +new_repo +run_gradle_sh HOME="$work/home-a" +expect_home "HOME elsewhere, GRADLE_USER_HOME unset: Gradle uses HOME/.gradle" "$work/home-a/.gradle" + +run_gradle_sh HOME="$work/home-a" GRADLE_USER_HOME="$work/home-b" +expect_home "GRADLE_USER_HOME set: Gradle uses it" "$work/home-b" + +run_gradle_sh HOME="$work/home-a" GRADLE_USER_HOME="" +expect_home "GRADLE_USER_HOME empty: Gradle uses HOME/.gradle" "$work/home-a/.gradle" + +run_gradle_sh HOME="$work/home-a" GRADLE_USER_HOME="relative-home" +expect_home "GRADLE_USER_HOME relative: Gradle uses it from the repository root" "$(git -C "$repo" rev-parse --show-toplevel)/relative-home" + +run_gradle_sh HOME="$work/home-a" JAVA_TOOL_OPTIONS="-Dgradle.user.home=$work/home-c" +expect_home "JVM options in JAVA_TOOL_OPTIONS: Gradle uses the checked home" "$work/home-a/.gradle" + +run_gradle_sh HOME="$work/home-a" JDK_JAVA_OPTIONS="-Dgradle.user.home=$work/home-c" +expect_home "JVM options in JDK_JAVA_OPTIONS: Gradle uses the checked home" "$work/home-a/.gradle" + +run_gradle_sh +expect_refused "HOME and GRADLE_USER_HOME unset: refused" "GRADLE_USER_HOME" + +mkdir -p "$work/home-m/link-target" "$work/home-m/.gradle" && ln -s "$work/home-m/link-target" "$work/home-m/link" +run_gradle_sh HOME="$work/home-m/link/.." +expect_refused "Gradle user home path from HOME that is not the folder Gradle opens: refused" "must not hold . or .. segments" +run_gradle_sh GRADLE_USER_HOME="$work/home-m/./.gradle" +expect_refused "Gradle user home path from GRADLE_USER_HOME that is not the folder Gradle opens: refused" "must not hold . or .. segments" + +mkdir -p "$work/home-n/.gradle" && printf 'org.gra\\\ndle.jvmargs=-Xmx1g\n' > "$work/home-n/.gradle/gradle.properties" +run_gradle_sh HOME="$work/home-n" +expect_refused "Gradle user home gradle.properties with a backslash: refused" "holds Gradle settings" +printf 'org.gradle.caching=true\n' > "$work/home-n/.gradle/gradle.properties" +run_gradle_sh HOME="$work/home-n" +expect_refused "Gradle user home gradle.properties with a Gradle setting: refused" "holds Gradle settings" + +mkdir -p "$work/home-p/.gradle/native/jansi/1.18" "$work/home-p/.gradle/native/0.2.7" && echo "library" > "$work/home-p/.gradle/native/0.2.7/libnative.so" +run_gradle_sh HOME="$work/home-p" +if [ "$status" -eq 0 ] && [ "$flags" = "no daemon" ] && [ "$(wc -l < "$work/java.log.flags" | tr -d ' ')" = 2 ]; then ok "both runs start a new Gradle process (no daemon reused)"; else ko "both runs start a new Gradle process (no daemon reused) (exit $status, '$flags')" "$out"; fi +if [ "$status" -eq 0 ] && [ ! -e "$work/home-p/.gradle/native" ]; then ok "native libraries from an earlier run: removed before Gradle runs"; else ko "native libraries from an earlier run: removed before Gradle runs" "$out"; fi + +mkdir -p "$work/home-q/.gradle/native/mine" +run_gradle_sh HOME="$work/home-q" +expect_refused "native folder holding something Gradle does not create: refused, not removed" "which Gradle does not create" +[ -d "$work/home-q/.gradle/native/mine" ] && ok "native folder holding something else: left in place" || ko "native folder holding something else: left in place" + +run_gradle_sh HOME="$work/home-a" JAVA_TOOL_OPTIONS="-Dorg.gradle.native.dir=$work/elsewhere" +expect_refused "native library folder set in JAVA_TOOL_OPTIONS: refused" "native libraries" +case_number=0 +for value in "-Dorg.gradle.nati've'.dir=$work/elsewhere" '-java"agent":agent.jar' "-java\\agent:agent.jar"; do + for variable in JAVA_TOOL_OPTIONS JDK_JAVA_OPTIONS _JAVA_OPTIONS; do + run_gradle_sh HOME="$work/home-a" "$variable=$value" + case_number=$((case_number + 1)) + expect_refused "JVM options in $variable compared as the JVM reads them (case $case_number): refused" "$variable sets dependency verification, an agent" + done +done + +if $as_root; then skip "native folder that cannot be removed"; else + mkdir -p "$work/home-r/.gradle/native/0.2.7/x" && echo "library" > "$work/home-r/.gradle/native/0.2.7/x/lib.so" + chmod 555 "$work/home-r/.gradle/native/0.2.7/x" + run_gradle_sh HOME="$work/home-r" + expect_refused "native folder that cannot be removed: refused" "could not be removed" + chmod 755 "$work/home-r/.gradle/native/0.2.7/x" +fi + +run_gradle_sh HOME="$work/hom$(printf '\303\251')" +expect_refused "Gradle user home with a non-ASCII name: refused" "printable ASCII" + +if $as_root; then skip "pinned file that cannot be read"; else + new_repo + chmod 000 "$repo/gradlew" + run_gradle_sh HOME="$work/home-f" + expect_refused "pinned file that cannot be read: refused" "gradlew cannot be read" + chmod 644 "$repo/gradlew" +fi + +mkdir -p "$work/home-d/.gradle/init.d" && echo "// init" > "$work/home-d/.gradle/init.d/a.gradle" +run_gradle_sh HOME="$work/home-d" +expect_refused "init script in the HOME Gradle uses: refused" "init.d holds init scripts" + +# ---- wrapper settings +new_repo "networkTimeout=10000" "validateDistributionUrl=true" +run_gradle_sh HOME="$work/home-e" +expect_home "settings written by the current wrapper task: accepted" "$work/home-e/.gradle" + +for bad in "validateDistributionUrl=false" "networkTimeout=abc" "networkTimeout=1234567" "networkTimeout=" "unknownKey=1"; do + new_repo "$bad" + run_gradle_sh HOME="$work/home-e" + expect_refused "settings with $bad: refused" "gradle-wrapper.properties must hold exactly the official distribution settings" +done + +# ---- the distribution pin +new_repo +rm "$repo/gradle/wrapper/gradle-distribution.sha256" 2> /dev/null; commit_repo +run_gradle_sh HOME="$work/home-f" +expect_refused "pin missing: refused" "gradle-distribution.sha256" + +new_repo +printf '%s gradle-8.14.3-all\n' "$(printf 'a%.0s' {1..64})" > "$repo/gradle/wrapper/gradle-distribution.sha256"; commit_repo +run_gradle_sh HOME="$work/home-f" +expect_refused "pin for another distribution: refused" "gradle-distribution.sha256" + +new_repo +echo "not a pin" > "$repo/gradle/wrapper/gradle-distribution.sha256"; commit_repo +run_gradle_sh HOME="$work/home-f" +expect_refused "malformed pin: refused" "gradle-distribution.sha256" + +for file in gradle/wrapper/gradle-wrapper.jar gradlew gradlew.bat; do + new_repo + echo "changed" >> "$repo/$file"; commit_repo + run_gradle_sh HOME="$work/home-f" + expect_refused "$file that differs from the pin: refused" "$file does not match" +done + +new_repo +head -1 "$repo/gradle/wrapper/gradle-distribution.sha256" > "$work/one-line.pin" +cp "$work/one-line.pin" "$repo/gradle/wrapper/gradle-distribution.sha256"; commit_repo +run_gradle_sh HOME="$work/home-f" +expect_refused "pin without the wrapper files: refused" "gradle-distribution.sha256" + +# ---- a distribution already unpacked in the Gradle user home +new_repo +run_gradle_sh HOME="$work/home-g" +expect_home "fresh Gradle home: accepted" "$work/home-g/.gradle" + +unpacked "$work/home-h/.gradle" +run_gradle_sh HOME="$work/home-h" +expect_home "reused home, zip removed by the wrapper, distribution unchanged: accepted" "$work/home-h/.gradle" +run_gradle_sh HOME="$work/home-h" +expect_home "reused home, second run: accepted" "$work/home-h/.gradle" + +unpacked "$work/home-i/.gradle" "$work/gradle-8.14.8-all.zip" +run_gradle_sh HOME="$work/home-i" +expect_home "reused home with its zip, distribution unchanged: accepted" "$work/home-i/.gradle" + +mkdir -p "$work/home-j/.gradle/wrapper/dists/gradle-8.14.8-all/abc" +: > "$work/home-j/.gradle/wrapper/dists/gradle-8.14.8-all/abc/gradle-8.14.8-all.zip.lck" +run_gradle_sh HOME="$work/home-j" +expect_home "interrupted download (lock file only): accepted, the wrapper downloads again" "$work/home-j/.gradle" + +unpacked "$work/home-k/.gradle" "$work/other.zip" +run_gradle_sh HOME="$work/home-k" +expect_home "zip that differs next to an unchanged distribution: accepted, the wrapper does not use it" "$work/home-k/.gradle" + +unpacked "$work/home-l/.gradle" "$work/gradle-8.14.8-all.zip" +rm "$dist_dir/gradle-8.14.8-all.zip.ok" "$dist_dir/gradle-8.14.8/lib/gradle-core.jar" +echo "partial" > "$dist_dir/gradle-8.14.8/lib/plugins/gradle-plugin.jar" +run_gradle_sh HOME="$work/home-l" +expect_home "interrupted unpack (no marker): accepted, the wrapper unpacks the zip again" "$work/home-l/.gradle" + +tamper() { # name, command run in the unpacked distribution, expected message part [, zip] + local home="$work/home-t$((passed + failed))/.gradle" + unpacked "$home" "${4:-}" + (cd "$dist_dir" && eval "$2") + run_gradle_sh HOME="${home%/.gradle}" + expect_refused "$1" "$3" +} +tamper "changed file: refused" "echo changed > gradle-8.14.8/lib/gradle-core.jar" "does not match" +tamper "added file: refused" "echo extra > gradle-8.14.8/lib/plugins/extra.jar" "does not match" +tamper "removed file: refused" "rm gradle-8.14.8/lib/plugins/gradle-plugin.jar" "does not match" +tamper "added directory: refused" "mkdir gradle-8.14.8/lib/extra" "does not match" +tamper "symbolic link in the distribution: refused" "ln -s /etc gradle-8.14.8/lib/link" "neither a file nor a directory" +tamper "second top-level directory: refused" "mkdir gradle-other" "unexpected" +tamper "unexpected top-level file: refused" "echo x > init.gradle" "unexpected" +tamper "distribution directory is a symbolic link: refused" "mv gradle-8.14.8 ../moved && ln -s ../moved gradle-8.14.8" "unexpected" +tamper "marker is a directory: refused" "rm gradle-8.14.8-all.zip.ok && mkdir gradle-8.14.8-all.zip.ok" "unexpected" +tamper "changed distribution with its zip: refused" "echo changed > gradle-8.14.8/bin/gradle" "does not match" "$work/gradle-8.14.8-all.zip" +tamper "zip as a directory: refused" "mkdir gradle-8.14.8-all.zip" "unexpected" +tamper "name with a newline in the distribution: refused" "touch gradle-8.14.8/lib/\$'a\nb'" "control character" +if $as_root; then + skip "folder in the distribution that cannot be read"; skip "distribution folder that cannot be listed" +else + tamper "folder in the distribution that cannot be read: refused" "chmod 000 gradle-8.14.8/lib/plugins" "cannot be read" + + unreadable_home="$work/home-o/.gradle" + unpacked "$unreadable_home" + chmod 111 "$unreadable_home/wrapper/dists/gradle-8.14.8-all" + run_gradle_sh HOME="$work/home-o" + expect_refused "distribution folder that cannot be listed: refused" "cannot be read" + chmod 755 "$unreadable_home/wrapper/dists/gradle-8.14.8-all" +fi + +# ---- the real wrapper (optional) +if [ "${GRADLE_SH_TEST_REAL:-}" = "1" ]; then + real="$work/real" + mkdir -p "$real" + (cd "$source_root" && git ls-files -z --cached --others --exclude-standard | tar --null -T - -cf -) | tar -xf - -C "$real" + git -C "$real" init -q && git -C "$real" add -A && git -C "$real" -c user.name=t -c user.email=t@example.com commit -qm copy + real_run() { (cd "$real" && env -i PATH="$PATH" JAVA_HOME="${JAVA_HOME:-}" "$@" bash .github/scripts/gradle.sh --no-daemon help 2>&1); } + real_dist() { find "$1/wrapper/dists" -mindepth 2 -maxdepth 2 -type d 2> /dev/null | head -1; } + for round in 1 2; do + if out="$(real_run HOME="$work/real-home")"; then ok "real wrapper, HOME elsewhere, run $round"; else ko "real wrapper, HOME elsewhere, run $round" "$out"; fi + done + d="$(real_dist "$work/real-home/.gradle")" + if [ -n "$d" ] && [ -z "$(ls "$d"/*.zip 2> /dev/null)" ]; then ok "real wrapper unpacked into HOME/.gradle and removed the zip"; else ko "real wrapper unpacked into HOME/.gradle and removed the zip (dist: '$d')"; fi + top="$(basename "$(ls -d "$d"/gradle-*/ 2> /dev/null | head -1)")" + pin="$(head -1 "$real/gradle/wrapper/gradle-distribution.sha256" | cut -d' ' -f1)" + if [ -n "$top" ] && [ "$(bash "$here/gradle-distribution.sh" hash "$d/$top")" = "$pin" ]; then ok "the distribution the wrapper unpacked matches the pin"; else ko "the distribution the wrapper unpacked matches the pin"; fi + if out="$(real_run HOME="$work/real-home" JAVA_TOOL_OPTIONS="-Dgradle.user.home=$work/real-other")" && [ ! -e "$work/real-other" ]; then + ok "real wrapper uses the checked home with JVM options set" + else + ko "real wrapper uses the checked home with JVM options set" "$out" + fi + jars=("$d/$top"/lib/*.jar) + cp "$real/gradle/wrapper/gradle-distribution.sha256" "$work/committed.pin" + if out="$(cd "$real" && bash .github/scripts/gradle-distribution.sh pin 2>&1)" && cmp -s "$work/committed.pin" "$real/gradle/wrapper/gradle-distribution.sha256"; then + ok "pin: the committed wrapper files are the ones the release generates, pin unchanged" + else + ko "pin: the committed wrapper files are the ones the release generates, pin unchanged" "$out" + fi + printf '#!/usr/bin/env sh\n' > "$real/gradlew" + if out="$(cd "$real" && bash .github/scripts/gradle-distribution.sh pin 2>&1)"; then ko "pin: a start script the release does not generate: refused" "$out" + elif grep -q "gradlew is not the file" <<< "$out"; then ok "pin: a start script the release does not generate: refused" + else ko "pin: a start script the release does not generate: refused" "$out"; fi + (cd "$real" && git checkout -q -- gradlew gradle/wrapper/gradle-distribution.sha256) + echo changed >> "${jars[0]}" + if out="$(real_run HOME="$work/real-home")"; then ko "real wrapper, changed distribution: refused" "$out"; elif grep -q "does not match" <<< "$out"; then ok "real wrapper, changed distribution: refused"; else ko "real wrapper, changed distribution: refused" "$out"; fi +fi + +echo "passed $passed, failed $failed" +[ "$failed" -eq 0 ] diff --git a/.github/scripts/gradle.sh b/.github/scripts/gradle.sh index f08622fc..eaf8905d 100755 --- a/.github/scripts/gradle.sh +++ b/.github/scripts/gradle.sh @@ -5,13 +5,18 @@ # (--no-scan on every run). # 2. Early warning (static): gradle/verification-metadata.xml verifies metadata and trusts no # artifact or key without a checksum, the wrapper settings are exactly the official download -# into the Gradle user home with its sha256 and at least the minimum Gradle version, -# gradle.properties holds project values only, and no Gradle user home init script, -# properties file, environment variable or build script changes dependency verification. +# into the Gradle user home with its sha256 and at least the minimum Gradle version, the +# wrapper jar, the start scripts and a distribution the wrapper would run from the Gradle user +# home match gradle/wrapper/gradle-distribution.sha256, gradle.properties holds project values +# only, and no Gradle user home init script, properties file, environment variable or build +# script changes dependency verification. +# The Gradle user home checked is the one Gradle uses (GRADLE_USER_HOME, else $HOME/.gradle). # 3. Probe run: Gradle starts from the wrapper jar. Before settings are evaluated, an init script # resolves a probe artifact from a temporary local repository (a coordinate no project uses, # so it is never in gradle/verification-metadata.xml); dependency verification must refuse it. # The files that run created are removed, then the build runs with the same command line. +# Both runs start a new Gradle process (--no-daemon, no running daemon is reused) and Gradle +# unpacks its native libraries again (the Gradle user home's native folder is removed first). # 4. The working tree is clean before, between and after the runs. set -euo pipefail @@ -25,6 +30,7 @@ for arg in "$@"; do [[ "$arg" =~ ^[a-z][A-Za-z0-9]*$ ]] || fail "gradle.sh: -x needs a task name" else case "$arg" in + # --no-daemon and --no-scan are kept for existing callers; gradle.sh passes both itself. --no-daemon | --no-build-cache | --fail-fast | --no-scan | -q | --quiet | --stacktrace | -x) ;; *) [[ "$arg" =~ ^[a-z][A-Za-z0-9]*$ ]] || fail "gradle.sh does not pass $arg to Gradle" ;; esac @@ -41,17 +47,18 @@ if grep -qiE 'trusted-artifacts|<([a-z0-9_-]+:)?trust[[:space:]>/]|ignored-key|t fail "$metadata must not trust artifacts or keys without a checksum" fi # The wrapper settings are exactly the official distribution download into the Gradle user home, -# with its sha256. +# with its sha256; the current wrapper task also writes a network timeout and URL validation. wrapper=gradle/wrapper/gradle-wrapper.properties settings="$(grep -vE '^[[:space:]]*(#|$)' "$wrapper" | LC_ALL=C sort)" expected_re='^distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists distributionSha256Sum=[0-9a-f]{64} distributionUrl=https\\://services\.gradle\.org/distributions/gradle-[0-9]+(\.[0-9]+)+-(all|bin)\.zip -zipStoreBase=GRADLE_USER_HOME +(networkTimeout=[1-9][0-9]{0,5} +)?(validateDistributionUrl=true +)?zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists$' [[ "$settings" =~ $expected_re ]] || fail "$wrapper must hold exactly the official distribution settings" -dist_sha="$(sed -n 's/^distributionSha256Sum=//p' "$wrapper")" dist_name="$(sed -n 's#^distributionUrl=.*/\(gradle-[0-9.]*-[a-z]*\)\.zip$#\1#p' "$wrapper")" # The wrapper's Gradle version is at least the minimum (set below): 9.x has its own, 10 and later # pass, 8.x and earlier need the 8.x one. Versions are compared number by number. @@ -79,15 +86,72 @@ case "${dist_version%%.*}" in esac version_at_least "$dist_version" "$minimum" || fail "$wrapper: Gradle $dist_version is below the minimum version $minimum" -gradle_home="${GRADLE_USER_HOME:-$HOME/.gradle}" -# A distribution already unpacked in the Gradle user home sits next to its zip, which has the -# pinned sha256. -for dist in "$gradle_home/wrapper/dists/$dist_name"/*/; do - [ -d "$dist" ] || continue - zip="$dist$dist_name.zip" - [ -f "$zip" ] || fail "$dist holds a distribution without its zip" - actual="$( (sha256sum "$zip" 2>/dev/null || shasum -a 256 "$zip") | cut -d' ' -f1)" - [ "$actual" = "$dist_sha" ] || fail "$zip does not match distributionSha256Sum" +# One Gradle user home: the folder checked here is the folder Gradle uses. It is exported, for +# every process the build starts, and passed to the wrapper with --gradle-user-home (below). +if [ -n "${GRADLE_USER_HOME:-}" ]; then + gradle_home="$GRADLE_USER_HOME" +elif [ -n "${HOME:-}" ]; then + gradle_home="$HOME/.gradle" +else + fail "gradle.sh needs GRADLE_USER_HOME or HOME to find the Gradle user home" +fi +case "$gradle_home" in /*) ;; *) gradle_home="$PWD/$gradle_home" ;; esac +# Without "." or ".." segments, the path the checks below read and the path Gradle opens are the +# same folder. +case "/$gradle_home/" in */./* | */../*) fail "the Gradle user home must not hold . or .. segments: $gradle_home" ;; esac +# Printable ASCII only, so the JVM opens the same path whatever its file name encoding. +[[ "$gradle_home" =~ ^[\ -~]+$ ]] || fail "the Gradle user home path must be printable ASCII" +export GRADLE_USER_HOME="$gradle_home" +# The pin (written by gradle-distribution.sh pin, which checks that this release generates the +# committed wrapper files) names this distribution, and the wrapper jar and start scripts match it. +pin_file=gradle/wrapper/gradle-distribution.sha256 +pin_help="run bash .github/scripts/gradle-distribution.sh pin and review the result" +[ -f "$pin_file" ] && [ ! -L "$pin_file" ] || fail "$pin_file is missing: $pin_help" +pin_re="^([0-9a-f]{64}) ${dist_name//./\\.} +([0-9a-f]{64}) gradle/wrapper/gradle-wrapper\\.jar +([0-9a-f]{64}) gradlew +([0-9a-f]{64}) gradlew\\.bat$" +[[ "$(cat "$pin_file")" =~ $pin_re ]] || fail "$pin_file does not pin $dist_name: $pin_help" +pin_sha="${BASH_REMATCH[1]}" +pinned_files=(gradle/wrapper/gradle-wrapper.jar gradlew gradlew.bat) +pinned_shas=("${BASH_REMATCH[2]}" "${BASH_REMATCH[3]}" "${BASH_REMATCH[4]}") +for i in 0 1 2; do + file="${pinned_files[i]}" + [ -f "$file" ] && [ ! -L "$file" ] || fail "$file is missing" + actual="$(bash .github/scripts/gradle-distribution.sh sum "$file")" || fail "$file cannot be read" + [ "$actual" = "${pinned_shas[i]}" ] || fail "$file does not match $pin_file: $pin_help" +done +# A distribution already in the Gradle user home holds only the wrapper's lock and marker files, +# the zip and the unpacked distribution. The wrapper runs an unpacked distribution only when its +# marker file is there, so that one must match the pin; without the marker, the wrapper deletes +# the folder and unpacks the zip again after checking distributionSha256Sum. A fresh Gradle home +# has nothing here: the wrapper downloads the zip and checks distributionSha256Sum itself. +unexpected() { fail "unexpected entry in the Gradle user home: $1"; } +dists="$gradle_home/wrapper/dists/$dist_name" +# A folder that cannot be listed is refused: its content could not be checked. +readable() { [ -r "$1" ] && [ -x "$1" ] || fail "the Gradle user home folder $1 cannot be read"; } +[ ! -e "$dists" ] || readable "$dists" +for dist in "$dists"/* "$dists"/.[!.]* "$dists"/..?*; do + [ -e "$dist" ] || [ -L "$dist" ] || continue + [ -d "$dist" ] && [ ! -L "$dist" ] || unexpected "$dist" + readable "$dist" + for entry in "$dist"/* "$dist"/.[!.]* "$dist"/..?*; do + [ -e "$entry" ] || [ -L "$entry" ] || continue + case "${entry##*/}" in + "$dist_name.zip" | "$dist_name.zip.lck" | "$dist_name.zip.ok" | "$dist_name.zip.part") + [ -f "$entry" ] && [ ! -L "$entry" ] || unexpected "$entry" + ;; + "gradle-$dist_version") + [ -d "$entry" ] && [ ! -L "$entry" ] || unexpected "$entry" + ;; + *) unexpected "$entry" ;; + esac + done + unpacked="$dist/gradle-$dist_version" + if [ -f "$dist/$dist_name.zip.ok" ] && [ -d "$unpacked" ]; then + actual="$(bash .github/scripts/gradle-distribution.sh hash "$unpacked")" || exit 1 + [ "$actual" = "$pin_sha" ] || fail "$unpacked does not match $pin_file" + fi done if [ -d "$gradle_home/init.d" ] && [ -n "$(ls -A "$gradle_home/init.d")" ]; then fail "$gradle_home/init.d holds init scripts" @@ -103,14 +167,19 @@ while IFS= read -r -d '' file; do fail "$file may only hold plain project values (key=value, lowercase key without dots)" fi done < <(git ls-files -z -- '*gradle.properties') -if [ -f "$gradle_home/gradle.properties" ] && grep -qiE "$pattern|org\.gradle\.|systemProp" "$gradle_home/gradle.properties"; then +# Backslashes are refused there too, so every key is read as written. +if [ -f "$gradle_home/gradle.properties" ] && grep -qiE "$pattern|org\.gradle\.|systemProp|\\\\" "$gradle_home/gradle.properties"; then fail "$gradle_home/gradle.properties holds Gradle settings" fi # The environment variables the JVM or Gradle read (GRADLE_OPTS and JAVA_OPTS are not used, see -# below): no dependency verification setting, agent, boot class path or other Java home. +# below): no dependency verification setting, agent, boot class path, other Java home or native +# library setting. +# Options are compared as the JVM reads them (quotes and backslashes removed). for variable in JAVA_TOOL_OPTIONS JDK_JAVA_OPTIONS _JAVA_OPTIONS JAVA_OPTIONS; do - if grep -qiE 'verification|-javaagent|-agentpath|-agentlib|-Xbootclasspath|java\.home' <<< "${!variable:-}"; then - fail "$variable sets dependency verification, an agent, the boot class path or the Java home" + value="${!variable:-}" + value="${value//[\"\'\\]/}" + if grep -qiE 'verification|-javaagent|-agentpath|-agentlib|-Xbootclasspath|java\.home|org\.gradle\.native' <<< "$value"; then + fail "$variable sets dependency verification, an agent, the boot class path, the Java home or Gradle's native libraries" fi done if env | grep -iE '^ORG_GRADLE_' | grep -qiE 'verification'; then @@ -142,7 +211,7 @@ done gradle() { "$java_cmd" -Xmx64m -Xms64m -Dorg.gradle.appname=gradlew \ -classpath gradle/wrapper/gradle-wrapper.jar org.gradle.wrapper.GradleWrapperMain \ - --init-script "$init" --no-scan "$@" + --gradle-user-home "$gradle_home" --init-script "$init" --no-scan --no-daemon "$@" } # The probe artifact: a coordinate no project uses, served from a temporary local repository. @@ -175,6 +244,21 @@ GRADLE # NUL-separated records, so paths with spaces or special characters are compared exactly. # Ignored and untracked directories are listed once, not file by file. snapshot() { git status --porcelain=v1 -z --ignored=traditional --untracked-files=normal | od -An -v -tx1 | tr -d ' \n'; } +# Native libraries an earlier run unpacked into the Gradle user home are not reused: Gradle +# unpacks them again from the checked distribution. Only a folder that holds nothing but what +# Gradle puts there (jansi, hash and version folders) is removed; anything else is refused. +native="$gradle_home/native" +if [ -e "$native" ] || [ -L "$native" ]; then + [ -d "$native" ] && [ ! -L "$native" ] || fail "$native is not a folder Gradle created" + readable "$native" + for entry in "$native"/* "$native"/.[!.]* "$native"/..?*; do + [ -e "$entry" ] || [ -L "$entry" ] || continue + [[ "${entry##*/}" =~ ^(jansi|[0-9a-f]{64}|[0-9]+(\.[0-9]+)*)$ ]] && [ -d "$entry" ] && [ ! -L "$entry" ] || + fail "$native holds $entry, which Gradle does not create: remove it or use another GRADLE_USER_HOME" + done + rm -rf -- "$native" 2> /dev/null || true + [ ! -e "$native" ] || fail "$native could not be removed" +fi state_before="$(snapshot)" git status --porcelain=v1 -z --ignored=traditional --untracked-files=normal > "$work/before.z" out="$(gradle "$@" 2>&1)" && fail "the dependency verification probe did not run" diff --git a/gradle/wrapper/gradle-distribution.sha256 b/gradle/wrapper/gradle-distribution.sha256 new file mode 100644 index 00000000..d1481312 --- /dev/null +++ b/gradle/wrapper/gradle-distribution.sha256 @@ -0,0 +1,4 @@ +fc0b334af874dfdbcd0704d377fe11a8c7a71d004dff932d4dd91844d96f9ad2 gradle-8.14.8-all +ad16bca46cb71bb5887c546860d93992aeee35c1fdaf4a242b0a612375d937e0 gradle/wrapper/gradle-wrapper.jar +b187b4c52e749f5760afdd6fadc31b2a98ad35fb249bf0dff03b72650f320409 gradlew +1d297e00bd21de3ace22b4d7f2de1f9dfa858883d66bbf7c1ccbecccec8f4f3b gradlew.bat diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 5c2d1cf0..02f06ab2 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 33e0c5df..727b67a9 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,7 +1,8 @@ -#Fri Aug 02 15:19:01 BST 2019 -distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.8-all.zip -distributionSha256Sum=34b008b7b744e4d12468e97ba03c572d0cd786fd0334e61e0171ea3d1a566b73 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -zipStorePath=wrapper/dists +distributionSha256Sum=34b008b7b744e4d12468e97ba03c572d0cd786fd0334e61e0171ea3d1a566b73 +distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.8-all.zip +networkTimeout=10000 +validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew index 8e25e6c1..23d15a93 100755 --- a/gradlew +++ b/gradlew @@ -1,7 +1,7 @@ -#!/usr/bin/env sh +#!/bin/sh # -# Copyright 2015 the original author or authors. +# Copyright © 2015-2021 the original authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -15,80 +15,115 @@ # See the License for the specific language governing permissions and # limitations under the License. # +# SPDX-License-Identifier: Apache-2.0 +# ############################################################################## -## -## Gradle start up script for UN*X -## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# ############################################################################## # Attempt to set APP_HOME + # Resolve links: $0 may be a link -PRG="$0" -# Need this for relative symlinks. -while [ -h "$PRG" ] ; do - ls=`ls -ld "$PRG"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - PRG="$link" - else - PRG=`dirname "$PRG"`"/$link" - fi +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac done -SAVED="`pwd`" -cd "`dirname \"$PRG\"`/" >/dev/null -APP_HOME="`pwd -P`" -cd "$SAVED" >/dev/null -APP_NAME="Gradle" -APP_BASE_NAME=`basename "$0"` - -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit # Use the maximum available, or set MAX_FD != -1 to use that value. -MAX_FD="maximum" +MAX_FD=maximum warn () { echo "$*" -} +} >&2 die () { echo echo "$*" echo exit 1 -} +} >&2 # OS specific support (must be 'true' or 'false'). cygwin=false msys=false darwin=false nonstop=false -case "`uname`" in - CYGWIN* ) - cygwin=true - ;; - Darwin* ) - darwin=true - ;; - MINGW* ) - msys=true - ;; - NONSTOP* ) - nonstop=true - ;; +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; esac -CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar +CLASSPATH="\\\"\\\"" + # Determine the Java command to use to start the JVM. if [ -n "$JAVA_HOME" ] ; then if [ -x "$JAVA_HOME/jre/sh/java" ] ; then # IBM's JDK on AIX uses strange locations for the executables - JAVACMD="$JAVA_HOME/jre/sh/java" + JAVACMD=$JAVA_HOME/jre/sh/java else - JAVACMD="$JAVA_HOME/bin/java" + JAVACMD=$JAVA_HOME/bin/java fi if [ ! -x "$JAVACMD" ] ; then die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME @@ -97,92 +132,120 @@ Please set the JAVA_HOME variable in your environment to match the location of your Java installation." fi else - JAVACMD="java" - which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. Please set the JAVA_HOME variable in your environment to match the location of your Java installation." + fi fi # Increase the maximum file descriptors if we can. -if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then - MAX_FD_LIMIT=`ulimit -H -n` - if [ $? -eq 0 ] ; then - if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then - MAX_FD="$MAX_FD_LIMIT" - fi - ulimit -n $MAX_FD - if [ $? -ne 0 ] ; then - warn "Could not set maximum file descriptor limit: $MAX_FD" - fi - else - warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" - fi +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac fi -# For Darwin, add options to specify how the application appears in the dock -if $darwin; then - GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" -fi +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) -# For Cygwin, switch paths to Windows format before running java -if $cygwin ; then - APP_HOME=`cygpath --path --mixed "$APP_HOME"` - CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` - JAVACMD=`cygpath --unix "$JAVACMD"` - - # We build the pattern for arguments to be converted via cygpath - ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` - SEP="" - for dir in $ROOTDIRSRAW ; do - ROOTDIRS="$ROOTDIRS$SEP$dir" - SEP="|" - done - OURCYGPATTERN="(^($ROOTDIRS))" - # Add a user-defined pattern to the cygpath arguments - if [ "$GRADLE_CYGPATTERN" != "" ] ; then - OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" - fi # Now convert the arguments - kludge to limit ourselves to /bin/sh - i=0 - for arg in "$@" ; do - CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` - CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option - - if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition - eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` - else - eval `echo args$i`="\"$arg\"" + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) fi - i=$((i+1)) + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg done - case $i in - (0) set -- ;; - (1) set -- "$args0" ;; - (2) set -- "$args0" "$args1" ;; - (3) set -- "$args0" "$args1" "$args2" ;; - (4) set -- "$args0" "$args1" "$args2" "$args3" ;; - (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; - (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; - (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; - (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; - (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; - esac fi -# Escape application args -save () { - for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done - echo " " -} -APP_ARGS=$(save "$@") -# Collect all arguments for the java command, following the shell quoting and substitution rules -eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' -# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong -if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then - cd "$(dirname "$0")" +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" fi +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat index 9618d8d9..5eed7ee8 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -1,100 +1,94 @@ -@rem -@rem Copyright 2015 the original author or authors. -@rem -@rem Licensed under the Apache License, Version 2.0 (the "License"); -@rem you may not use this file except in compliance with the License. -@rem You may obtain a copy of the License at -@rem -@rem https://www.apache.org/licenses/LICENSE-2.0 -@rem -@rem Unless required by applicable law or agreed to in writing, software -@rem distributed under the License is distributed on an "AS IS" BASIS, -@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -@rem See the License for the specific language governing permissions and -@rem limitations under the License. -@rem - -@if "%DEBUG%" == "" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto init - -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto init - -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:init -@rem Get command-line arguments, handling Windows variants - -if not "%OS%" == "Windows_NT" goto win9xME_args - -:win9xME_args -@rem Slurp the command line arguments. -set CMD_LINE_ARGS= -set _SKIP=2 - -:win9xME_args_slurp -if "x%~1" == "x" goto execute - -set CMD_LINE_ARGS=%* - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% - -:end -@rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH= + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega