Resolve Markdown paths safely across Linux and Windows
This commit is contained in:
+69
-7
@@ -1,22 +1,84 @@
|
||||
<?php
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
function canonicalPath(string $path): string|false
|
||||
{
|
||||
// Windows can leave another junction in the result of a single realpath().
|
||||
for ($depth = 0; $depth < 32; ++$depth) {
|
||||
$resolved = realpath($path);
|
||||
if ($resolved === false || DIRECTORY_SEPARATOR === '/' || $resolved === $path) {
|
||||
return $resolved;
|
||||
}
|
||||
$path = $resolved;
|
||||
}
|
||||
|
||||
$path = $_GET['file'] ?? $_SERVER['REQUEST_URI'];
|
||||
$file = realpath('d:/www/' . $path);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$file || !str_ends_with($file, '.md')) {
|
||||
function requestedMarkdown(): string|false
|
||||
{
|
||||
$uri = explode('?', $_SERVER['REQUEST_URI'] ?? '', 2)[0];
|
||||
if (preg_match('/%(?![0-9a-f]{2})/i', $uri)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$path = rawurldecode($uri);
|
||||
if (!str_starts_with($path, '/') || !str_ends_with(strtolower($path), '.md')
|
||||
|| str_contains($path, '\\') || preg_match('/[\x00-\x1f\x7f:]/', $path)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (explode('/', substr($path, 1)) as $segment) {
|
||||
if ($segment === '' || $segment === '.' || $segment === '..'
|
||||
|| (DIRECTORY_SEPARATOR === '\\' && rtrim($segment, ' .') !== $segment)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$documentRoot = $_SERVER['DOCUMENT_ROOT'] ?? '';
|
||||
if ($documentRoot === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Recheck symlink targets on every request, including under persistent FastCGI.
|
||||
clearstatcache(true);
|
||||
$root = canonicalPath($documentRoot);
|
||||
if ($root === false || !is_dir($root)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$allowedRoot = $root;
|
||||
$linkPrefix = $_SERVER['MD2HTML_LINK_PREFIX'] ?? '';
|
||||
if ($linkPrefix !== '' && str_starts_with($path, rtrim($linkPrefix, '/') . '/')) {
|
||||
$linkRoot = $_SERVER['MD2HTML_LINK_ROOT'] ?? '';
|
||||
$allowedRoot = $linkRoot === '' ? false : canonicalPath($linkRoot);
|
||||
if ($allowedRoot === false || !is_dir($allowedRoot)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$file = canonicalPath(rtrim($root, DIRECTORY_SEPARATOR) . str_replace('/', DIRECTORY_SEPARATOR, $path));
|
||||
$allowedPrefix = rtrim($allowedRoot, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
|
||||
if ($file === false || !str_starts_with($file, $allowedPrefix)
|
||||
|| !str_ends_with(strtolower($file), '.md') || !is_file($file)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return @file_get_contents($file);
|
||||
}
|
||||
|
||||
$md = requestedMarkdown();
|
||||
if ($md === false) {
|
||||
http_response_code(404);
|
||||
exit('Not found');
|
||||
}
|
||||
|
||||
$md = file_get_contents($file);
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
$parser = new Parsedown();
|
||||
|
||||
echo '<!doctype html>';
|
||||
echo '<meta charset="utf-8">';
|
||||
echo '<meta name="viewport" content="width=device-width, initial-scale=1">';
|
||||
echo '<link rel="stylesheet" href="//localhost/md2html/src/markdown.css?h=' . md5_file(__DIR__ . '/markdown.css') . '">';
|
||||
echo '<script src="//localhost/md2html/src/toc.js?h=' . md5_file(__DIR__ . '/toc.js') . '" defer></script>';
|
||||
echo '<link rel="stylesheet" href="/_md2html/markdown.css?h=' . md5_file(__DIR__ . '/markdown.css') . '">';
|
||||
echo '<script src="/_md2html/toc.js?h=' . md5_file(__DIR__ . '/toc.js') . '" defer></script>';
|
||||
echo '<button class="md2html-toc-toggle" type="button" aria-expanded="false" aria-label="Zobraziť obsah dokumentu" hidden><span aria-hidden="true">☰</span></button>';
|
||||
echo '<div class="md2html-toc-backdrop" aria-hidden="true" hidden></div>';
|
||||
echo '<nav class="md2html-toc" aria-label="Obsah dokumentu" hidden>';
|
||||
|
||||
Reference in New Issue
Block a user