Core\IO::within
Core\IO::within(string $base, string $path): stringCore\IO::within( string $base, string $path, ): string
Checks that a file name stays inside one folder, and returns its full path.
Core\IO::within($base, $path) joins $path to the folder $base and resolves the result. It follows every .. and every symbolic link. If the final path is inside $base, the method returns it. If it is outside, the method throws a RuntimeError. The file itself does not need to exist yet, so you can check a name before you create the file.
Use it for a file name that comes from a form or a URL. A name like ../../config.toml then cannot reach a file outside the folder. The returned path is safe to pass to Core\IO::readText, Core\IO::write and the other file methods.
within throws an IOError when $base does not exist.
A program you start from the command line may read any path. On a server, a program may read only the files in its application folder, except nvs.toml and names that start with .. Any other path needs the fs.read capability. This holds for $base and for the joined path.
In plain words: it checks that a name stays inside one folder. A name that leads out of the folder throws an error.
Parameters
$basestringThe directory the result must stay inside. It must exist, because the check uses its canonical path.$pathstringThe path to resolve against$base. This is the untrusted part, and the reason to use this method. A relative$pathis joined to$base, also when it is written directly in the code. It is not joined to the folder of the file that callswithin. An absolute path is resolved too, and then the same check makes it fail.
Return value
Returns string. The resolved absolute path, as a plain string. It has no .., no symbolic links and no repeated separators. It is the path the operating system itself uses for the file.
Errors
RuntimeErrorThe resolved path is not inside$base. The message names the base and the argument, but never the place the argument led to. So the error shows nothing about the target of a symbolic link. If the configuration does not grantfs.readfor either path, the same error class is thrown earlier.IOErrorThe capability allowed the call, but nothing could be resolved.$basedoes not exist or is not a directory, or no parent directory of the joined path exists.
Examples
Open a file a user named
<?nvs// Shows how `Core\IO::within` checks that a file name from a user stays inside one folder.
string $folder = Core\IO::createTemporaryDirectory();Core\IO::write(Core\Path::join($folder, 'readme.txt'), "Welcome!\n");
// "readme.txt" is inside the folder. `within` returns its full path, so the program can read it.// This prints "Welcome!".string $path = Core\IO::within($folder, 'readme.txt');echo Core\IO::readText($path);
// "../secret.txt" points to the folder above. `within` throws a `RuntimeError`.// This prints "refused: ../secret.txt".try { Core\IO::within($folder, '../secret.txt'); echo "allowed: ../secret.txt\n";} catch (RuntimeError $error) { echo "refused: ../secret.txt\n";}Welcome!refused: ../secret.txtWhich names stay inside
<?nvs// Shows which file names `Core\IO::within` allows, and which ones it refuses.
string $folder = Core\IO::createTemporaryDirectory();Core\IO::makeDirectory(Core\Path::join($folder, 'photos'));
// A name may use `..` if the result is still inside the folder. A file that does not exist yet is allowed.// A name that ends outside the folder is refused. An absolute path to another place is refused too.array<string> $names = [ 'photos/cat.jpg', 'photos/../notes.txt', 'new-file.txt', '../other-folder/file.txt', 'photos/../../file.txt', '/etc/passwd',];
// This prints "allowed" for the first three names and "refused" for the last three.foreach ($names as string $name) { try { Core\IO::within($folder, $name); echo 'allowed: ', $name, "\n"; } catch (RuntimeError $error) { echo 'refused: ', $name, "\n"; }}allowed: photos/cat.jpgallowed: photos/../notes.txtallowed: new-file.txtrefused: ../other-folder/file.txtrefused: photos/../../file.txtrefused: /etc/passwdSend a file from an uploads folder
<?nvs// Shows how a program sends a file that a visitor asked for by name, and only from its uploads folder.
class Downloads{ // Returns the content of the file, or a message when the name is not allowed. public static function send(string $uploads, string $requested): string { try { string $path = Core\IO::within($uploads, $requested); return Core\IO::readText($path); } catch (RuntimeError $error) { return "403 Forbidden\n"; } }}
string $uploads = Core\IO::createTemporaryDirectory();Core\IO::write(Core\Path::join($uploads, 'invoice-17.txt'), "Invoice 17: 49.90 EUR\n");
// The first name is a file in the uploads folder. This prints "Invoice 17: 49.90 EUR".echo Downloads::send($uploads, 'invoice-17.txt');
// The second name tries to leave the uploads folder. This prints "403 Forbidden".echo Downloads::send($uploads, '../../config/database.toml');Invoice 17: 49.90 EUR403 Forbidden