Skip to content

Commit

Permalink
Move out escaping function to central place
Browse files Browse the repository at this point in the history
Alexander Obuhovich committed Mar 4, 2016

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 232919c commit 7b3315a
Showing 9 changed files with 47 additions and 20 deletions.
2 changes: 1 addition & 1 deletion driver-testsuite/tests/Form/GeneralTest.php
Original file line number Diff line number Diff line change
@@ -207,7 +207,7 @@ public function testAdvancedForm()
array (
'agreement' = 'on',
'email' = '[email protected]',
'first_name' = 'Foo "item"',
'first_name' = 'Foo "item"',
'last_name' = 'Bar',
'notes' = 'new notes',
'select_number' = '30',
11 changes: 5 additions & 6 deletions driver-testsuite/web-fixtures/advanced_form_post.php
Original file line number Diff line number Diff line change
@@ -8,19 +8,18 @@
<?php
error_reporting(0);

require_once 'utils.php';

if (isset($_POST['select_multiple_numbers']) && false !== strpos($_POST['select_multiple_numbers'][0], ',')) {
$_POST['select_multiple_numbers'] = explode(',', $_POST['select_multiple_numbers'][0]);
}

$_POST['agreement'] = isset($_POST['agreement']) ? 'on' : 'off';
ksort($_POST);
foreach ($_POST as $key => $value) {
$post_for_printing[htmlspecialchars($key, ENT_QUOTES, 'UTF-8')] = htmlspecialchars(var_export($value, TRUE), ENT_QUOTES, 'UTF-8');
}
echo str_replace('>', '', var_export($post_for_printing, true)) . "\n";
echo str_replace('>', '', var_export(html_escape_value($_POST), true)) . "\n";
if (isset($_FILES['about']) && file_exists($_FILES['about']['tmp_name'])) {
echo htmlspecialchars($_FILES['about']['name'], ENT_QUOTES, 'UTF-8') . "\n";
echo htmlspecialchars(file_get_contents($_FILES['about']['tmp_name'], ENT_QUOTES, 'UTF-8'));
echo html_escape_value($_FILES['about']['name']) . "\n";
echo html_escape_value(file_get_contents($_FILES['about']['tmp_name']));
} else {
echo "no file";
}
10 changes: 7 additions & 3 deletions driver-testsuite/web-fixtures/basic_form_post.php
Original file line number Diff line number Diff line change
@@ -5,8 +5,12 @@
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
<h1>Anket for <?php echo htmlspecialchars($_POST['first_name'], ENT_QUOTES, 'UTF-8') ?></h1>
<span id="first">Firstname: <?php echo htmlspecialchars($_POST['first_name'], ENT_QUOTES, 'UTF-8') ?></span>
<span id="last">Lastname: <?php echo htmlspecialchars($_POST['last_name'], ENT_QUOTES, 'UTF-8') ?></span>
<?php
require_once 'utils.php';
?>
<h1>Anket for <?php echo html_escape_value($_POST['first_name']) ?></h1>

<span id="first">Firstname: <?php echo html_escape_value($_POST['first_name']) ?></span>
<span id="last">Lastname: <?php echo html_escape_value($_POST['last_name']) ?></span>
</body>
</html>
5 changes: 4 additions & 1 deletion driver-testsuite/web-fixtures/basic_get_form.php
Original file line number Diff line number Diff line change
@@ -8,7 +8,10 @@
<h1>Basic Get Form Page</h1>

<div id="serach">
<?php echo isset($_GET['q']) && $_GET['q'] ? htmlspecialchars($_GET['q'], ENT_QUOTES, 'UTF-8') : 'No search query' ?>
<?php
require_once 'utils.php';
echo isset($_GET['q']) && $_GET['q'] ? html_escape_value($_GET['q']) : 'No search query';
?>
</div>

<form>
5 changes: 4 additions & 1 deletion driver-testsuite/web-fixtures/cookie_page2.php
Original file line number Diff line number Diff line change
@@ -5,6 +5,9 @@
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
Previous cookie: <?php echo isset($_COOKIE['srvr_cookie']) ? htmlspecialchars($_COOKIE['srvr_cookie'], ENT_QUOTES, 'UTF-8') : 'NO'; ?>
Previous cookie: <?php
require_once 'utils.php';
echo isset($_COOKIE['srvr_cookie']) ? html_escape_value($_COOKIE['srvr_cookie']) : 'NO';
?>
</body>
</html>
4 changes: 3 additions & 1 deletion driver-testsuite/web-fixtures/issue130.php
Original file line number Diff line number Diff line change
@@ -2,10 +2,12 @@
<html>
<body>
<?php
require_once 'utils.php';

if ('1' === $_GET['p']) {
echo '<a href="issue130.php?p=2">Go to 2</a>';
} else {
echo '<strong>'.htmlspecialchars($_SERVER['HTTP_REFERER'], ENT_QUOTES, 'UTF-8').'</strong>';
echo '<strong>'.html_escape_value($_SERVER['HTTP_REFERER']).'</strong>';
}
?>
</body>
4 changes: 3 additions & 1 deletion driver-testsuite/web-fixtures/issue140.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php
require_once 'utils.php';

if (!empty($_POST)) {
setcookie("tc", $_POST['cookie_value'], null, '/');
} elseif (isset($_GET["show_value"])) {
echo htmlspecialchars($_COOKIE["tc"], ENT_QUOTES, 'UTF-8');
echo html_escape_value($_COOKIE["tc"]);
die();
}
?>
10 changes: 4 additions & 6 deletions driver-testsuite/web-fixtures/print_cookies.php
Original file line number Diff line number Diff line change
@@ -5,11 +5,9 @@
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
<?php
foreach ($_COOKIE as $key => $value) {
$cookie_for_printing[htmlspecialchars($key, ENT_QUOTES, 'UTF-8')] = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}
?>
<?php echo str_replace('>', '', var_export($cookie_for_printing, true)); ?>
<?php
require_once 'utils.php';
echo str_replace('>', '', var_export(html_escape_value($_COOKIE), true));
?>
</body>
</html>
16 changes: 16 additions & 0 deletions driver-testsuite/web-fixtures/utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

function html_escape_value($data)
{
if (!is_array($data)) {
return htmlspecialchars($data, ENT_QUOTES, 'UTF-8', false);
}

$escapedData = array();

foreach ($data as $key => $value) {
$escapedData[html_escape_value($key)] = html_escape_value($value);
}

return $escapedData;
}

0 comments on commit 7b3315a

Please sign in to comment.