25 lines
1.1 KiB
C++
25 lines
1.1 KiB
C++
// Route: GET /csp/page (per-request nonce in header and script tag)
|
|||
|
|
#include "support/Assert.hpp"
|
||
|
|
using namespace molo::example::test;
|
||
|
|
static std::string nonce_of(const std::string& h) {
|
||
|
|
std::size_t a = h.find("'nonce-");
|
||
|
|
if (a == std::string::npos) return "";
|
||
|
|
a += 7;
|
||
|
|
return h.substr(a, h.find('\'', a) - a);
|
||
|
|
}
|
||
|
|
int main() {
|
||
|
|
Fixture f;
|
||
|
|
auto a = f.client->get("/csp/page");
|
||
|
|
auto b = f.client->get("/csp/page");
|
||
|
|
check(a.status == 200 && contains(a.content_type(), "text/html"), "html 200");
|
||
|
|
std::string h = a.headers.get("Content-Security-Policy-Report-Only");
|
||
|
|
check(contains(h, "default-src 'none'"), "deny-first default");
|
||
|
|
check(contains(h, "report-uri /csp/report"), "report uri set");
|
||
|
|
std::string n = nonce_of(h);
|
||
|
|
check(n.size() == 32, "nonce is 16 random bytes in hex");
|
||
|
|
check(contains(a.body, "<script nonce=\"" + n + "\">"), "script carries same nonce");
|
||
|
|
check(n != nonce_of(b.headers.get("Content-Security-Policy-Report-Only")), "nonce differs per request");
|
||
|
|
check(f.client->post("/csp/page", "x").status == 405, "POST is 405");
|
||
|
|
return finish("csp/page");
|
||
|
|
}
|