Skip to content

Commit 54cf492

Browse files
author
Sergey DENISOV
committed
Harden Windows Git bootstrap fallbacks
1 parent a3a25a0 commit 54cf492

1 file changed

Lines changed: 99 additions & 24 deletions

File tree

run_app.bat

Lines changed: 99 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,24 @@ echo.
112112
echo ==> Downloading portable Git for Windows
113113
echo This stays in user space and does not require administrator rights.
114114

115-
mkdir "%SCRIPT_DIR%\.bootstrap-tools" 2>nul
115+
call :ensure_dir "%SCRIPT_DIR%\.bootstrap-tools"
116+
if errorlevel 1 exit /b %errorlevel%
116117
if exist "%PORTABLE_GIT_DIR%" rmdir /s /q "%PORTABLE_GIT_DIR%"
117-
mkdir "%PORTABLE_GIT_DIR%"
118+
call :ensure_dir "%PORTABLE_GIT_DIR%"
119+
if errorlevel 1 exit /b %errorlevel%
118120

119-
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
120-
"$ErrorActionPreference='Stop';" ^
121-
"$tmp = Join-Path $env:TEMP ('mingit-' + [guid]::NewGuid().ToString());" ^
122-
"New-Item -ItemType Directory -Path $tmp | Out-Null;" ^
123-
"$archive = Join-Path $tmp 'MinGit.zip';" ^
124-
"Invoke-WebRequest -Uri 'https://github.com/git-for-windows/git/releases/latest/download/MinGit-64-bit.zip' -OutFile $archive;" ^
125-
"Expand-Archive -Path $archive -DestinationPath '%PORTABLE_GIT_DIR%' -Force;" ^
126-
"Remove-Item -LiteralPath $tmp -Recurse -Force;"
121+
set "TMP_GIT_DIR=%TEMP%\mingit-%RANDOM%%RANDOM%"
122+
set "TMP_GIT_ARCHIVE=%TMP_GIT_DIR%\MinGit.zip"
123+
call :ensure_dir "%TMP_GIT_DIR%"
124+
if errorlevel 1 exit /b %errorlevel%
125+
126+
call :download_url_to_file "https://github.com/git-for-windows/git/releases/latest/download/MinGit-64-bit.zip" "%TMP_GIT_ARCHIVE%"
127+
if errorlevel 1 goto :portable_git_failed
128+
129+
call :extract_zip_to_dir "%TMP_GIT_ARCHIVE%" "%PORTABLE_GIT_DIR%"
130+
if errorlevel 1 goto :portable_git_failed
131+
132+
if exist "%TMP_GIT_DIR%" rmdir /s /q "%TMP_GIT_DIR%"
127133
if errorlevel 1 (
128134
echo Failed to download portable Git.
129135
exit /b 1
@@ -132,6 +138,11 @@ if errorlevel 1 (
132138
call :set_git_command
133139
exit /b %errorlevel%
134140

141+
:portable_git_failed
142+
if exist "%TMP_GIT_DIR%" rmdir /s /q "%TMP_GIT_DIR%"
143+
echo Failed to download portable Git.
144+
exit /b 1
145+
135146
:repo_is_dirty
136147
call :set_git_command
137148
if errorlevel 1 exit /b 1
@@ -175,22 +186,44 @@ exit /b %errorlevel%
175186
echo.
176187
echo ==> Downloading SK-Ana source archive
177188

178-
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
179-
"$ErrorActionPreference='Stop';" ^
180-
"$tmp = Join-Path $env:TEMP ('sk-ana-' + [guid]::NewGuid().ToString());" ^
181-
"New-Item -ItemType Directory -Path $tmp | Out-Null;" ^
182-
"$archive = Join-Path $tmp 'SK-Ana.zip';" ^
183-
"$repo = '%SK_ANA_REPO_URL%'.Replace('https://github.com/', '').Replace('.git', '');" ^
184-
"$url = 'https://github.com/' + $repo + '/archive/refs/heads/%SK_ANA_BRANCH%.zip';" ^
185-
"Invoke-WebRequest -Uri $url -OutFile $archive;" ^
186-
"Expand-Archive -Path $archive -DestinationPath $tmp -Force;" ^
187-
"$source = Get-ChildItem -Path $tmp -Directory | Where-Object { $_.Name -like 'SK-Ana-*' } | Select-Object -First 1;" ^
188-
"if (-not $source) { throw 'Could not unpack SK-Ana archive.' }" ^
189-
"if (Test-Path '%REPO_ROOT%') { Remove-Item -LiteralPath '%REPO_ROOT%' -Recurse -Force }" ^
190-
"Move-Item -LiteralPath $source.FullName -Destination '%REPO_ROOT%';" ^
191-
"Remove-Item -LiteralPath $tmp -Recurse -Force;"
189+
set "TMP_REPO_DIR=%TEMP%\sk-ana-%RANDOM%%RANDOM%"
190+
set "TMP_REPO_ARCHIVE=%TMP_REPO_DIR%\SK-Ana.zip"
191+
set "TMP_REPO_EXTRACT=%TMP_REPO_DIR%\extract"
192+
set "REPO_ARCHIVE_URL=%SK_ANA_REPO_URL%"
193+
set "REPO_ARCHIVE_URL=%REPO_ARCHIVE_URL:https://github.com/=%"
194+
set "REPO_ARCHIVE_URL=%REPO_ARCHIVE_URL:.git=%"
195+
set "REPO_ARCHIVE_URL=https://github.com/%REPO_ARCHIVE_URL%/archive/refs/heads/%SK_ANA_BRANCH%.zip"
196+
197+
call :ensure_dir "%TMP_REPO_DIR%"
198+
if errorlevel 1 exit /b %errorlevel%
199+
call :ensure_dir "%TMP_REPO_EXTRACT%"
200+
if errorlevel 1 exit /b %errorlevel%
201+
202+
call :download_url_to_file "%REPO_ARCHIVE_URL%" "%TMP_REPO_ARCHIVE%"
203+
if errorlevel 1 goto :download_repo_archive_failed
204+
205+
call :extract_zip_to_dir "%TMP_REPO_ARCHIVE%" "%TMP_REPO_EXTRACT%"
206+
if errorlevel 1 goto :download_repo_archive_failed
207+
208+
for /d %%D in ("%TMP_REPO_EXTRACT%\SK-Ana-*") do (
209+
set "REPO_ARCHIVE_SOURCE=%%~fD"
210+
goto :download_repo_archive_source_found
211+
)
212+
213+
echo Could not unpack SK-Ana archive.
214+
goto :download_repo_archive_failed
215+
216+
:download_repo_archive_source_found
217+
if exist "%REPO_ROOT%" rmdir /s /q "%REPO_ROOT%"
218+
move "%REPO_ARCHIVE_SOURCE%" "%REPO_ROOT%" >nul
219+
if errorlevel 1 goto :download_repo_archive_failed
220+
if exist "%TMP_REPO_DIR%" rmdir /s /q "%TMP_REPO_DIR%"
192221
exit /b %errorlevel%
193222

223+
:download_repo_archive_failed
224+
if exist "%TMP_REPO_DIR%" rmdir /s /q "%TMP_REPO_DIR%"
225+
exit /b 1
226+
194227
:bootstrap_repo_if_needed
195228
call :looks_like_repo_root "%REPO_ROOT%"
196229
if not errorlevel 1 (
@@ -234,3 +267,45 @@ if errorlevel 1 exit /b %errorlevel%
234267

235268
call :update_repo_if_possible
236269
exit /b %errorlevel%
270+
271+
:ensure_dir
272+
if exist "%~1\" exit /b 0
273+
mkdir "%~1" >nul 2>nul
274+
if exist "%~1\" exit /b 0
275+
echo Failed to create directory: %~1
276+
exit /b 1
277+
278+
:download_url_to_file
279+
set "DOWNLOAD_URL=%~1"
280+
set "DOWNLOAD_TARGET=%~2"
281+
282+
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
283+
"$ErrorActionPreference='Stop';" ^
284+
"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls;" ^
285+
"$client = New-Object System.Net.WebClient;" ^
286+
"$client.DownloadFile('%DOWNLOAD_URL%', '%DOWNLOAD_TARGET%');"
287+
if not errorlevel 1 exit /b 0
288+
289+
where curl.exe >nul 2>nul
290+
if errorlevel 1 exit /b 1
291+
292+
curl.exe -L --fail --retry 3 "%DOWNLOAD_URL%" -o "%DOWNLOAD_TARGET%"
293+
if errorlevel 1 exit /b 1
294+
exit /b 0
295+
296+
:extract_zip_to_dir
297+
set "ZIP_SOURCE=%~1"
298+
set "ZIP_DEST=%~2"
299+
300+
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
301+
"$ErrorActionPreference='Stop';" ^
302+
"Add-Type -AssemblyName System.IO.Compression.FileSystem;" ^
303+
"[System.IO.Compression.ZipFile]::ExtractToDirectory('%ZIP_SOURCE%', '%ZIP_DEST%');"
304+
if not errorlevel 1 exit /b 0
305+
306+
where tar.exe >nul 2>nul
307+
if errorlevel 1 exit /b 1
308+
309+
tar.exe -xf "%ZIP_SOURCE%" -C "%ZIP_DEST%"
310+
if errorlevel 1 exit /b 1
311+
exit /b 0

0 commit comments

Comments
 (0)