extract: own-account links judged re-rooted, normalized, for the trusted username only (N2-1 review 2)
- strip-escaping-links.py: the /home[N]/<user>/ exemption ran on the RAW
target (`/home/u/../../etc/shadow` was exempt) and on usernames taken
from the archive's own top-dir names (an archive could name itself
cpmove-root). Now only the username WHP passes (extract.sh <username> =
IMPORT_USERNAME, validated) is trusted; the target must be normalized; and
it is RE-ROOTED at the link's <top>/homedir in the extract and kept only
if that resolves (realpath strict) inside it. No trusted name: nothing
exempt.
- scan-symlinks.php: same trusted-name rule; a non-normalized absolute
target is DANGEROUS outright (never prefix-compared).
- tests: /home/u/../../etc/shadow, /home/u/./x, re-rooted dangling,
re-rooted through an escaping link, another user's /home, an
archive-named user (cpmove-root) via extract.sh, no/invalid trusted name.
8 checks fail on 72b8fa9.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
72b8fa9d08
commit
36d4949d34
4 files changed
+109
-35
No files matched your search
@@ -124,7 +124,6 @@ function relative_symlink_escape(string $archivePath, string $target, array $lin
|
||||
}
|
||||
|
||||
$findings = [];
|
||||
$cpanelUsername = null;
|
||||
|
||||
$cmd = 'tar -tvf ' . escapeshellarg($tarPath) . ' 2>/dev/null';
|
||||
$fh = @popen($cmd, 'r');
|
||||
@@ -170,19 +169,18 @@ foreach ($links as [$archivePath, $target]) {
|
||||
}
|
||||
if ($target === '') continue;
|
||||
|
||||
if ($cpanelUsername === null) {
|
||||
if (preg_match('#^cpmove-([^/]+)/#', $archivePath, $m)) {
|
||||
$cpanelUsername = $m[1];
|
||||
}
|
||||
}
|
||||
|
||||
// (1) user-internal — accept symlinks pointing into the customer's
|
||||
// own /home/<user>/ tree. The panel rewrites these on extract.
|
||||
$userInternal = false;
|
||||
$usernames = [];
|
||||
if ($cpanelUsername !== null && $cpanelUsername !== '') $usernames[] = $cpanelUsername;
|
||||
if ($username !== '') $usernames[] = $username;
|
||||
foreach ($usernames as $u) {
|
||||
// N2-1 review: only the TRUSTED --username (the WHP account WHP passes);
|
||||
// a name read from the archive's top dir (cpmove-<name>) is never trusted.
|
||||
if (preg_match('/\A[a-z_][a-z0-9_-]{0,31}\z/', $username)) $usernames[] = $username;
|
||||
// N2-1 review: only a NORMALIZED target can be user-internal
|
||||
// (`/home/<u>/../../etc/shadow` is not).
|
||||
$normalized = !preg_match('#(^|/)\.{1,2}(/|$)|//#', $target);
|
||||
foreach ($normalized ? $usernames : [] as $u) {
|
||||
$prefix = '/home/' . $u . '/';
|
||||
if (strpos($target, $prefix) === 0 || $target === rtrim($prefix, '/')) {
|
||||
$userInternal = true;
|
||||
@@ -195,6 +193,19 @@ foreach ($links as [$archivePath, $target]) {
|
||||
}
|
||||
if ($userInternal) continue;
|
||||
|
||||
// N2-1 review: a non-normalized absolute target ('.', '..', '//') is
|
||||
// never compared by prefix (`/home/<u>/../../etc` would dodge /etc):
|
||||
// DANGEROUS outright.
|
||||
if (!$normalized) {
|
||||
$findings[] = [
|
||||
'type' => 'DANGEROUS',
|
||||
'archive_path' => $archivePath,
|
||||
'target' => $target,
|
||||
'reason' => "absolute target is not normalized ('.', '..' or empty component)",
|
||||
];
|
||||
continue;
|
||||
}
|
||||
|
||||
// (2) exact root.
|
||||
$type = null;
|
||||
$reason = '';
|
||||
|
||||
Reference in new issue
Block a user