# ============================================================================== # ClassNotify P2.3: Visual Receipt End-to-End Quality Gates Verification Suite # ============================================================================== param ( [string]$BaseUrl = "https://classnotify.paradise.eu.org", [string]$VpsHost = "158.179.179.29", [string]$SshKey = "C:\Users\Administrator\.ssh\id_rsa_tokyo" ) $ErrorActionPreference = "Continue" Add-Type -AssemblyName System.Net.Http Add-Type -AssemblyName System.Security Write-Host "==========================================================" -ForegroundColor Cyan Write-Host " ClassNotify P2.3 Visual Receipt Quality Gates E2E Test " -ForegroundColor Cyan Write-Host "==========================================================" -ForegroundColor Cyan $passed = 0 $failed = 0 function Report-Result([string]$gateName, [bool]$passedCondition, [string]$details = "") { if ($passedCondition) { Write-Host " [PASS] $gateName" -ForegroundColor Green if ($details) { Write-Host " $details" -ForegroundColor DarkGray } $script:passed++ } else { Write-Host " [FAIL] $gateName" -ForegroundColor Red if ($details) { Write-Host " $details" -ForegroundColor Yellow } $script:failed++ } } # 1. Load Device Credentials Write-Host "`n>>> [Gate 1] Checking local device credentials..." -ForegroundColor Yellow $credsPath = "C:\ProgramData\ClassNotify\device_credentials.json" $bytes = [System.IO.File]::ReadAllBytes($credsPath) $creds = $null try { Add-Type -AssemblyName System.Security $entropy = [System.Text.Encoding]::UTF8.GetBytes("ClassNotify_Device_Credential_V1") $dec = [System.Security.Cryptography.ProtectedData]::Unprotect($bytes, $entropy, [System.Security.Cryptography.DataProtectionScope]::LocalMachine) $creds = [System.Text.Encoding]::UTF8.GetString($dec) | ConvertFrom-Json } catch { $creds = [System.Text.Encoding]::UTF8.GetString($bytes) | ConvertFrom-Json } $deviceId = $creds.DeviceId $deviceToken = $creds.DeviceToken $tenantId = $creds.TenantId Write-Host " DeviceId: $deviceId" -ForegroundColor White Write-Host " TenantId: $tenantId" -ForegroundColor White $notifId = "msg_1790129104698_c152" $receiptId = "rcpt_ac98f0bd6dca4a11a66a19c2d738a88f" # Generate a valid minimal JPEG byte array $sampleJpeg = [byte[]]@( 0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00, 0x01, 0x01, 0x01, 0x00, 0x48, 0x00, 0x48, 0x00, 0x00, 0xFF, 0xDB, 0x00, 0x43, 0x00, 0x08, 0x06, 0x06, 0x07, 0x06, 0x05, 0x08, 0x07, 0x07, 0x07, 0x09, 0x09, 0x08, 0x0A, 0x0C, 0x14, 0x0D, 0x0C, 0x0B, 0x0B, 0x0C, 0x19, 0x12, 0x13, 0x0F, 0x14, 0x1D, 0x1A, 0x1F, 0x1E, 0x1D, 0x1A, 0x1C, 0x1C, 0x20, 0x24, 0x2E, 0x27, 0x20, 0x22, 0x2C, 0x23, 0x1C, 0x1C, 0x28, 0x37, 0x29, 0x2C, 0x30, 0x31, 0x34, 0x34, 0x34, 0x1F, 0x27, 0x39, 0x3D, 0x38, 0x32, 0x3C, 0x2E, 0x33, 0x34, 0x32, 0xFF, 0xC0, 0x00, 0x0B, 0x08, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x11, 0x00, 0xFF, 0xC4, 0x00, 0x1F, 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0xFF, 0xDA, 0x00, 0x08, 0x01, 0x01, 0x00, 0x00, 0x3F, 0x00, 0x7F, 0x00, 0xFF, 0xD9 ) $handler = [System.Net.Http.HttpClientHandler]::new() $http = [System.Net.Http.HttpClient]::new($handler) $http.Timeout = [TimeSpan]::FromSeconds(15) # Helper function to send HTTP requests cleanly function Send-HttpReq([string]$method, [string]$url, [byte[]]$body, [string]$contentType, [hashtable]$headers) { $req = [System.Net.Http.HttpRequestMessage]::new([System.Net.Http.HttpMethod]::$method, $url) if ($body -ne $null) { $content = [System.Net.Http.ByteArrayContent]::new($body) if ($contentType) { $content.Headers.ContentType = [System.Net.Http.Headers.MediaTypeHeaderValue]::Parse($contentType) } $req.Content = $content } if ($headers) { foreach ($k in $headers.Keys) { $req.Headers.TryAddWithoutValidation($k, [string]$headers[$k]) | Out-Null } } return $http.SendAsync($req).GetAwaiter().GetResult() } # Gate 2: Valid Upload Write-Host "`n>>> [Gate 2] Testing valid visual receipt upload..." -ForegroundColor Yellow $uploadUrl = "$BaseUrl/api/device/notifications/$notifId/visual-receipt" $res2 = Send-HttpReq "Post" $uploadUrl $sampleJpeg "image/jpeg" @{ "X-Device-Id" = $deviceId "X-Device-Token" = $deviceToken } $body2 = $res2.Content.ReadAsStringAsync().GetAwaiter().GetResult() $json2 = $body2 | ConvertFrom-Json $uploadOk = ($res2.IsSuccessStatusCode -and $json2.success -eq $true -and $json2.visualReceiptState -eq "Available") Report-Result "Valid Visual Receipt Upload" $uploadOk "HTTP $([int]$res2.StatusCode), State: $($json2.visualReceiptState), Expires: $($json2.expiresAt)" # Gate 3: First-Receipt-Wins Idempotent Upload Write-Host "`n>>> [Gate 3] Testing idempotent duplicate upload (first wins)..." -ForegroundColor Yellow $res3 = Send-HttpReq "Post" $uploadUrl $sampleJpeg "image/jpeg" @{ "X-Device-Id" = $deviceId "X-Device-Token" = $deviceToken } $body3 = $res3.Content.ReadAsStringAsync().GetAwaiter().GetResult() $json3 = $body3 | ConvertFrom-Json $dupOk = ($res3.IsSuccessStatusCode -and $json3.success -eq $true -and $json3.visualReceiptState -eq "Available") Report-Result "Idempotent Re-upload (First Wins)" $dupOk "HTTP $([int]$res3.StatusCode) without error" # Gate 4: Non-JPEG Rejection (415) Write-Host "`n>>> [Gate 4] Testing non-JPEG rejection (415)..." -ForegroundColor Yellow $textBytes = [System.Text.Encoding]::UTF8.GetBytes("THIS_IS_NOT_A_JPEG_FILE") $res4 = Send-HttpReq "Post" $uploadUrl $textBytes "text/plain" @{ "X-Device-Id" = $deviceId "X-Device-Token" = $deviceToken } $is415 = ([int]$res4.StatusCode -eq 415) Report-Result "Non-JPEG Content-Type Rejection (415)" $is415 "HTTP $([int]$res4.StatusCode)" # Gate 5: Oversized Payload Rejection (> 512 KiB -> 413) Write-Host "`n>>> [Gate 5] Testing oversized payload rejection (413)..." -ForegroundColor Yellow $bigBytes = New-Object byte[] (600 * 1024) $bigBytes[0] = 0xFF; $bigBytes[1] = 0xD8 $bigBytes[$bigBytes.Length - 2] = 0xFF; $bigBytes[$bigBytes.Length - 1] = 0xD9 $res5 = Send-HttpReq "Post" $uploadUrl $bigBytes "image/jpeg" @{ "X-Device-Id" = $deviceId "X-Device-Token" = $deviceToken } $is413 = ([int]$res5.StatusCode -eq 413) Report-Result "Oversized Payload Rejection (413)" $is413 "HTTP $([int]$res5.StatusCode)" # Gate 6: Fake NotificationId Rejection (404) Write-Host "`n>>> [Gate 6] Testing fake NotificationId rejection (404)..." -ForegroundColor Yellow $fakeUrl = "$BaseUrl/api/device/notifications/msg_nonexistent_99999/visual-receipt" $res6 = Send-HttpReq "Post" $fakeUrl $sampleJpeg "image/jpeg" @{ "X-Device-Id" = $deviceId "X-Device-Token" = $deviceToken } $is404 = ([int]$res6.StatusCode -eq 404) Report-Result "Fake NotificationId Rejection (404)" $is404 "HTTP $([int]$res6.StatusCode)" # Gate 7: Invalid Device Token Rejection (401) Write-Host "`n>>> [Gate 7] Testing invalid device credentials rejection (401)..." -ForegroundColor Yellow $res7 = Send-HttpReq "Post" $uploadUrl $sampleJpeg "image/jpeg" @{ "X-Device-Id" = $deviceId "X-Device-Token" = "invalid_token_12345" } $is401 = ([int]$res7.StatusCode -eq 401) Report-Result "Invalid Token Rejection (401)" $is401 "HTTP $([int]$res7.StatusCode)" # Gate 8: Web Authenticated GET with Bearer Token & Cache Headers Write-Host "`n>>> [Gate 8] Testing Teacher Authenticated GET with Cache-Control no-store..." -ForegroundColor Yellow $viewUrl = "$BaseUrl/api/notifications/$notifId/receipts/$deviceId/visual-receipt" # 8.1 Without Token -> 401 $res8a = Send-HttpReq "Get" $viewUrl $null $null $null $unauthBlocked = ([int]$res8a.StatusCode -eq 401) Report-Result "Unauthenticated GET Blocked (401)" $unauthBlocked "HTTP $([int]$res8a.StatusCode)" # 8.2 With Valid Teacher Token $tUser = [Environment]::GetEnvironmentVariable('CLASSNOTIFY_E2E_USERNAME', 'User') $tPass = [Environment]::GetEnvironmentVariable('CLASSNOTIFY_E2E_PASSWORD', 'User') $loginUrl = "$BaseUrl/api/auth/login" $loginPayload = [System.Text.Encoding]::UTF8.GetBytes((@{ username = $tUser; password = $tPass } | ConvertTo-Json)) $resLogin = Send-HttpReq "Post" $loginUrl $loginPayload "application/json" $null $token = $null if ($resLogin.IsSuccessStatusCode) { $loginResp = $resLogin.Content.ReadAsStringAsync().GetAwaiter().GetResult() | ConvertFrom-Json $token = $loginResp.token } if ($token) { $res8b = Send-HttpReq "Get" $viewUrl $null $null @{ "Authorization" = "Bearer $token" } $cacheControl = $res8b.Headers.CacheControl.ToString() $contentType = $res8b.Content.Headers.ContentType.MediaType $downloadBytes = $res8b.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult() $magicValid = ($downloadBytes.Length -ge 4 -and $downloadBytes[0] -eq 0xFF -and $downloadBytes[1] -eq 0xD8) $isNoStore = ($cacheControl -match "no-store") $viewOk = ($res8b.IsSuccessStatusCode -and $contentType -eq "image/jpeg" -and $magicValid -and $isNoStore) Report-Result "Teacher GET Visual Receipt" $viewOk "HTTP $([int]$res8b.StatusCode), ContentType: $contentType, CacheControl: $cacheControl, Size: $($downloadBytes.Length)" } else { Report-Result "Teacher GET Visual Receipt" $false "Failed to login as $tUser" } # Gate 9: Verify Server Disk Storage & Safe Relative Paths Write-Host "`n>>> [Gate 9] Verifying Server Disk Storage under /var/lib/classnotify/visual-receipts/..." -ForegroundColor Yellow $checkDiskCmd = "find /var/lib/classnotify/visual-receipts -name '${receiptId}.jpg' -ls" $b64Disk = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($checkDiskCmd)) $diskOutput = ssh -i $SshKey -o StrictHostKeyChecking=no root@$VpsHost "echo $b64Disk | base64 -d | bash" $fileFound = ($diskOutput -match $receiptId) Report-Result "Storage Isolation & File Creation on VPS" $fileFound "Disk entry: $diskOutput" # Gate 10: Verify Database NotificationRecordDto History Receipts mapping Write-Host "`n>>> [Gate 10] Verifying NotificationReceipts DB Record State & Hash..." -ForegroundColor Yellow $dbCheckCmd = 'sudo -u postgres psql -d classnotify -t -A -c "SELECT \"VisualReceiptState\", \"VisualReceiptSizeBytes\", \"VisualReceiptSha256\" FROM \"NotificationReceipts\" WHERE \"Id\" = ''' + $receiptId + ''';"' $b64Db = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($dbCheckCmd)) $dbOutput = ssh -i $SshKey -o StrictHostKeyChecking=no root@$VpsHost "echo $b64Db | base64 -d | bash" $fields = $dbOutput.Trim().Split('|') $dbStateOk = ($fields[0] -eq "Available" -and [long]$fields[1] -gt 0 -and $fields[2].Length -eq 64) Report-Result "Database Receipt Record Integrity" $dbStateOk "State: $($fields[0]), Size: $($fields[1]), SHA256: $($fields[2])" # Gate 11: Direct URL Traversal / Guessing Blocked Write-Host "`n>>> [Gate 11] Verifying Static Path Guessing is Not Exposed by Caddy..." -ForegroundColor Yellow $guessUrl = "$BaseUrl/visual-receipts/$receiptId.jpg" $guessRes = Send-HttpReq "Get" $guessUrl $null $null $null $guessBlocked = ([int]$guessRes.StatusCode -eq 404 -or ([int]$guessRes.StatusCode -eq 200 -and $guessRes.Content.Headers.ContentType.MediaType -match "text/html")) Report-Result "Direct File Path Traversal Blocked" $guessBlocked "Static disk path not directly served" # Gate 12: Expired State returns 410 Gone Write-Host "`n>>> [Gate 12] Testing Expired Visual Receipt returns 410 Gone..." -ForegroundColor Yellow $expireCmd = 'sudo -u postgres psql -d classnotify -c "UPDATE \"NotificationReceipts\" SET \"VisualReceiptState\" = ''Expired'' WHERE \"Id\" = ''' + $receiptId + ''';"' $b64Exp = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($expireCmd)) ssh -i $SshKey -o StrictHostKeyChecking=no root@$VpsHost "echo $b64Exp | base64 -d | bash" | Out-Null $res12 = Send-HttpReq "Get" $viewUrl $null $null @{ "Authorization" = "Bearer $token" } $is410 = ([int]$res12.StatusCode -eq 410) Report-Result "Expired Visual Receipt Returns 410 Gone" $is410 "HTTP $([int]$res12.StatusCode)" # Restore to Available $restoreCmd = 'sudo -u postgres psql -d classnotify -c "UPDATE \"NotificationReceipts\" SET \"VisualReceiptState\" = ''Available'' WHERE \"Id\" = ''' + $receiptId + ''';"' $b64Res = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($restoreCmd)) ssh -i $SshKey -o StrictHostKeyChecking=no root@$VpsHost "echo $b64Res | base64 -d | bash" | Out-Null Write-Host "`n==========================================================" -ForegroundColor Cyan Write-Host " Quality Gates Summary: Passed=$passed, Failed=$failed " -ForegroundColor $(if ($failed -eq 0) { "Green" } else { "Red" }) Write-Host "==========================================================" -ForegroundColor Cyan if ($failed -gt 0) { exit 1 } exit 0