forked from mtusi2026/mtusi2026lab4
223 lines
7.0 KiB
Batchfile
223 lines
7.0 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%\lab4-negative
|
|
if not exist "%TMP%" mkdir "%TMP%"
|
|
|
|
call :guid
|
|
set RND=%GUIDVAL%
|
|
|
|
set ALICE=alice%RND%
|
|
set BOB=bob%RND%
|
|
set EVE=eve%RND%
|
|
|
|
echo === Setup users ===
|
|
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 "%BOB%" bob
|
|
if errorlevel 1 exit /b 1
|
|
set BOB_TOKEN=!bob_TOKEN!
|
|
|
|
call :login "%EVE%" eve
|
|
if errorlevel 1 exit /b 1
|
|
set EVE_TOKEN=!eve_TOKEN!
|
|
|
|
echo === Create chat1 alice+bob ===
|
|
curl.exe -s -X POST "%BASE%/api/v1/chats" ^
|
|
-H "Authorization: Bearer %ALICE_TOKEN%" ^
|
|
-H "Content-Type: application/json" ^
|
|
-d "{\"title\":\"Negative 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
|
|
|
|
echo === Create chat2 alice+eve ===
|
|
curl.exe -s -X POST "%BASE%/api/v1/chats" ^
|
|
-H "Authorization: Bearer %ALICE_TOKEN%" ^
|
|
-H "Content-Type: application/json" ^
|
|
-d "{\"title\":\"Negative 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 must return 413 ===
|
|
powershell -NoProfile -Command "$path='%TMP%\large.png'; $sig=[byte[]](0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A); $pad=New-Object byte[] (11534336-8); [IO.File]::WriteAllBytes($path, $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 must return 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 must return 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 must return 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 into chat1 ===
|
|
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 201 "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
|
|
echo attachment=%ATT_ID% in chat1=%CHAT1_ID%
|
|
|
|
echo === 6. Cross-chat attachment reference must return 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 === 7. Unknown attachment id must return 404 ===
|
|
call :guid
|
|
set UNKNOWN=%%a
|
|
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 === 8. Eve cannot download chat1 attachment ===
|
|
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
|
|
rem %1 username, %2 display_name, %3 prefix
|
|
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
|
|
rem %1 username, %2 prefix
|
|
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"
|
|
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~2.json' -Raw | ConvertFrom-Json).user.id"') do set "%~2_UID=%%a"
|
|
exit /b 0
|
|
|
|
:check_code
|
|
rem %1 code file, %2 expected status, %3 label
|
|
set /p RC=<"%TMP%\%~1"
|
|
if not "%RC%"=="%~2" (
|
|
echo FAIL: %~3 expected %~2, got %RC%
|
|
exit /b 1
|
|
)
|
|
exit /b 0 |