package opportunities import ( "strings" "testing" ) // THE INTAKE REFUSALS, tested where they are decided rather than through HTTP, because a validation rule // that only exists in a handler is one route away from being bypassed. // // Every case here is one Joseph named: malformed, oversized, and locked files must be survived GRACEFULLY. // Graceful means a named refusal a human can act on, never a panic and never a silent acceptance that // becomes a corrupt row for the extraction pipeline to trip over later. func TestMediaTypeAllowlistIsClosed(t *testing.T) { refused := []struct{ declared, name string }{ {"application/x-msdownload", "payload.exe"}, {"image/png", "screenshot.png"}, {"application/octet-stream", "mystery.bin"}, {"", "no-extension"}, } for _, c := range refused { mt := normalizeMediaType(c.declared, c.name) if _, ok := allowedMediaTypes[mt]; ok { t.Fatalf("media type %q from (%q, %q) was ALLOWED and should not be", mt, c.declared, c.name) } } // POSITIVE CONTROL. If the allowlist refused everything these assertions would pass while the feature // was entirely broken, which is the failure mode this build keeps finding. allowed := []struct{ declared, name string }{ {"application/pdf", "solicitation.pdf"}, {"", "statement_of_work.pdf"}, // recovered from the extension alone {"application/octet-stream", "attachment.docx"}, // browsers really do send this for a docx {"text/plain; charset=utf-8", "notes.txt"}, // parameters stripped {"APPLICATION/PDF", "SHOUTING.PDF"}, // case folded } for _, c := range allowed { mt := normalizeMediaType(c.declared, c.name) if _, ok := allowedMediaTypes[mt]; !ok { t.Fatalf("media type %q from (%q, %q) was REFUSED and should be allowed", mt, c.declared, c.name) } } } // WHAT GETS STORED IS ALWAYS A CLEAN TYPE FROM THE ALLOWLIST, never the caller's string. This is the // assertion that makes the case above safe: a mangled declared type may be RECOVERED from the extension, // and the recovered value is a literal key of the allowlist, so no control character, parameter, or // attacker chosen text reaches the media_type column. func TestStoredMediaTypeIsAlwaysAllowlistLiteral(t *testing.T) { for _, c := range []struct{ declared, name string }{ {"text/plain\x00", "null-byte.txt"}, {"text/plain; charset=\">