forked from mtusi2026/mtusi2026lab5
107 lines
5.8 KiB
Batchfile
107 lines
5.8 KiB
Batchfile
@echo off
|
|
setlocal EnableDelayedExpansion
|
|
cd /d "%~dp0.."
|
|
|
|
set BASE=http://localhost:8080
|
|
set PASS=Example-only-not-a-real-secret-123
|
|
set FIXTURE=tests\fixtures\sample.png
|
|
set TMP=%TEMP%\lab5-truncated
|
|
if not exist "%TMP%" mkdir "%TMP%"
|
|
|
|
call :guid
|
|
set RND=%GUIDVAL%
|
|
set U=trunc%RND%
|
|
call :register "%U%" "Truncated" u
|
|
if errorlevel 1 exit /b 1
|
|
set UID=!u_ID!
|
|
call :login "%U%" u
|
|
if errorlevel 1 exit /b 1
|
|
set TOKEN=!u_TOKEN!
|
|
|
|
curl.exe -s -X POST "%BASE%/api/v1/chats" ^
|
|
-H "Authorization: Bearer %TOKEN%" -H "Content-Type: application/json" ^
|
|
-d "{\"title\":\"Truncated images\",\"member_ids\":[\"%UID%\"]}" ^
|
|
-o "%TMP%\chat.json" -w "%%{http_code}" > "%TMP%\chat.code"
|
|
call :check_code chat.code 201 "create chat"
|
|
if errorlevel 1 exit /b 1
|
|
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\chat.json' -Raw | ConvertFrom-Json).id"') do set CHAT_ID=%%a
|
|
|
|
echo === Build truncated PNG (valid IHDR, cut IDAT, no IEND) ===
|
|
powershell -NoProfile -Command "$b=[IO.File]::ReadAllBytes('%FIXTURE%'); $cut=[int]($b.Length*0.6); $out=New-Object byte[] $cut; [Array]::Copy($b,0,$out,0,$cut); [IO.File]::WriteAllBytes('%TMP%\truncated.png',$out); Write-Host ('truncated.png bytes='+$cut+' of '+$b.Length)"
|
|
|
|
echo === Truncated PNG must be rejected with 415 at upload ===
|
|
curl.exe -s -X POST "%BASE%/api/v1/chats/%CHAT_ID%/attachments?filename=truncated.png" ^
|
|
-H "Authorization: Bearer %TOKEN%" -H "Content-Type: image/png" ^
|
|
--data-binary "@%TMP%\truncated.png" ^
|
|
-o "%TMP%\png.json" -w "%%{http_code}" > "%TMP%\png.code"
|
|
call :check_code png.code 415 "truncated png rejected at upload"
|
|
if errorlevel 1 ( type "%TMP%\png.json" & exit /b 1 )
|
|
echo PASS: truncated PNG returned 415 (not 202)
|
|
|
|
echo === Build valid JPEG, then truncate it ===
|
|
powershell -NoProfile -Command "Add-Type -AssemblyName System.Drawing; $bmp=New-Object System.Drawing.Bitmap 64,64; for($x=0;$x -lt 64;$x++){ for($y=0;$y -lt 64;$y++){ $bmp.SetPixel($x,$y,[System.Drawing.Color]::FromArgb(($x*4),($y*4),128)) } }; $bmp.Save('%TMP%\full.jpg',[System.Drawing.Imaging.ImageFormat]::Jpeg); $bmp.Dispose(); $b=[IO.File]::ReadAllBytes('%TMP%\full.jpg'); $cut=[int]($b.Length*0.6); $out=New-Object byte[] $cut; [Array]::Copy($b,0,$out,0,$cut); [IO.File]::WriteAllBytes('%TMP%\truncated.jpg',$out); Write-Host ('truncated.jpg bytes='+$cut+' of '+$b.Length)"
|
|
|
|
echo === Truncated JPEG must be rejected with 415 at upload ===
|
|
curl.exe -s -X POST "%BASE%/api/v1/chats/%CHAT_ID%/attachments?filename=truncated.jpg" ^
|
|
-H "Authorization: Bearer %TOKEN%" -H "Content-Type: image/jpeg" ^
|
|
--data-binary "@%TMP%\truncated.jpg" ^
|
|
-o "%TMP%\jpg.json" -w "%%{http_code}" > "%TMP%\jpg.code"
|
|
call :check_code jpg.code 415 "truncated jpeg rejected at upload"
|
|
if errorlevel 1 ( type "%TMP%\jpg.json" & exit /b 1 )
|
|
echo PASS: truncated JPEG returned 415 (not 202)
|
|
|
|
echo === Valid JPEG still accepted with 202 (no false positives) ===
|
|
curl.exe -s -X POST "%BASE%/api/v1/chats/%CHAT_ID%/attachments?filename=full.jpg" ^
|
|
-H "Authorization: Bearer %TOKEN%" -H "Content-Type: image/jpeg" ^
|
|
--data-binary "@%TMP%\full.jpg" ^
|
|
-o "%TMP%\ok.json" -w "%%{http_code}" > "%TMP%\ok.code"
|
|
call :check_code ok.code 202 "valid jpeg accepted"
|
|
if errorlevel 1 ( type "%TMP%\ok.json" & exit /b 1 )
|
|
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\ok.json' -Raw | ConvertFrom-Json).id"') do set ATT_ID=%%a
|
|
powershell -NoProfile -Command "for($i=0;$i -lt 60;$i++){ $m=Invoke-RestMethod -Uri '%BASE%/api/v1/attachments/%ATT_ID%' -Headers @{Authorization='Bearer %TOKEN%'}; if($m.status -eq 'ready'){ exit 0 }; if($m.status -eq 'failed'){ exit 2 }; Start-Sleep -Seconds 1 }; exit 1"
|
|
if errorlevel 1 ( echo FAIL: valid jpeg did not reach ready & exit /b 1 )
|
|
echo PASS: valid JPEG reached ready
|
|
|
|
echo === Valid PNG still accepted (fixture regression) ===
|
|
curl.exe -s -X POST "%BASE%/api/v1/chats/%CHAT_ID%/attachments?filename=sample.png" ^
|
|
-H "Authorization: Bearer %TOKEN%" -H "Content-Type: image/png" ^
|
|
--data-binary "@%FIXTURE%" ^
|
|
-o "%TMP%\png_ok.json" -w "%%{http_code}" > "%TMP%\png_ok.code"
|
|
call :check_code png_ok.code 202 "valid png accepted"
|
|
if errorlevel 1 exit /b 1
|
|
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\png_ok.json' -Raw | ConvertFrom-Json).id"') do set PATT=%%a
|
|
powershell -NoProfile -Command "for($i=0;$i -lt 60;$i++){ $m=Invoke-RestMethod -Uri '%BASE%/api/v1/attachments/%PATT%' -Headers @{Authorization='Bearer %TOKEN%'}; if($m.status -eq 'ready'){ exit 0 }; if($m.status -eq 'failed'){ exit 2 }; Start-Sleep -Seconds 1 }; exit 1"
|
|
if errorlevel 1 ( echo FAIL: valid png did not reach ready & exit /b 1 )
|
|
echo PASS: valid PNG reached ready
|
|
|
|
echo.
|
|
echo TRUNCATED-IMAGE CHECKS PASSED
|
|
exit /b 0
|
|
|
|
:guid
|
|
for /f "delims=" %%a in ('powershell -NoProfile -Command "[guid]::NewGuid().ToString('N').Substring(0,8)"') do set GUIDVAL=%%a
|
|
exit /b 0
|
|
|
|
:register
|
|
curl.exe -s -X POST "%BASE%/api/v1/users" ^
|
|
-H "Content-Type: application/json" ^
|
|
-d "{\"username\":\"%~1\",\"display_name\":\"%~2\",\"password\":\"%PASS%\"}" ^
|
|
-o "%TMP%\%~3.json" -w "%%{http_code}" > "%TMP%\%~3.code"
|
|
set /p RC=<"%TMP%\%~3.code"
|
|
if not "%RC%"=="201" ( echo FAIL: register %~1, HTTP %RC% & type "%TMP%\%~3.json" & exit /b 1 )
|
|
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~3.json' -Raw | ConvertFrom-Json).id"') do set "%~3_ID=%%a"
|
|
exit /b 0
|
|
|
|
:login
|
|
curl.exe -s -X POST "%BASE%/api/v1/auth/sessions" ^
|
|
-u "%~1:%PASS%" ^
|
|
-o "%TMP%\%~2.json" -w "%%{http_code}" > "%TMP%\%~2.code"
|
|
set /p RC=<"%TMP%\%~2.code"
|
|
if not "%RC%"=="201" ( echo FAIL: login %~1, HTTP %RC% & type "%TMP%\%~2.json" & exit /b 1 )
|
|
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~2.json' -Raw | ConvertFrom-Json).token"') do set "%~2_TOKEN=%%a"
|
|
exit /b 0
|
|
|
|
:check_code
|
|
set /p RC=<"%TMP%\%~1"
|
|
if not "%RC%"=="%~2" ( echo FAIL: %~3 expected %~2, got %RC% & exit /b 1 )
|
|
exit /b 0 |