php-src/tests/security/open_basedir.inc
Peter Kokot 1ad08256f3 Sync leading and final newlines in source code files
This patch adds missing newlines, trims multiple redundant final
newlines into a single one, and trims redundant leading newlines.

According to POSIX, a line is a sequence of zero or more non-' <newline>'
characters plus a terminating '<newline>' character. [1] Files should
normally have at least one final newline character.

C89 [2] and later standards [3] mention a final newline:
"A source file that is not empty shall end in a new-line character,
which shall not be immediately preceded by a backslash character."

Although it is not mandatory for all files to have a final newline
fixed, a more consistent and homogeneous approach brings less of commit
differences issues and a better development experience in certain text
editors and IDEs.

[1] http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_206
[2] https://port70.net/~nsz/c/c89/c89-draft.html#2.1.1.2
[3] https://port70.net/~nsz/c/c99/n1256.html#5.1.1.2
2018-10-14 12:56:38 +02:00

139 lines
4.4 KiB
PHP

<?php
// This file contains helper functions for testing open_basedir configuration
// Care must be taken with where the directories are created because different
// SAPIs set the working directory differently. So simply creating a directory
// relative to the current working directory like this: mkdir("blah") might
// actually create it in several different places depending on the SAPI..!
//
// Note also depending on the version of php being tested, so the open_basedir
// configuration may or may not be changeable from a script (PHP_INI_SYSTEM).
//
// For this reason we set the open_basedir to . (current directory) and then
// move around to various directories for testing using chdir(). This is NOT
// recommended for production use as . bypasses all semblence of security..!
//
// Although safe mode has been removed in php 6.0, open_basedir is still valid.
// See http://www.php.net/features.safe-mode for more information
function recursive_delete_directory($directory) {
// Remove any trailing slash first
if (substr($directory, -1) == '/') {
$directory = substr($directory, 0, -1);
}
// Make sure the directory is valid
if (is_dir($directory) == FALSE) {
return FALSE;
}
// Check we can access the directory
if (is_readable($directory) == FALSE) {
return FALSE;
}
$handle = opendir($directory);
// Scan through the directory contents
while (FALSE !== ($item = readdir($handle))) {
if ($item != '.') {
if ($item != '..') {
$path = ($directory.'/'.$item);
if (is_dir($path) == TRUE) {
recursive_delete_directory($path);
} else {
@chmod($path, 0777);
unlink($path);
}
}
}
}
closedir($handle);
@chmod($directory, 0777);
rmdir($directory);
return TRUE;
}
function create_directories() {
delete_directories();
$directory = getcwd();
var_dump(mkdir($directory."/test"));
var_dump(mkdir($directory."/test/ok"));
var_dump(mkdir($directory."/test/bad"));
file_put_contents($directory."/test/ok/ok.txt", "Hello World!");
file_put_contents($directory."/test/bad/bad.txt", "Hello World!");
}
function delete_directories() {
$directory = (getcwd()."/test");
recursive_delete_directory($directory);
}
function test_open_basedir_error($function) {
global $savedDirectory;
var_dump($function("../bad"));
var_dump($function("../bad/bad.txt"));
var_dump($function(".."));
var_dump($function("../"));
var_dump($function("/"));
var_dump($function("../bad/."));
$directory = $savedDirectory;
var_dump($function($directory."/test/bad/bad.txt"));
var_dump($function($directory."/test/bad/../bad/bad.txt"));
}
function test_open_basedir_before($function, $change = TRUE) {
global $savedDirectory;
echo "*** Testing open_basedir configuration [$function] ***\n";
$directory = getcwd();
$savedDirectory = $directory;
var_dump(chdir($directory));
create_directories();
// Optionally change directory
if ($change == TRUE) {
var_dump(chdir($directory."/test/ok"));
}
}
// Delete directories using a --CLEAN-- section!
function test_open_basedir_after($function) {
echo "*** Finished testing open_basedir configuration [$function] ***\n";
}
// This is used by functions that return an array on success
function test_open_basedir_array($function) {
global $savedDirectory;
test_open_basedir_before($function);
test_open_basedir_error($function);
var_dump(is_array($function("./../.")));
var_dump(is_array($function("../ok")));
var_dump(is_array($function("ok.txt")));
var_dump(is_array($function("../ok/ok.txt")));
$directory = $savedDirectory;
var_dump(is_array($function($directory."/test/ok/ok.txt")));
var_dump(is_array($function($directory."/test/ok/../ok/ok.txt")));
test_open_basedir_after($function);
}
function test_open_basedir($function) {
global $savedDirectory;
test_open_basedir_before($function);
test_open_basedir_error($function);
var_dump($function("./../."));
var_dump($function("../ok"));
var_dump($function("ok.txt"));
var_dump($function("../ok/ok.txt"));
$directory = $savedDirectory;
var_dump($function($directory."/test/ok/ok.txt"));
var_dump($function($directory."/test/ok/../ok/ok.txt"));
test_open_basedir_after($function);
}
?>