Debian Codegraph @

libexec/pyenv-version-file-read

raw symbols SCIP
1 #!/usr/bin/env bash
2 # Usage: pyenv version-file-read <file>
3 set -e
4 [ -n "$PYENV_DEBUG" ] && set -x
5
6 VERSION_FILE="$1"
7
8 function is_version_safe() {
9 # As needed, check that the constructed path exists as a child path of PYENV_ROOT/versions
10 version="$1"
11 if [[ "$version" == ".." || "$version" == */* ]]; then
12 # Sanity check the value of version to prevent malicious path-traversal
13 (
14 cd "$PYENV_ROOT/versions/$version" &>/dev/null || exit 1
15 [[ "$PWD" == "$PYENV_ROOT/versions/"* ]]
16 )
17 return $?
18 else
19 return 0
20 fi
21 }
22
23 if [ -s "$VERSION_FILE" ]; then
24 # Read the first non-whitespace word from the specified version file.
25 # Be careful not to load it whole in case there's something crazy in it.
26 IFS="$IFS"$'\r'
27 sep=
28 while read -n 1024 -r version _ || [[ $version ]]; do
29 if [[ -z "$version" || "$version" == \#* ]]; then
30 # Skip empty lines and comments
31 continue
32 elif ! is_version_safe "$version"; then
33 # CVE-2022-35861 allowed arbitrary code execution in some contexts and is mitigated by is_version_safe.
34 echo "pyenv: invalid version \`$version' ignored in \`$VERSION_FILE'" >&2
35 continue
36 fi
37 printf "%s%s" "$sep" "$version"
38 sep=:
39 done <"$VERSION_FILE"
40 [[ $sep ]] && { echo; exit; }
41 fi
42
43 exit 1