<?php
require_once("secrets.php");
$nonce = random_bytes(8);

if(isset(
$_GET['flag'])){
 if(
isAdmin()){
    
header('X-Content-Type-Options: nosniff');
    
header('X-Frame-Options: DENY');
    
header('Content-type: text/html; charset=UTF-8');
    echo 
$flag;
    die();
 }
 else{
     echo 
"You are not an admin!";
     die();
 }
}

for(
$i=0; $i<10; $i++){
    if(isset(
$_GET['digest'])){
        
$_nonce = hash($_GET['digest'], $nonce);
        if(
$_nonce){
            
$nonce = $_nonce;
            continue;
        }
    }
    
$nonce = md5($nonce);
}

// The report bot can review a direct proof-of-concept URL. Allow a practical
// reflected-XSS payload while retaining the digest/header-buffering weakness.
if(isset($_GET['username']) && strlen($_GET['username']) <= 256) {
    
// A large digest causes output before headers; model that condition
    // explicitly so the reflected proof-of-concept is executable.
    
if (!isset($_GET['digest']) || strlen($_GET['digest']) < 100) {
        
header("content-security-policy: default-src 'none'; style-src 'nonce-$nonce'; script-src 'nonce-$nonce'");
    }
    echo <<<EOT
        <script nonce='$nonce'>
            setInterval(
                ()=>greeting.style.color=Math.random()<0.3?'crimson':'navy'
            ,100);
        </script>
        <center><h1> Welcome, <span id='greeting'>
{$_GET['username']}</span>!!</h1>
        <p>Click <a href="?flag">here</a> to retrieve your reward!</p>
EOT;
}else{
    
show_source(__FILE__);
}

// Spotted something odd? Report it at /report.php
// Deployment info: /Dockerfile