147 lines
6.9 KiB
Batchfile
147 lines
6.9 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-negative
|
|
if not exist "%TMP%" mkdir "%TMP%"
|
|
|
|
call :guid
|
|
set RND=%GUIDVAL%
|
|
set ALICE=alice%RND%
|
|
set BOB=bob%RND%
|
|
set EVE=eve%RND%
|
|
|
|
call :register "%ALICE%" "Alice" alice
|
|
if errorlevel 1 exit /b 1
|
|
set ALICE_ID=!alice_ID!
|
|
call :register "%BOB%" "Bob" bob
|
|
if errorlevel 1 exit /b 1
|
|
set BOB_ID=!bob_ID!
|
|
call :register "%EVE%" "Eve" eve
|
|
if errorlevel 1 exit /b 1
|
|
call :login "%ALICE%" alice
|
|
if errorlevel 1 exit /b 1
|
|
set ALICE_TOKEN=!alice_TOKEN!
|
|
call :login "%EVE%" eve
|
|
if errorlevel 1 exit /b 1
|
|
set EVE_TOKEN=!eve_TOKEN!
|
|
|
|
curl.exe -s -X POST "%BASE%/api/v1/chats" ^
|
|
-H "Authorization: Bearer %ALICE_TOKEN%" -H "Content-Type: application/json" ^
|
|
-d "{\"title\":\"Neg chat1\",\"member_ids\":[\"%ALICE_ID%\",\"%BOB_ID%\"]}" ^
|
|
-o "%TMP%\chat1.json" -w "%%{http_code}" > "%TMP%\chat1.code"
|
|
call :check_code chat1.code 201 "create chat1"
|
|
if errorlevel 1 exit /b 1
|
|
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\chat1.json' -Raw | ConvertFrom-Json).id"') do set CHAT1_ID=%%a
|
|
|
|
curl.exe -s -X POST "%BASE%/api/v1/chats" ^
|
|
-H "Authorization: Bearer %ALICE_TOKEN%" -H "Content-Type: application/json" ^
|
|
-d "{\"title\":\"Neg chat2\",\"member_ids\":[\"%ALICE_ID%\",\"%EVE_ID%\"]}" ^
|
|
-o "%TMP%\chat2.json" -w "%%{http_code}" > "%TMP%\chat2.code"
|
|
call :check_code chat2.code 201 "create chat2"
|
|
if errorlevel 1 exit /b 1
|
|
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\chat2.json' -Raw | ConvertFrom-Json).id"') do set CHAT2_ID=%%a
|
|
|
|
echo === 1. Too large file 413 ===
|
|
powershell -NoProfile -Command "$sig=[byte[]](0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A); $pad=New-Object byte[] (11534336-8); [IO.File]::WriteAllBytes('%TMP%\large.png', $sig+$pad)"
|
|
curl.exe -s -o nul -X POST "%BASE%/api/v1/chats/%CHAT1_ID%/attachments?filename=large.png" ^
|
|
-H "Authorization: Bearer %ALICE_TOKEN%" -H "Content-Type: image/png" ^
|
|
--data-binary "@%TMP%\large.png" -w "%%{http_code}" > "%TMP%\large.code"
|
|
call :check_code large.code 413 "too large upload"
|
|
if errorlevel 1 exit /b 1
|
|
|
|
echo === 2. Broken image 415 ===
|
|
powershell -NoProfile -Command "[IO.File]::WriteAllBytes('%TMP%\broken.png',[byte[]](0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A,0xFF,0xFF,0xFF,0xFF))"
|
|
curl.exe -s -o nul -X POST "%BASE%/api/v1/chats/%CHAT1_ID%/attachments?filename=broken.png" ^
|
|
-H "Authorization: Bearer %ALICE_TOKEN%" -H "Content-Type: image/png" ^
|
|
--data-binary "@%TMP%\broken.png" -w "%%{http_code}" > "%TMP%\broken.code"
|
|
call :check_code broken.code 415 "broken png"
|
|
if errorlevel 1 exit /b 1
|
|
|
|
echo === 3. MIME mismatch 415 ===
|
|
curl.exe -s -o nul -X POST "%BASE%/api/v1/chats/%CHAT1_ID%/attachments?filename=fake.png" ^
|
|
-H "Authorization: Bearer %ALICE_TOKEN%" -H "Content-Type: image/jpeg" ^
|
|
--data-binary "@%FIXTURE%" -w "%%{http_code}" > "%TMP%\mime.code"
|
|
call :check_code mime.code 415 "jpeg disguised as png"
|
|
if errorlevel 1 exit /b 1
|
|
|
|
echo === 4. Dangerous filename 400 ===
|
|
curl.exe -s -o nul -X POST "%BASE%/api/v1/chats/%CHAT1_ID%/attachments?filename=..%%2Fevil.png" ^
|
|
-H "Authorization: Bearer %ALICE_TOKEN%" -H "Content-Type: image/png" ^
|
|
--data-binary "@%FIXTURE%" -w "%%{http_code}" > "%TMP%\filename.code"
|
|
call :check_code filename.code 400 "path traversal filename"
|
|
if errorlevel 1 exit /b 1
|
|
|
|
echo === 5. Valid upload, wait ready ===
|
|
curl.exe -s -X POST "%BASE%/api/v1/chats/%CHAT1_ID%/attachments?filename=sample.png" ^
|
|
-H "Authorization: Bearer %ALICE_TOKEN%" -H "Content-Type: image/png" ^
|
|
--data-binary "@%FIXTURE%" -o "%TMP%\att.json" -w "%%{http_code}" > "%TMP%\att.code"
|
|
call :check_code att.code 202 "valid upload"
|
|
if errorlevel 1 exit /b 1
|
|
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\att.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 %ALICE_TOKEN%'}; if($m.status -eq 'ready' -or $m.status -eq 'failed'){ exit 0 }; Start-Sleep -Seconds 1 }; exit 1"
|
|
if errorlevel 1 ( echo FAIL: not ready in 60s & exit /b 1 )
|
|
|
|
echo === 6. Unknown variant 404 ===
|
|
curl.exe -s -o nul -H "Authorization: Bearer %ALICE_TOKEN%" -w "%%{http_code}" "%BASE%/api/v1/attachments/%ATT_ID%/content?variant=nope" > "%TMP%\variant.code"
|
|
call :check_code variant.code 404 "unknown variant"
|
|
if errorlevel 1 exit /b 1
|
|
|
|
echo === 7. Cross-chat reference 404 ===
|
|
call :guid
|
|
set KEY1=%GUIDVAL%
|
|
curl.exe -s -o nul -X POST "%BASE%/api/v1/chats/%CHAT2_ID%/messages" ^
|
|
-H "Authorization: Bearer %ALICE_TOKEN%" -H "Idempotency-Key: %KEY1%" -H "Content-Type: application/json" ^
|
|
-d "{\"text\":\"\",\"attachment_ids\":[\"%ATT_ID%\"]}" -w "%%{http_code}" > "%TMP%\crosschat.code"
|
|
call :check_code crosschat.code 404 "cross-chat attachment reference"
|
|
if errorlevel 1 exit /b 1
|
|
|
|
echo === 8. Unknown attachment id 404 ===
|
|
for /f "delims=" %%a in ('powershell -NoProfile -Command "[guid]::NewGuid().ToString()"') do set UNKNOWN=%%a
|
|
call :guid
|
|
set KEY2=%GUIDVAL%
|
|
curl.exe -s -o nul -X POST "%BASE%/api/v1/chats/%CHAT1_ID%/messages" ^
|
|
-H "Authorization: Bearer %ALICE_TOKEN%" -H "Idempotency-Key: %KEY2%" -H "Content-Type: application/json" ^
|
|
-d "{\"text\":\"\",\"attachment_ids\":[\"%UNKNOWN%\"]}" -w "%%{http_code}" > "%TMP%\unknown.code"
|
|
call :check_code unknown.code 404 "unknown attachment id"
|
|
if errorlevel 1 exit /b 1
|
|
|
|
echo === 9. Eve download 404 ===
|
|
curl.exe -s -o nul -H "Authorization: Bearer %EVE_TOKEN%" -w "%%{http_code}" "%BASE%/api/v1/attachments/%ATT_ID%/content" > "%TMP%\eve_dl.code"
|
|
call :check_code eve_dl.code 404 "eve download"
|
|
if errorlevel 1 exit /b 1
|
|
|
|
echo.
|
|
echo ALL NEGATIVE 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 |