2013-01-31 4 views
1

오늘 나는 Content-Security-Policy (CSP)을 구현했습니다. 또한 report-uri을 포함하여 POST 요청을 myserver.com/csp-report.php으로 보냅니다. MDN은 자신의 웹 사이트에 설명 된 바와 같이, POST 요청은 다음과 같이이다 : 나는 [email protected]이 정보를 이메일로 원하는JSON Post in PHP (CSP-Report)

{ 
    "csp-report": { 
    "document-uri": "http://example.com/signup.html", 
    "referrer": "http://evil.example.net/haxor.html", 
    "blocked-uri": "http://evil.example.net/injected.png", 
    "violated-directive": "img-src *.example.com", 
    "original-policy": "default-src 'self'; img-src 'self' *.example.com; report-uri /_/csp-reports", 
    } 
} 

. 현재,이 코드를 가지고 있지만 그것은 단지 이메일의 "어레이() 배열()"대신`isset`의`isSet`를 입력했기 때문에

<?php 
$tars = Array("[email protected]", "[email protected]"); 
$from = "[email protected]"; 
$subject = "CSP Report"; 
$text = print_r($_POST, true); 
$text = (isSet($_GET["text"]) ? $_GET["text"] : $text); 
foreach($tars as $tar){ 
    $e = mail($tar,$subject,$text,"From: $from"); 
} 
if($e){ 
    header("Content-type: text/javascript"); 
    echo 'console.log("Email Sent");'; 
    exit(); 
} 
?> 
+0

어쩌면이다 –

답변

3
<?php 
# 
# Set vars for mail sender and recipient 
$sender = $_SERVER['SERVER_ADMIN']; 
$recipient = $_SERVER['SERVER_ADMIN']; 
$subject = $_SERVER['SERVER_NAME'] . ' CSP Report'; 
$smtp_headers = 'From: ' . $_SERVER['SERVER_ADMIN'] . "\r\n" . 
    'Reply-To: ' . $_SERVER['SERVER_ADMIN'] . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 
# 
# Get the report content 
$json = file_get_contents('php://input'); 
if ($json === false) { 
    throw new Exception('Bad Request'); 
} 
$message = 'The user agent "' . $_SERVER['HTTP_USER_AGENT'] . '" ' 
      . 'from ' . $_SERVER['REMOTE_HOST'] . ' ' 
      . '(IP ' . $_SERVER['REMOTE_ADDR'] . ') ' 
      . 'reported the following content security policy (CSP) violation:' . "\n\n"; 
$csp = json_decode($json, true); 
if (is_null($csp)) { 
    throw new Exception('Bad JSON Violation'); 
} 

# Parse 
foreach ($csp['csp-report'] as $key => $value) { 
    $message .= ' ' . $key . ": " . $value ."\n"; 
} 

# 
# Send the report 
$reported = mail($recipient, $subject, $message, $smtp_headers); 

# 
# Log in client? 
if ($reported) { 
    header("Content-type: text/javascript"); 
    echo 'console.log("Email Sent");'; 
    exit(); 
} 
?>