Files
moloapp/tests/example/security/upload_test.cpp
T

25 lines
1.7 KiB
C++
Raw Normal View History

// Route: /security/upload (extension allowlist, size cap, magic bytes)
#include "support/Assert.hpp"
using namespace molo::example::test;
static const char* kForm = "application/x-www-form-urlencoded";
int main() {
Fixture f;
check(contains(f.client->get("/security/upload").body, "<form"), "form renders");
auto ok = f.client->post("/security/upload", "filename=a.pdf&content_hex=255044462d31", kForm);
check(ok.status == 200 && contains(ok.body, "Accepted a.pdf"), "valid pdf accepted");
auto fake = f.client->post("/security/upload", "filename=a.png&content_hex=255044462d31", kForm);
check(fake.status == 422 && contains(fake.body, "magic bytes"), "magic mismatch inline 422");
auto ext = f.client->post("/security/upload", "filename=a.exe&content_hex=4d5a", kForm);
check(ext.status == 422 && contains(ext.body, "extension not allowed"), "bad extension");
auto path = f.client->post("/security/upload", "filename=..%2Fa.txt&content_hex=6869", kForm);
check(path.status == 422, "path in name rejected");
auto hex = f.client->post("/security/upload", "filename=a.txt&content_hex=zz", kForm);
check(hex.status == 422 && contains(hex.body, "hex digits"), "bad hex");
auto big = f.client->post("/security/upload", "filename=a.txt&content_hex=" + std::string(2050, '4') + std::string(2050, '1'), kForm);
check(big.status == 422 || big.status == 413, "oversize rejected");
auto bigok = f.client->post("/security/upload", "filename=a.txt&content_hex=" + std::string(2050, '6'), kForm);
check(bigok.status == 413, "over 1024 bytes is 413");
check(contains(f.client->get("/security/denials").body, "UploadValidator"), "side effect: denial recorded");
return finish("security/upload");
}