mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Merge pull request #12411 from appwrite/fix-host-validation
fix(avatars): block redirect
This commit is contained in:
16 files changed
+1116
-88
No files matched your search
@@ -3932,6 +3932,8 @@ Http::put('/v1/account/recovery')
|
||||
|
||||
$hooks->trigger('passwordValidator', [$dbForProject, $project, $password, &$user, true]);
|
||||
|
||||
$sessions = $profile->getAttribute('sessions', []);
|
||||
|
||||
$profile = $dbForProject->updateDocument('users', $profile->getId(), new Document(
|
||||
[
|
||||
'password' => $newPassword,
|
||||
@@ -3944,6 +3946,14 @@ Http::put('/v1/account/recovery')
|
||||
|
||||
$user->setAttributes($profile->getArrayCopy());
|
||||
|
||||
$invalidate = $project->getAttribute('auths', default: [])['invalidateSessions'] ?? false;
|
||||
if ($invalidate) {
|
||||
foreach ($sessions as $session) {
|
||||
/** @var Document $session */
|
||||
$dbForProject->deleteDocument('sessions', $session->getId());
|
||||
}
|
||||
}
|
||||
|
||||
$recoveryDocument = $dbForProject->getDocument('tokens', $verifiedToken->getId());
|
||||
|
||||
/**
|
||||
|
||||
@@ -591,7 +591,7 @@ Http::post('/v1/users/:userId/targets')
|
||||
->label('audits.event', 'target.create')
|
||||
->label('audits.resource', 'target/response.$id')
|
||||
->label('event', 'users.[userId].targets.[targetId].create')
|
||||
->label('scope', 'targets.write')
|
||||
->label('scope', 'users.write')
|
||||
->label('sdk', new Method(
|
||||
namespace: 'users',
|
||||
group: 'targets',
|
||||
@@ -852,7 +852,7 @@ Http::get('/v1/users/:userId/prefs')
|
||||
Http::get('/v1/users/:userId/targets/:targetId')
|
||||
->desc('Get user target')
|
||||
->groups(['api', 'users'])
|
||||
->label('scope', 'targets.read')
|
||||
->label('scope', 'users.read')
|
||||
->label('sdk', new Method(
|
||||
namespace: 'users',
|
||||
group: 'targets',
|
||||
@@ -1078,7 +1078,7 @@ Http::get('/v1/users/:userId/logs')
|
||||
Http::get('/v1/users/:userId/targets')
|
||||
->desc('List user targets')
|
||||
->groups(['api', 'users'])
|
||||
->label('scope', 'targets.read')
|
||||
->label('scope', 'users.read')
|
||||
->label('sdk', new Method(
|
||||
namespace: 'users',
|
||||
group: 'targets',
|
||||
@@ -1843,7 +1843,7 @@ Http::patch('/v1/users/:userId/targets/:targetId')
|
||||
->label('audits.event', 'target.update')
|
||||
->label('audits.resource', 'target/{response.$id}')
|
||||
->label('event', 'users.[userId].targets.[targetId].update')
|
||||
->label('scope', 'targets.write')
|
||||
->label('scope', 'users.write')
|
||||
->label('sdk', new Method(
|
||||
namespace: 'users',
|
||||
group: 'targets',
|
||||
@@ -2661,7 +2661,7 @@ Http::delete('/v1/users/:userId/targets/:targetId')
|
||||
->label('audits.event', 'target.delete')
|
||||
->label('audits.resource', 'target/{request.$targetId}')
|
||||
->label('event', 'users.[userId].targets.[targetId].delete')
|
||||
->label('scope', 'targets.write')
|
||||
->label('scope', 'users.write')
|
||||
->label('sdk', new Method(
|
||||
namespace: 'users',
|
||||
group: 'targets',
|
||||
|
||||
+40
-32
@@ -657,50 +657,58 @@ $server->onWorkerStart(function (int $workerId) use ($server, $register, $stats,
|
||||
$userId = $event['userId'];
|
||||
|
||||
if ($realtime->hasSubscriber($projectId, 'user:' . $userId)) {
|
||||
$connection = array_key_first(reset($realtime->subscriptions[$projectId]['user:' . $userId]));
|
||||
$subscriptionsBefore = \count($realtime->getSubscriptionMetadata($connection));
|
||||
$connections = [];
|
||||
foreach ($realtime->subscriptions[$projectId]['user:' . $userId] as $byConnection) {
|
||||
foreach (\array_keys($byConnection) as $connectionId) {
|
||||
$connections[$connectionId] = true;
|
||||
}
|
||||
}
|
||||
|
||||
$consoleDatabase = getConsoleDB();
|
||||
$project = $consoleDatabase->getAuthorization()->skip(fn () => $consoleDatabase->getDocument('projects', $projectId));
|
||||
$database = getProjectDB($project);
|
||||
|
||||
/** @var User $user */
|
||||
$user = $database->getDocument('users', $userId);
|
||||
|
||||
$roles = $user->getRoles($database->getAuthorization());
|
||||
$authorization = $realtime->connections[$connection]['authorization'] ?? null;
|
||||
$previousUserId = $realtime->connections[$connection]['userId'] ?? '';
|
||||
|
||||
$meta = $realtime->getSubscriptionMetadata($connection);
|
||||
foreach (\array_keys($connections) as $connection) {
|
||||
$subscriptionsBefore = \count($realtime->getSubscriptionMetadata($connection));
|
||||
$authorization = $realtime->connections[$connection]['authorization'] ?? null;
|
||||
$previousUserId = $realtime->connections[$connection]['userId'] ?? '';
|
||||
|
||||
$realtime->unsubscribe($connection);
|
||||
$meta = $realtime->getSubscriptionMetadata($connection);
|
||||
|
||||
foreach ($meta as $subscriptionId => $subscription) {
|
||||
$queries = Query::parseQueries($subscription['queries'] ?? []);
|
||||
$channels = Realtime::rebindAccountChannels(
|
||||
$subscription['channels'] ?? [],
|
||||
$previousUserId,
|
||||
$userId
|
||||
);
|
||||
$realtime->subscribe(
|
||||
$projectId,
|
||||
$connection,
|
||||
$subscriptionId,
|
||||
$roles,
|
||||
$channels,
|
||||
$queries,
|
||||
$userId
|
||||
);
|
||||
}
|
||||
$realtime->unsubscribe($connection);
|
||||
|
||||
// Restore authorization after subscribe
|
||||
if ($authorization !== null) {
|
||||
$realtime->connections[$connection]['authorization'] = $authorization;
|
||||
}
|
||||
foreach ($meta as $subscriptionId => $subscription) {
|
||||
$queries = Query::parseQueries($subscription['queries'] ?? []);
|
||||
$channels = Realtime::rebindAccountChannels(
|
||||
$subscription['channels'] ?? [],
|
||||
$previousUserId,
|
||||
$userId
|
||||
);
|
||||
$realtime->subscribe(
|
||||
$projectId,
|
||||
$connection,
|
||||
$subscriptionId,
|
||||
$roles,
|
||||
$channels,
|
||||
$queries,
|
||||
$userId
|
||||
);
|
||||
}
|
||||
|
||||
$subscriptionsAfter = \count($realtime->getSubscriptionMetadata($connection));
|
||||
$subscriptionDelta = $subscriptionsAfter - $subscriptionsBefore;
|
||||
if ($subscriptionDelta !== 0) {
|
||||
$register->get('telemetry.workerSubscriptionCounter')->add($subscriptionDelta, $register->get('telemetry.workerAttributes'));
|
||||
// Restore authorization after subscribe
|
||||
if ($authorization !== null) {
|
||||
$realtime->connections[$connection]['authorization'] = $authorization;
|
||||
}
|
||||
|
||||
$subscriptionsAfter = \count($realtime->getSubscriptionMetadata($connection));
|
||||
$subscriptionDelta = $subscriptionsAfter - $subscriptionsBefore;
|
||||
if ($subscriptionDelta !== 0) {
|
||||
$register->get('telemetry.workerSubscriptionCounter')->add($subscriptionDelta, $register->get('telemetry.workerAttributes'));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,198 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Network\Validator;
|
||||
|
||||
use Utopia\Validator;
|
||||
|
||||
/**
|
||||
* Validates that a hostname (or IP literal) is publicly routable.
|
||||
*
|
||||
* Resolves A and AAAA records and rejects the value if any resolved address —
|
||||
* or the literal itself, when given an IP — falls inside a private, loopback,
|
||||
* link-local, multicast, or otherwise reserved range. Used to block SSRF on
|
||||
* endpoints that fetch user-controlled URLs.
|
||||
*
|
||||
* Distinct from Utopia\Validator\Hostname, which only checks string format
|
||||
* and an optional allow-list and does not touch DNS.
|
||||
*
|
||||
* Known limitation: there is a TOCTOU window between this DNS lookup and the
|
||||
* subsequent HTTP fetch. To fully prevent DNS rebinding the caller must pin
|
||||
* curl to a verified IP via CURLOPT_RESOLVE.
|
||||
*/
|
||||
class PublicHostname extends Validator
|
||||
{
|
||||
/**
|
||||
* IPv4 ranges PHP's FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
|
||||
* does not cover, but which still must not be reachable from a public
|
||||
* fetch endpoint.
|
||||
*/
|
||||
private const PRIVATE_IPV4_CIDRS = [
|
||||
'100.64.0.0/10', // CGNAT (RFC 6598)
|
||||
'192.0.0.0/24', // IETF protocol assignments
|
||||
'192.0.2.0/24', // TEST-NET-1
|
||||
'198.18.0.0/15', // Benchmarking
|
||||
'198.51.100.0/24', // TEST-NET-2
|
||||
'203.0.113.0/24', // TEST-NET-3
|
||||
'224.0.0.0/4', // Multicast
|
||||
'255.255.255.255/32', // Broadcast
|
||||
];
|
||||
|
||||
/**
|
||||
* IPv6 ranges PHP's filter flags miss — most importantly the
|
||||
* IPv4-mapped, IPv4-translated, and 6to4 ranges that can be used to
|
||||
* smuggle private IPv4 destinations past an IPv6-only check.
|
||||
*/
|
||||
private const PRIVATE_IPV6_CIDRS = [
|
||||
'::/128', // Unspecified
|
||||
'::ffff:0:0/96', // IPv4-mapped (e.g. ::ffff:127.0.0.1)
|
||||
'64:ff9b::/96', // IPv4/IPv6 translation
|
||||
'64:ff9b:1::/48', // Local-use IPv4/IPv6 translation
|
||||
'100::/64', // Discard
|
||||
'2001::/32', // Teredo
|
||||
'2001:db8::/32', // Documentation
|
||||
'2002::/16', // 6to4 (covers 2002:7f00::/24 → 127.0.0.0/8 etc.)
|
||||
'ff00::/8', // Multicast
|
||||
];
|
||||
|
||||
private string $reason = '';
|
||||
|
||||
public function getDescription(): string
|
||||
{
|
||||
return $this->reason !== ''
|
||||
? $this->reason
|
||||
: 'Value must be a publicly routable hostname or address.';
|
||||
}
|
||||
|
||||
public function isArray(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getType(): string
|
||||
{
|
||||
return self::TYPE_STRING;
|
||||
}
|
||||
|
||||
public function isValid(mixed $value): bool
|
||||
{
|
||||
$this->reason = '';
|
||||
|
||||
if (!\is_string($value) || $value === '') {
|
||||
$this->reason = 'Hostname is empty.';
|
||||
return false;
|
||||
}
|
||||
|
||||
$hostname = \strtolower(\trim($value, " \t\n\r\0\x0B[]"));
|
||||
|
||||
// IP literals are checked directly, no DNS round-trip.
|
||||
if (\filter_var($hostname, FILTER_VALIDATE_IP) !== false) {
|
||||
if (!self::isPublicIp($hostname)) {
|
||||
$this->reason = "Address {$hostname} is in a private or reserved range.";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
$addresses = self::resolve($hostname);
|
||||
|
||||
if (empty($addresses)) {
|
||||
$this->reason = "Hostname {$hostname} does not resolve.";
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($addresses as $ip) {
|
||||
if (!self::isPublicIp($ip)) {
|
||||
$this->reason = "Hostname {$hostname} resolves to private or reserved address {$ip}.";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a hostname to all A and AAAA records.
|
||||
*
|
||||
* @return array<string>
|
||||
*/
|
||||
public static function resolve(string $hostname): array
|
||||
{
|
||||
$ipv4 = @\gethostbynamel($hostname) ?: [];
|
||||
|
||||
$ipv6 = [];
|
||||
$records = @\dns_get_record($hostname, DNS_AAAA);
|
||||
if (\is_array($records)) {
|
||||
foreach ($records as $record) {
|
||||
if (!empty($record['ipv6'])) {
|
||||
$ipv6[] = $record['ipv6'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return \array_values(\array_unique(\array_merge($ipv4, $ipv6)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true only if the given IP literal sits in a globally routable range.
|
||||
*/
|
||||
public static function isPublicIp(string $ip): bool
|
||||
{
|
||||
$filtered = \filter_var(
|
||||
$ip,
|
||||
FILTER_VALIDATE_IP,
|
||||
FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
|
||||
);
|
||||
|
||||
if ($filtered === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$isIpv4 = \filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false;
|
||||
$extra = $isIpv4 ? self::PRIVATE_IPV4_CIDRS : self::PRIVATE_IPV6_CIDRS;
|
||||
|
||||
foreach ($extra as $cidr) {
|
||||
if (self::ipInCidr($ip, $cidr)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Binary CIDR match. Works for both IPv4 and IPv6 as long as the
|
||||
* IP and subnet are the same family.
|
||||
*/
|
||||
private static function ipInCidr(string $ip, string $cidr): bool
|
||||
{
|
||||
[$subnet, $maskBits] = \explode('/', $cidr, 2);
|
||||
$maskBits = (int) $maskBits;
|
||||
|
||||
$ipBinary = @\inet_pton($ip);
|
||||
$subnetBinary = @\inet_pton($subnet);
|
||||
|
||||
if ($ipBinary === false || $subnetBinary === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (\strlen($ipBinary) !== \strlen($subnetBinary)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$fullBytes = \intdiv($maskBits, 8);
|
||||
$remainderBits = $maskBits % 8;
|
||||
|
||||
if ($fullBytes > 0 && \substr($ipBinary, 0, $fullBytes) !== \substr($subnetBinary, 0, $fullBytes)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($remainderBits > 0) {
|
||||
$mask = \chr(0xFF << (8 - $remainderBits) & 0xFF);
|
||||
if ((\ord($ipBinary[$fullBytes]) & \ord($mask)) !== (\ord($subnetBinary[$fullBytes]) & \ord($mask))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace Appwrite\Platform\Modules\Avatars\Http\Favicon;
|
||||
|
||||
use Appwrite\Extend\Exception;
|
||||
use Appwrite\Network\Validator\PublicHostname;
|
||||
use Appwrite\Platform\Modules\Avatars\Http\Action;
|
||||
use Appwrite\SDK\AuthType;
|
||||
use Appwrite\SDK\ContentType;
|
||||
@@ -15,7 +16,9 @@ use DOMDocument;
|
||||
use DOMElement;
|
||||
use enshrined\svgSanitize\Sanitizer as SvgSanitizer;
|
||||
use Utopia\Domains\Domain;
|
||||
use Utopia\Fetch\Adapter;
|
||||
use Utopia\Fetch\Client;
|
||||
use Utopia\Fetch\Response as FetchResponse;
|
||||
use Utopia\Image\Image;
|
||||
use Utopia\Platform\Action as UtopiaAction;
|
||||
use Utopia\Platform\Scope\HTTP;
|
||||
@@ -26,6 +29,9 @@ class Get extends Action
|
||||
{
|
||||
use HTTP;
|
||||
|
||||
private const ALLOWED_SCHEMES = ['http', 'https'];
|
||||
private const MAX_REDIRECTS = 5;
|
||||
|
||||
public static function getName(): string
|
||||
{
|
||||
return 'getFavicon';
|
||||
@@ -56,7 +62,7 @@ class Get extends Action
|
||||
],
|
||||
contentType: ContentType::IMAGE
|
||||
))
|
||||
->param('url', '', new URL(['http', 'https']), 'Website URL which you want to fetch the favicon from.')
|
||||
->param('url', '', new URL(self::ALLOWED_SCHEMES), 'Website URL which you want to fetch the favicon from.')
|
||||
->inject('response')
|
||||
->callback($this->action(...));
|
||||
}
|
||||
@@ -67,34 +73,25 @@ class Get extends Action
|
||||
$height = 56;
|
||||
$quality = 80;
|
||||
$output = 'png';
|
||||
$type = 'png';
|
||||
|
||||
if (!\extension_loaded('imagick')) {
|
||||
throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Imagick extension is missing');
|
||||
}
|
||||
|
||||
$domain = new Domain(\parse_url($url, PHP_URL_HOST));
|
||||
$userAgent = \sprintf(
|
||||
APP_USERAGENT,
|
||||
System::getEnv('_APP_VERSION', 'UNKNOWN'),
|
||||
System::getEnv('_APP_EMAIL_SECURITY', System::getEnv('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS', APP_EMAIL_SECURITY))
|
||||
);
|
||||
|
||||
if (!$domain->isKnown()) {
|
||||
throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED);
|
||||
}
|
||||
|
||||
$client = new Client();
|
||||
try {
|
||||
$res = $client
|
||||
->setAllowRedirects(true)
|
||||
->setMaxRedirects(5)
|
||||
->setUserAgent(\sprintf(
|
||||
APP_USERAGENT,
|
||||
System::getEnv('_APP_VERSION', 'UNKNOWN'),
|
||||
System::getEnv('_APP_EMAIL_SECURITY', System::getEnv('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS', APP_EMAIL_SECURITY))
|
||||
))
|
||||
->fetch($url);
|
||||
$pageResponse = $this->safeFetch($url, $userAgent);
|
||||
} catch (\Throwable) {
|
||||
throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED);
|
||||
}
|
||||
|
||||
$body = $res->getBody();
|
||||
$body = $pageResponse->getBody();
|
||||
|
||||
$doc = new DOMDocument();
|
||||
$doc->strictErrorChecking = false;
|
||||
if (!empty($body)) {
|
||||
@@ -110,12 +107,11 @@ class Get extends Action
|
||||
$href = $link->getAttribute('href');
|
||||
$rel = $link->getAttribute('rel');
|
||||
$sizes = $link->getAttribute('sizes');
|
||||
$absolute = URLParse::unparse(\array_merge(\parse_url($url), \parse_url($href)));
|
||||
$absolute = URLParse::resolveLocation($url, $href);
|
||||
|
||||
switch (\strtolower($rel)) {
|
||||
case 'icon':
|
||||
case 'shortcut icon':
|
||||
//case 'apple-touch-icon':
|
||||
$ext = \pathinfo(\parse_url($absolute, PHP_URL_PATH), PATHINFO_EXTENSION);
|
||||
|
||||
switch ($ext) {
|
||||
@@ -154,27 +150,17 @@ class Get extends Action
|
||||
$outputExt = 'ico';
|
||||
}
|
||||
|
||||
$domain = new Domain(\parse_url($outputHref, PHP_URL_HOST));
|
||||
|
||||
if (!$domain->isKnown()) {
|
||||
throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED);
|
||||
}
|
||||
|
||||
$client = new Client();
|
||||
try {
|
||||
$res = $client
|
||||
->setAllowRedirects(true)
|
||||
->setMaxRedirects(5)
|
||||
->fetch($outputHref);
|
||||
$iconResponse = $this->safeFetch($outputHref, $userAgent);
|
||||
} catch (\Throwable) {
|
||||
throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED);
|
||||
}
|
||||
|
||||
if ($res->getStatusCode() !== 200) {
|
||||
if ($iconResponse->getStatusCode() !== 200) {
|
||||
throw new Exception(Exception::AVATAR_ICON_NOT_FOUND);
|
||||
}
|
||||
|
||||
$data = $res->getBody();
|
||||
$data = $iconResponse->getBody();
|
||||
|
||||
if ('ico' === $outputExt) { // Skip crop, Imagick isn\'t supporting icon files
|
||||
if (
|
||||
@@ -215,4 +201,74 @@ class Get extends Action
|
||||
->file($data);
|
||||
unset($image);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
protected static function assertSafeUrl(string $url): void
|
||||
{
|
||||
$parts = \parse_url($url);
|
||||
if (!\is_array($parts)) {
|
||||
throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED, 'Malformed URL.');
|
||||
}
|
||||
|
||||
$scheme = \strtolower($parts['scheme'] ?? '');
|
||||
if (!\in_array($scheme, self::ALLOWED_SCHEMES, true)) {
|
||||
throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED, "Scheme '{$scheme}' is not allowed.");
|
||||
}
|
||||
|
||||
$host = $parts['host'] ?? '';
|
||||
if ($host === '') {
|
||||
throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED, 'URL has no host.');
|
||||
}
|
||||
|
||||
$isIpLiteral = \filter_var(\trim($host, '[]'), FILTER_VALIDATE_IP) !== false;
|
||||
if (!$isIpLiteral) {
|
||||
try {
|
||||
$domain = new Domain($host);
|
||||
} catch (\Throwable) {
|
||||
throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED, "Hostname '{$host}' is invalid.");
|
||||
}
|
||||
|
||||
if (!$domain->isKnown()) {
|
||||
throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED, "Hostname '{$host}' is not a known public domain.");
|
||||
}
|
||||
}
|
||||
|
||||
$validator = new PublicHostname();
|
||||
if (!$validator->isValid($host)) {
|
||||
throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED, $validator->getDescription());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function safeFetch(string $url, string $userAgent, ?Adapter $adapter = null): FetchResponse
|
||||
{
|
||||
for ($hop = 0; $hop <= self::MAX_REDIRECTS; $hop++) {
|
||||
self::assertSafeUrl($url);
|
||||
|
||||
$client = $adapter !== null ? new Client($adapter) : new Client();
|
||||
$response = $client
|
||||
->setAllowRedirects(false)
|
||||
->setUserAgent($userAgent)
|
||||
->fetch($url);
|
||||
|
||||
$status = $response->getStatusCode();
|
||||
if ($status < 300 || $status >= 400) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$headers = \array_change_key_case($response->getHeaders(), CASE_LOWER);
|
||||
$location = $headers['location'] ?? '';
|
||||
if ($location === '') {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$url = URLParse::resolveLocation($url, $location);
|
||||
}
|
||||
|
||||
throw new \RuntimeException('Too many redirects.');
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace Appwrite\Platform\Modules\Avatars\Http\Image;
|
||||
|
||||
use Appwrite\Extend\Exception;
|
||||
use Appwrite\Network\Validator\PublicHostname;
|
||||
use Appwrite\Platform\Modules\Avatars\Http\Action;
|
||||
use Appwrite\SDK\AuthType;
|
||||
use Appwrite\SDK\ContentType;
|
||||
@@ -69,10 +70,19 @@ class Get extends Action
|
||||
throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Imagick extension is missing');
|
||||
}
|
||||
|
||||
$domain = new Domain(\parse_url($url, PHP_URL_HOST));
|
||||
$host = \parse_url($url, PHP_URL_HOST) ?? '';
|
||||
|
||||
if (!$domain->isKnown()) {
|
||||
throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED);
|
||||
$isIpLiteral = \filter_var(\trim($host, '[]'), FILTER_VALIDATE_IP) !== false;
|
||||
if (!$isIpLiteral) {
|
||||
$domain = new Domain($host);
|
||||
if (!$domain->isKnown()) {
|
||||
throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED);
|
||||
}
|
||||
}
|
||||
|
||||
$hostnameValidator = new PublicHostname();
|
||||
if (!$hostnameValidator->isValid($host)) {
|
||||
throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED, $hostnameValidator->getDescription());
|
||||
}
|
||||
|
||||
$client = new Client();
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace Appwrite\Platform\Modules\Avatars\Http\Screenshots;
|
||||
|
||||
use Appwrite\Extend\Exception;
|
||||
use Appwrite\Network\Validator\PublicHostname;
|
||||
use Appwrite\Platform\Modules\Avatars\Http\Action;
|
||||
use Appwrite\SDK\AuthType;
|
||||
use Appwrite\SDK\ContentType;
|
||||
@@ -95,10 +96,19 @@ class Get extends Action
|
||||
throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Imagick extension is missing');
|
||||
}
|
||||
|
||||
$domain = new Domain(\parse_url($url, PHP_URL_HOST));
|
||||
$host = \parse_url($url, PHP_URL_HOST) ?? '';
|
||||
|
||||
if (!$domain->isKnown()) {
|
||||
throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED);
|
||||
$isIpLiteral = \filter_var(\trim($host, '[]'), FILTER_VALIDATE_IP) !== false;
|
||||
if (!$isIpLiteral) {
|
||||
$domain = new Domain($host);
|
||||
if (!$domain->isKnown()) {
|
||||
throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED);
|
||||
}
|
||||
}
|
||||
|
||||
$hostnameValidator = new PublicHostname();
|
||||
if (!$hostnameValidator->isValid($host)) {
|
||||
throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED, $hostnameValidator->getDescription());
|
||||
}
|
||||
|
||||
$client = new Client();
|
||||
|
||||
@@ -297,9 +297,10 @@ class Builds extends Action
|
||||
$templateReferenceValue = $template->getAttribute('referenceValue', '');
|
||||
|
||||
$templateRootDirectory = $template->getAttribute('rootDirectory', '');
|
||||
$templateRootDirectory = \rtrim($templateRootDirectory, '/');
|
||||
$templateRootDirectory = \ltrim($templateRootDirectory, '.');
|
||||
$templateRootDirectory = \ltrim($templateRootDirectory, '/');
|
||||
$templateRootDirectory = \trim($templateRootDirectory, '/');
|
||||
if ($templateRootDirectory !== '' && \preg_match('#(^|/)\.\.(/|$)#', $templateRootDirectory)) {
|
||||
throw new \Exception('Invalid template root directory');
|
||||
}
|
||||
|
||||
if (! empty($templateRepositoryName) && ! empty($templateOwnerName) && ! empty($templateReferenceType) && ! empty($templateReferenceValue)) {
|
||||
$stdout = '';
|
||||
@@ -362,9 +363,10 @@ class Builds extends Action
|
||||
// VCS and VCS+Temaplte
|
||||
$tmpDirectory = '/tmp/builds/' . $deploymentId . '/code';
|
||||
$rootDirectory = $resource->getAttribute('providerRootDirectory', '');
|
||||
$rootDirectory = \rtrim($rootDirectory, '/');
|
||||
$rootDirectory = \ltrim($rootDirectory, '.');
|
||||
$rootDirectory = \ltrim($rootDirectory, '/');
|
||||
$rootDirectory = \trim($rootDirectory, '/');
|
||||
if ($rootDirectory !== '' && \preg_match('#(^|/)\.\.(/|$)#', $rootDirectory)) {
|
||||
throw new \Exception('Invalid root directory');
|
||||
}
|
||||
|
||||
$owner = $github->getOwnerName($providerInstallationId);
|
||||
$repositoryName = $github->getRepositoryName($providerRepositoryId);
|
||||
@@ -420,9 +422,10 @@ class Builds extends Action
|
||||
$templateReferenceValue = $template->getAttribute('referenceValue', '');
|
||||
|
||||
$templateRootDirectory = $template->getAttribute('rootDirectory', '');
|
||||
$templateRootDirectory = \rtrim($templateRootDirectory, '/');
|
||||
$templateRootDirectory = \ltrim($templateRootDirectory, '.');
|
||||
$templateRootDirectory = \ltrim($templateRootDirectory, '/');
|
||||
$templateRootDirectory = \trim($templateRootDirectory, '/');
|
||||
if ($templateRootDirectory !== '' && \preg_match('#(^|/)\.\.(/|$)#', $templateRootDirectory)) {
|
||||
throw new \Exception('Invalid template root directory');
|
||||
}
|
||||
|
||||
if (! empty($templateRepositoryName) && ! empty($templateOwnerName) && ! empty($templateReferenceType) && ! empty($templateReferenceValue)) {
|
||||
// Clone template repo
|
||||
@@ -443,7 +446,8 @@ class Builds extends Action
|
||||
Console::execute('rsync -av --exclude \'.git\' ' . \escapeshellarg($tmpTemplateDirectory . '/' . $templateRootDirectory . '/') . ' ' . \escapeshellarg($tmpDirectory . '/' . $rootDirectory), '', $stdout, $stderr);
|
||||
|
||||
// Commit and push
|
||||
$exit = Console::execute('git config --global user.email ' . \escapeshellarg(APP_VCS_GITHUB_EMAIL) . ' && git config --global user.name ' . \escapeshellarg(APP_VCS_GITHUB_USERNAME) . ' && cd ' . \escapeshellarg($tmpDirectory) . ' && git checkout -b ' . \escapeshellarg($branchName) . ' && git add . && git commit -m "Create ' . \escapeshellarg($resource->getAttribute('name', '')) . ' function" && git push origin ' . \escapeshellarg($branchName), '', $stdout, $stderr);
|
||||
$commitMessage = \escapeshellarg('Create ' . $resource->getAttribute('name', '') . ' function');
|
||||
$exit = Console::execute('git config --global user.email ' . \escapeshellarg(APP_VCS_GITHUB_EMAIL) . ' && git config --global user.name ' . \escapeshellarg(APP_VCS_GITHUB_USERNAME) . ' && cd ' . \escapeshellarg($tmpDirectory) . ' && git checkout -b ' . \escapeshellarg($branchName) . ' && git add . && git commit -m ' . $commitMessage . ' && git push origin ' . \escapeshellarg($branchName), '', $stdout, $stderr);
|
||||
|
||||
if ($exit !== 0) {
|
||||
throw new \Exception('Unable to push code repository: ' . $stderr);
|
||||
|
||||
@@ -70,6 +70,10 @@ class Get extends Action
|
||||
throw new Exception(Exception::MEMBERSHIP_NOT_FOUND);
|
||||
}
|
||||
|
||||
if ($membership->getAttribute('teamInternalId') !== $team->getSequence()) {
|
||||
throw new Exception(Exception::TEAM_MEMBERSHIP_MISMATCH);
|
||||
}
|
||||
|
||||
// Default should be "false", but existing projects already rely on this being "true"
|
||||
$membershipsPrivacy = [
|
||||
'userName' => $project->getAttribute('auths', [])['membershipsUserName'] ?? true,
|
||||
|
||||
@@ -78,6 +78,10 @@ class Update extends Action
|
||||
throw new Exception(Exception::MEMBERSHIP_NOT_FOUND);
|
||||
}
|
||||
|
||||
if ($membership->getAttribute('teamInternalId') !== $team->getSequence()) {
|
||||
throw new Exception(Exception::TEAM_MEMBERSHIP_MISMATCH);
|
||||
}
|
||||
|
||||
$profile = $dbForProject->getDocument('users', $membership->getAttribute('userId'));
|
||||
if ($profile->isEmpty()) {
|
||||
throw new Exception(Exception::USER_NOT_FOUND);
|
||||
|
||||
@@ -6,6 +6,7 @@ use Appwrite\Event\Message\Mail as MailMessage;
|
||||
use Appwrite\Event\Message\Usage as UsageMessage;
|
||||
use Appwrite\Event\Publisher\Mail as MailPublisher;
|
||||
use Appwrite\Event\Publisher\Usage as UsagePublisher;
|
||||
use Appwrite\Network\Validator\PublicHostname;
|
||||
use Appwrite\Template\Template;
|
||||
use Appwrite\Usage\Context as UsageContext;
|
||||
use Exception;
|
||||
@@ -101,6 +102,16 @@ class Webhooks extends Action
|
||||
}
|
||||
|
||||
$url = \rawurldecode($webhook->getAttribute('url'));
|
||||
|
||||
if (System::getEnv('_APP_ENV', 'development') === 'production') {
|
||||
$host = \parse_url($url, PHP_URL_HOST) ?? '';
|
||||
$hostnameValidator = new PublicHostname();
|
||||
if (!$hostnameValidator->isValid($host)) {
|
||||
$this->errors[] = 'Webhook target ' . $host . ' rejected: ' . $hostnameValidator->getDescription();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$signatureKey = $webhook->getAttribute('signatureKey');
|
||||
$signature = base64_encode(hash_hmac('sha1', $url . $payload, $signatureKey, true));
|
||||
$httpUser = $webhook->getAttribute('httpUser');
|
||||
|
||||
@@ -118,4 +118,151 @@ class URL
|
||||
{
|
||||
return \http_build_query($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a Location-header reference against an absolute base URL,
|
||||
* following RFC 3986 §5.3 (Reference Resolution). Handles:
|
||||
* - absolute references (scheme present)
|
||||
* - protocol-relative (//host/path)
|
||||
* - absolute-path (/foo)
|
||||
* - relative-path (foo, ../foo)
|
||||
* - query-only (?x=1) -- keeps base path
|
||||
* - fragment-only (#frag) -- keeps base path AND base query
|
||||
* - dot-segment normalisation -- /a/b/../c → /a/c
|
||||
*/
|
||||
public static function resolveLocation(string $base, string $reference): string
|
||||
{
|
||||
$reference = \trim($reference);
|
||||
if ($reference === '') {
|
||||
return $base;
|
||||
}
|
||||
|
||||
$ref = \parse_url($reference);
|
||||
$bas = \parse_url($base);
|
||||
if (!\is_array($ref) || !\is_array($bas)) {
|
||||
return $reference;
|
||||
}
|
||||
|
||||
$target = [];
|
||||
|
||||
if (isset($ref['scheme'])) {
|
||||
$target['scheme'] = $ref['scheme'];
|
||||
self::copyAuthority($ref, $target);
|
||||
$target['path'] = self::removeDotSegments($ref['path'] ?? '');
|
||||
if (isset($ref['query'])) {
|
||||
$target['query'] = $ref['query'];
|
||||
}
|
||||
} else {
|
||||
$target['scheme'] = $bas['scheme'] ?? '';
|
||||
|
||||
if (isset($ref['host'])) {
|
||||
self::copyAuthority($ref, $target);
|
||||
$target['path'] = self::removeDotSegments($ref['path'] ?? '');
|
||||
if (isset($ref['query'])) {
|
||||
$target['query'] = $ref['query'];
|
||||
}
|
||||
} else {
|
||||
self::copyAuthority($bas, $target);
|
||||
$refPath = $ref['path'] ?? '';
|
||||
|
||||
if ($refPath === '') {
|
||||
$target['path'] = $bas['path'] ?? '';
|
||||
if (isset($ref['query'])) {
|
||||
$target['query'] = $ref['query'];
|
||||
} elseif (isset($bas['query'])) {
|
||||
$target['query'] = $bas['query'];
|
||||
}
|
||||
} else {
|
||||
if (\str_starts_with($refPath, '/')) {
|
||||
$target['path'] = self::removeDotSegments($refPath);
|
||||
} else {
|
||||
$target['path'] = self::removeDotSegments(self::mergePaths($bas, $refPath));
|
||||
}
|
||||
if (isset($ref['query'])) {
|
||||
$target['query'] = $ref['query'];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($ref['fragment'])) {
|
||||
$target['fragment'] = $ref['fragment'];
|
||||
}
|
||||
|
||||
return self::unparse($target);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $source
|
||||
* @param array $target
|
||||
*/
|
||||
private static function copyAuthority(array $source, array &$target): void
|
||||
{
|
||||
foreach (['user', 'pass', 'host', 'port'] as $key) {
|
||||
if (isset($source[$key])) {
|
||||
$target[$key] = $source[$key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* RFC 3986 §5.2.3 — merge base path with reference path.
|
||||
*/
|
||||
private static function mergePaths(array $base, string $referencePath): string
|
||||
{
|
||||
if (isset($base['host']) && empty($base['path'])) {
|
||||
return '/' . $referencePath;
|
||||
}
|
||||
|
||||
$basePath = $base['path'] ?? '';
|
||||
$slash = \strrpos($basePath, '/');
|
||||
if ($slash === false) {
|
||||
return $referencePath;
|
||||
}
|
||||
|
||||
return \substr($basePath, 0, $slash + 1) . $referencePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* RFC 3986 §5.2.4 — remove "." and ".." segments from a path.
|
||||
*/
|
||||
private static function removeDotSegments(string $path): string
|
||||
{
|
||||
$output = '';
|
||||
$input = $path;
|
||||
|
||||
while ($input !== '') {
|
||||
if (\str_starts_with($input, '../')) {
|
||||
$input = \substr($input, 3);
|
||||
} elseif (\str_starts_with($input, './')) {
|
||||
$input = \substr($input, 2);
|
||||
} elseif (\str_starts_with($input, '/./')) {
|
||||
$input = '/' . \substr($input, 3);
|
||||
} elseif ($input === '/.') {
|
||||
$input = '/';
|
||||
} elseif (\str_starts_with($input, '/../')) {
|
||||
$input = '/' . \substr($input, 4);
|
||||
$slash = \strrpos($output, '/');
|
||||
$output = $slash === false ? '' : \substr($output, 0, $slash);
|
||||
} elseif ($input === '/..') {
|
||||
$input = '/';
|
||||
$slash = \strrpos($output, '/');
|
||||
$output = $slash === false ? '' : \substr($output, 0, $slash);
|
||||
} elseif ($input === '.' || $input === '..') {
|
||||
$input = '';
|
||||
} else {
|
||||
$start = ($input[0] === '/') ? 1 : 0;
|
||||
$next = \strpos($input, '/', $start);
|
||||
if ($next === false) {
|
||||
$output .= $input;
|
||||
$input = '';
|
||||
} else {
|
||||
$output .= \substr($input, 0, $next);
|
||||
$input = \substr($input, $next);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
@@ -795,7 +795,9 @@ class DatabasesStringTypesTest extends Scope
|
||||
|
||||
$this->assertEquals(204, $deleteVarchar['headers']['status-code']);
|
||||
|
||||
// Poll until async deletion completes
|
||||
// Poll until async deletion completes. Timeout raised to 60s to match
|
||||
// the migration-polling and schema-polling defaults used elsewhere —
|
||||
// shared-mode CI delete-worker contention can exceed the original 30s.
|
||||
$this->assertEventually(function () use ($databaseId, $tableId) {
|
||||
$response = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/varchar_min', [
|
||||
'content-type' => 'application/json',
|
||||
@@ -803,6 +805,6 @@ class DatabasesStringTypesTest extends Scope
|
||||
'x-appwrite-key' => $this->getProject()['apiKey'],
|
||||
]);
|
||||
$this->assertEquals(404, $response['headers']['status-code']);
|
||||
}, 30000, 250);
|
||||
}, 60000, 250);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,250 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Network;
|
||||
|
||||
use Appwrite\Extend\Exception;
|
||||
use Appwrite\Platform\Modules\Avatars\Http\Favicon\Get;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Utopia\Fetch\Adapter;
|
||||
use Utopia\Fetch\Options\Request as RequestOptions;
|
||||
use Utopia\Fetch\Response;
|
||||
|
||||
class FetcherTest extends TestCase
|
||||
{
|
||||
public function testAssertSafeAcceptsPublicIpLiteral(): void
|
||||
{
|
||||
// Should not throw
|
||||
TestableGet::assertSafe('http://1.1.1.1/');
|
||||
TestableGet::assertSafe('https://8.8.8.8/path');
|
||||
$this->expectNotToPerformAssertions();
|
||||
}
|
||||
|
||||
#[DataProvider('unsafeUrls')]
|
||||
public function testAssertSafeRejectsUnsafeUrls(string $url, string $reasonFragment): void
|
||||
{
|
||||
try {
|
||||
TestableGet::assertSafe($url);
|
||||
$this->fail("Expected Exception for {$url}");
|
||||
} catch (Exception $e) {
|
||||
$this->assertSame(Exception::AVATAR_REMOTE_URL_FAILED, $e->getType());
|
||||
$this->assertStringContainsString(
|
||||
$reasonFragment,
|
||||
$e->getMessage(),
|
||||
"Wrong rejection reason for {$url}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public static function unsafeUrls(): array
|
||||
{
|
||||
return [
|
||||
'loopback v4' => ['http://127.0.0.1/iam', 'private or reserved'],
|
||||
'loopback v6' => ['http://[::1]/iam', 'private or reserved'],
|
||||
'link-local imds' => ['http://169.254.169.254/latest/', 'private or reserved'],
|
||||
'private rfc1918 a' => ['http://10.0.0.5/secret', 'private or reserved'],
|
||||
'private rfc1918 b' => ['http://192.168.1.1/admin', 'private or reserved'],
|
||||
'private rfc1918 c' => ['http://172.16.0.1/x', 'private or reserved'],
|
||||
'cgnat' => ['http://100.64.0.1/x', 'private or reserved'],
|
||||
'ipv4-mapped loopback' => ['http://[::ffff:127.0.0.1]/x', 'private or reserved'],
|
||||
'6to4 imds' => ['http://[2002:a9fe:a9fe::]/x', 'private or reserved'],
|
||||
'file scheme' => ['file:///etc/passwd', "Scheme 'file' is not allowed"],
|
||||
'gopher scheme' => ['gopher://attacker/x', "Scheme 'gopher' is not allowed"],
|
||||
'ftp scheme' => ['ftp://internal/file', "Scheme 'ftp' is not allowed"],
|
||||
'no scheme' => ['example.com/path', "Scheme '' is not allowed"],
|
||||
'malformed no host' => ['http:///path', 'Malformed URL'],
|
||||
'unknown psl' => ['http://not-a-real-tld.notreal/x', 'not a known public domain'],
|
||||
];
|
||||
}
|
||||
|
||||
public function testFetchFollowsPublicRedirect(): void
|
||||
{
|
||||
$adapter = $this->scriptedAdapter([
|
||||
$this->redirect('https://1.1.1.1/final'),
|
||||
$this->ok('FINAL_BODY'),
|
||||
]);
|
||||
|
||||
$fetcher = new TestableGet();
|
||||
$response = $fetcher->fetchForTest('http://8.8.8.8/start', $adapter);
|
||||
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
$this->assertSame('FINAL_BODY', $response->getBody());
|
||||
$this->assertSame(2, $adapter->callCount);
|
||||
}
|
||||
|
||||
public function testFetchBlocksRedirectToPrivateIp(): void
|
||||
{
|
||||
// This is the exact SSRF chain from the report:
|
||||
// public attacker page → 302 → 169.254.169.254 (AWS IMDS)
|
||||
$adapter = $this->scriptedAdapter([
|
||||
$this->redirect('http://169.254.169.254/latest/meta-data/iam/security-credentials/'),
|
||||
// This second response must NEVER be served — the fetcher should
|
||||
// reject the redirect target before calling send() again.
|
||||
$this->ok('SHOULD_NEVER_LEAK'),
|
||||
]);
|
||||
|
||||
$fetcher = new TestableGet();
|
||||
|
||||
try {
|
||||
$fetcher->fetchForTest('http://8.8.8.8/attacker-page', $adapter);
|
||||
$this->fail('Expected Exception');
|
||||
} catch (Exception $e) {
|
||||
$this->assertSame(Exception::AVATAR_REMOTE_URL_FAILED, $e->getType());
|
||||
$this->assertStringContainsString('169.254.169.254', $e->getMessage());
|
||||
$this->assertSame(
|
||||
1,
|
||||
$adapter->callCount,
|
||||
'Redirect target should not have been fetched — only the initial request should hit the adapter'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function testFetchBlocksRedirectToLoopback(): void
|
||||
{
|
||||
$adapter = $this->scriptedAdapter([
|
||||
$this->redirect('http://127.0.0.1:9999/secrets'),
|
||||
$this->ok('SHOULD_NEVER_LEAK'),
|
||||
]);
|
||||
|
||||
$fetcher = new TestableGet();
|
||||
|
||||
try {
|
||||
$fetcher->fetchForTest('http://8.8.8.8/attacker-page', $adapter);
|
||||
$this->fail('Expected Exception');
|
||||
} catch (Exception $e) {
|
||||
$this->assertSame(Exception::AVATAR_REMOTE_URL_FAILED, $e->getType());
|
||||
$this->assertStringContainsString('127.0.0.1', $e->getMessage());
|
||||
$this->assertSame(1, $adapter->callCount);
|
||||
}
|
||||
}
|
||||
|
||||
public function testFetchBlocksRedirectToRelativePathOnPrivateBase(): void
|
||||
{
|
||||
// Edge case: a server on a public IP returns a relative redirect.
|
||||
// We resolve it against the (still public) base and continue — safe.
|
||||
$adapter = $this->scriptedAdapter([
|
||||
$this->redirect('/different-path'),
|
||||
$this->ok('FINAL'),
|
||||
]);
|
||||
|
||||
$fetcher = new TestableGet();
|
||||
$response = $fetcher->fetchForTest('http://8.8.8.8/start', $adapter);
|
||||
|
||||
$this->assertSame('FINAL', $response->getBody());
|
||||
}
|
||||
|
||||
public function testFetchRejectsRedirectChainOverLimit(): void
|
||||
{
|
||||
// Six redirects all to public targets; with maxRedirects=5 the sixth
|
||||
// hop trips the limit.
|
||||
$adapter = $this->scriptedAdapter([
|
||||
$this->redirect('https://1.1.1.1/2'),
|
||||
$this->redirect('https://1.0.0.1/3'),
|
||||
$this->redirect('https://8.8.8.8/4'),
|
||||
$this->redirect('https://8.8.4.4/5'),
|
||||
$this->redirect('https://9.9.9.9/6'),
|
||||
$this->redirect('https://208.67.222.222/7'),
|
||||
]);
|
||||
|
||||
$fetcher = new TestableGet();
|
||||
|
||||
$this->expectException(\RuntimeException::class);
|
||||
$this->expectExceptionMessage('Too many redirects');
|
||||
$fetcher->fetchForTest('http://8.8.8.8/0', $adapter);
|
||||
}
|
||||
|
||||
public function testFetchBlocksNonHttpRedirect(): void
|
||||
{
|
||||
$adapter = $this->scriptedAdapter([
|
||||
$this->redirect('file:///etc/passwd'),
|
||||
$this->ok('SHOULD_NEVER_LEAK'),
|
||||
]);
|
||||
|
||||
$fetcher = new TestableGet();
|
||||
|
||||
try {
|
||||
$fetcher->fetchForTest('http://8.8.8.8/page', $adapter);
|
||||
$this->fail('Expected Exception');
|
||||
} catch (Exception $e) {
|
||||
$this->assertSame(Exception::AVATAR_REMOTE_URL_FAILED, $e->getType());
|
||||
$this->assertStringContainsString("Scheme 'file'", $e->getMessage());
|
||||
$this->assertSame(1, $adapter->callCount);
|
||||
}
|
||||
}
|
||||
|
||||
private function redirect(string $location): Response
|
||||
{
|
||||
// Mix the header key case on purpose. Fetcher must normalise per
|
||||
// RFC 7230 §3.2; if it ever drops to a naive lowercase lookup the
|
||||
// entire redirect-following suite fails — that's the regression
|
||||
// signal we want.
|
||||
return new Response(302, '', ['Location' => $location]);
|
||||
}
|
||||
|
||||
private function ok(string $body): Response
|
||||
{
|
||||
return new Response(200, $body, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Response[] $responses
|
||||
*/
|
||||
private function scriptedAdapter(array $responses): ScriptedAdapter
|
||||
{
|
||||
return new ScriptedAdapter($responses);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test-only adapter that returns a pre-scripted list of responses in order.
|
||||
* Exposed as a named class so PHPStan can see the callCount property.
|
||||
*/
|
||||
class ScriptedAdapter implements Adapter
|
||||
{
|
||||
public int $callCount = 0;
|
||||
|
||||
/** @param Response[] $responses */
|
||||
public function __construct(private array $responses)
|
||||
{
|
||||
}
|
||||
|
||||
public function send(
|
||||
string $url,
|
||||
string $method,
|
||||
mixed $body,
|
||||
array $headers,
|
||||
RequestOptions $options,
|
||||
?callable $chunkCallback = null
|
||||
): Response {
|
||||
$response = $this->responses[$this->callCount] ?? null;
|
||||
$this->callCount++;
|
||||
if ($response === null) {
|
||||
throw new \RuntimeException("Adapter ran out of scripted responses (call {$this->callCount})");
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
class TestableGet extends Get
|
||||
{
|
||||
/**
|
||||
* Skip parent constructor. Action::__construct chains into
|
||||
* Appwrite\SDK\Method, which loads Swoole at file-load time —
|
||||
* Swoole is a production-only extension, so building the real
|
||||
* Action in a unit test fails. The protected method bridges
|
||||
* below are all the surface the redirect-chain tests need.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public static function assertSafe(string $url): void
|
||||
{
|
||||
parent::assertSafeUrl($url);
|
||||
}
|
||||
|
||||
public function fetchForTest(string $url, Adapter $adapter): Response
|
||||
{
|
||||
return $this->safeFetch($url, 'test', $adapter);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Network\Validators;
|
||||
|
||||
use Appwrite\Network\Validator\PublicHostname;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class PublicHostnameTest extends TestCase
|
||||
{
|
||||
public function testRejectsEmptyAndNonString(): void
|
||||
{
|
||||
$validator = new PublicHostname();
|
||||
|
||||
$this->assertFalse($validator->isValid(''));
|
||||
$this->assertFalse($validator->isValid(null));
|
||||
$this->assertFalse($validator->isValid(123));
|
||||
$this->assertFalse($validator->isValid([]));
|
||||
}
|
||||
|
||||
#[DataProvider('privateIpv4Addresses')]
|
||||
public function testRejectsPrivateIpv4Literals(string $ip): void
|
||||
{
|
||||
$validator = new PublicHostname();
|
||||
|
||||
$this->assertFalse(
|
||||
$validator->isValid($ip),
|
||||
"Expected {$ip} to be rejected as private/reserved"
|
||||
);
|
||||
$this->assertFalse(PublicHostname::isPublicIp($ip));
|
||||
}
|
||||
|
||||
public static function privateIpv4Addresses(): array
|
||||
{
|
||||
return [
|
||||
'unspecified' => ['0.0.0.0'],
|
||||
'private 10/8' => ['10.0.0.1'],
|
||||
'private 172.16/12' => ['172.16.5.10'],
|
||||
'private 192.168/16' => ['192.168.1.1'],
|
||||
'cgnat' => ['100.64.0.1'],
|
||||
'loopback' => ['127.0.0.1'],
|
||||
'link-local imds' => ['169.254.169.254'],
|
||||
'gcp metadata' => ['169.254.169.254'],
|
||||
'multicast' => ['224.0.0.1'],
|
||||
'reserved 240/4' => ['240.0.0.1'],
|
||||
'broadcast' => ['255.255.255.255'],
|
||||
'test-net-1' => ['192.0.2.1'],
|
||||
'test-net-2' => ['198.51.100.1'],
|
||||
'test-net-3' => ['203.0.113.1'],
|
||||
'benchmark' => ['198.18.0.1'],
|
||||
];
|
||||
}
|
||||
|
||||
#[DataProvider('privateIpv6Addresses')]
|
||||
public function testRejectsPrivateIpv6Literals(string $ip): void
|
||||
{
|
||||
$validator = new PublicHostname();
|
||||
|
||||
$this->assertFalse(
|
||||
$validator->isValid($ip),
|
||||
"Expected {$ip} to be rejected as private/reserved"
|
||||
);
|
||||
$this->assertFalse(PublicHostname::isPublicIp($ip));
|
||||
}
|
||||
|
||||
public static function privateIpv6Addresses(): array
|
||||
{
|
||||
return [
|
||||
'loopback' => ['::1'],
|
||||
'unspecified' => ['::'],
|
||||
'link-local' => ['fe80::1'],
|
||||
'unique-local' => ['fc00::1'],
|
||||
'unique-local fd' => ['fd12:3456:789a::1'],
|
||||
'multicast' => ['ff02::1'],
|
||||
'ipv4-mapped loopback' => ['::ffff:127.0.0.1'],
|
||||
'ipv4-mapped private' => ['::ffff:10.0.0.1'],
|
||||
'ipv4-mapped imds' => ['::ffff:169.254.169.254'],
|
||||
'6to4 loopback' => ['2002:7f00:1::'],
|
||||
'6to4 imds' => ['2002:a9fe:a9fe::'],
|
||||
'teredo' => ['2001:0:1::1'],
|
||||
'documentation' => ['2001:db8::1'],
|
||||
];
|
||||
}
|
||||
|
||||
#[DataProvider('publicIpAddresses')]
|
||||
public function testAcceptsPublicIpLiterals(string $ip): void
|
||||
{
|
||||
$validator = new PublicHostname();
|
||||
|
||||
$this->assertTrue(
|
||||
$validator->isValid($ip),
|
||||
"Expected {$ip} to be accepted as public"
|
||||
);
|
||||
$this->assertTrue(PublicHostname::isPublicIp($ip));
|
||||
}
|
||||
|
||||
public static function publicIpAddresses(): array
|
||||
{
|
||||
return [
|
||||
'google dns' => ['8.8.8.8'],
|
||||
'cloudflare' => ['1.1.1.1'],
|
||||
'opendns' => ['208.67.222.222'],
|
||||
'public ipv6' => ['2606:4700:4700::1111'],
|
||||
];
|
||||
}
|
||||
|
||||
public function testRejectsMalformedInput(): void
|
||||
{
|
||||
$validator = new PublicHostname();
|
||||
|
||||
$this->assertFalse($validator->isValid('not a hostname at all'));
|
||||
$this->assertFalse($validator->isValid('http://example.com'));
|
||||
$this->assertFalse($validator->isValid('999.999.999.999'));
|
||||
}
|
||||
|
||||
public function testRejectsNonResolvingHostname(): void
|
||||
{
|
||||
$validator = new PublicHostname();
|
||||
|
||||
// RFC 2606 reserves this for testing — guaranteed not to resolve.
|
||||
$this->assertFalse($validator->isValid('a-hostname-that-does-not-exist.invalid'));
|
||||
}
|
||||
|
||||
public function testReasonIsPopulatedOnFailure(): void
|
||||
{
|
||||
$validator = new PublicHostname();
|
||||
$validator->isValid('127.0.0.1');
|
||||
|
||||
$this->assertStringContainsString('127.0.0.1', $validator->getDescription());
|
||||
$this->assertStringContainsString('private', $validator->getDescription());
|
||||
}
|
||||
|
||||
public function testReasonResetsBetweenCalls(): void
|
||||
{
|
||||
$validator = new PublicHostname();
|
||||
|
||||
$validator->isValid('');
|
||||
$this->assertSame('Hostname is empty.', $validator->getDescription());
|
||||
|
||||
$validator->isValid('127.0.0.1');
|
||||
$this->assertStringContainsString('127.0.0.1', $validator->getDescription());
|
||||
}
|
||||
|
||||
public function testIpInCidrEdgesViaPublicIp(): void
|
||||
{
|
||||
// Boundary checks: first and last address of common ranges.
|
||||
$this->assertFalse(PublicHostname::isPublicIp('10.0.0.0'));
|
||||
$this->assertFalse(PublicHostname::isPublicIp('10.255.255.255'));
|
||||
$this->assertTrue(PublicHostname::isPublicIp('11.0.0.0'));
|
||||
$this->assertTrue(PublicHostname::isPublicIp('9.255.255.255'));
|
||||
|
||||
$this->assertFalse(PublicHostname::isPublicIp('169.254.0.0'));
|
||||
$this->assertFalse(PublicHostname::isPublicIp('169.254.255.255'));
|
||||
$this->assertTrue(PublicHostname::isPublicIp('169.253.255.255'));
|
||||
$this->assertTrue(PublicHostname::isPublicIp('169.255.0.0'));
|
||||
|
||||
$this->assertFalse(PublicHostname::isPublicIp('100.64.0.0'));
|
||||
$this->assertFalse(PublicHostname::isPublicIp('100.127.255.255'));
|
||||
$this->assertTrue(PublicHostname::isPublicIp('100.128.0.0'));
|
||||
$this->assertTrue(PublicHostname::isPublicIp('100.63.255.255'));
|
||||
}
|
||||
}
|
||||
@@ -114,4 +114,156 @@ class URLTest extends TestCase
|
||||
|
||||
$this->assertEquals('param1=value1¶m2=value2', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* RFC 3986 §5.4 reference resolution test cases.
|
||||
*/
|
||||
public function testResolveLocationAbsolute(): void
|
||||
{
|
||||
// Absolute URL wins over the base
|
||||
$this->assertSame(
|
||||
'https://other.example/path',
|
||||
URL::resolveLocation('https://base.example/a/b', 'https://other.example/path')
|
||||
);
|
||||
|
||||
// Different scheme too
|
||||
$this->assertSame(
|
||||
'http://other.example/',
|
||||
URL::resolveLocation('https://base.example/a/b', 'http://other.example/')
|
||||
);
|
||||
}
|
||||
|
||||
public function testResolveLocationProtocolRelative(): void
|
||||
{
|
||||
// Inherits scheme from base
|
||||
$this->assertSame(
|
||||
'https://other.example/path',
|
||||
URL::resolveLocation('https://base.example/a/b', '//other.example/path')
|
||||
);
|
||||
|
||||
// Including with a port
|
||||
$this->assertSame(
|
||||
'https://other.example:8080/path',
|
||||
URL::resolveLocation('https://base.example/', '//other.example:8080/path')
|
||||
);
|
||||
}
|
||||
|
||||
public function testResolveLocationAbsolutePath(): void
|
||||
{
|
||||
$this->assertSame(
|
||||
'https://base.example/new',
|
||||
URL::resolveLocation('https://base.example/a/b', '/new')
|
||||
);
|
||||
|
||||
// Preserves port
|
||||
$this->assertSame(
|
||||
'https://base.example:9000/new',
|
||||
URL::resolveLocation('https://base.example:9000/a/b', '/new')
|
||||
);
|
||||
}
|
||||
|
||||
public function testResolveLocationRelativePath(): void
|
||||
{
|
||||
$this->assertSame(
|
||||
'https://base.example/a/c',
|
||||
URL::resolveLocation('https://base.example/a/b', 'c')
|
||||
);
|
||||
|
||||
$this->assertSame(
|
||||
'https://base.example/a/b/c',
|
||||
URL::resolveLocation('https://base.example/a/b/', 'c')
|
||||
);
|
||||
}
|
||||
|
||||
public function testResolveLocationQueryOnly(): void
|
||||
{
|
||||
// The bug Copilot called out: query-only must keep the base path
|
||||
$this->assertSame(
|
||||
'https://base.example/a/b?x=1',
|
||||
URL::resolveLocation('https://base.example/a/b', '?x=1')
|
||||
);
|
||||
|
||||
// Replaces any base query
|
||||
$this->assertSame(
|
||||
'https://base.example/a/b?x=1',
|
||||
URL::resolveLocation('https://base.example/a/b?old=value', '?x=1')
|
||||
);
|
||||
}
|
||||
|
||||
public function testResolveLocationFragmentOnly(): void
|
||||
{
|
||||
// Fragment-only must keep base path AND base query
|
||||
$this->assertSame(
|
||||
'https://base.example/a/b#section',
|
||||
URL::resolveLocation('https://base.example/a/b', '#section')
|
||||
);
|
||||
|
||||
$this->assertSame(
|
||||
'https://base.example/a/b?x=1#section',
|
||||
URL::resolveLocation('https://base.example/a/b?x=1', '#section')
|
||||
);
|
||||
}
|
||||
|
||||
public function testResolveLocationDotSegments(): void
|
||||
{
|
||||
// RFC 3986 §5.2.4 normalisation. `/c/d/..` resolves to `/c/`
|
||||
// (the trailing slash is significant — it's the directory of `d`).
|
||||
$this->assertSame(
|
||||
'https://base.example/c/',
|
||||
URL::resolveLocation('https://base.example/a/b', '/c/d/..')
|
||||
);
|
||||
|
||||
$this->assertSame(
|
||||
'https://base.example/c/',
|
||||
URL::resolveLocation('https://base.example/a/b', '/c/d/../')
|
||||
);
|
||||
|
||||
// From inside the directory `/a/b/`, `../c` goes up one level → `/a/c`
|
||||
$this->assertSame(
|
||||
'https://base.example/a/c',
|
||||
URL::resolveLocation('https://base.example/a/b/', '../c')
|
||||
);
|
||||
|
||||
// From file `/a/b`, `../c` goes up to root → `/c`
|
||||
$this->assertSame(
|
||||
'https://base.example/c',
|
||||
URL::resolveLocation('https://base.example/a/b', '../c')
|
||||
);
|
||||
|
||||
$this->assertSame(
|
||||
'https://base.example/a/b/c',
|
||||
URL::resolveLocation('https://base.example/a/b/', './c')
|
||||
);
|
||||
}
|
||||
|
||||
public function testResolveLocationEmpty(): void
|
||||
{
|
||||
// Empty reference returns base unchanged
|
||||
$this->assertSame(
|
||||
'https://base.example/a/b',
|
||||
URL::resolveLocation('https://base.example/a/b', '')
|
||||
);
|
||||
|
||||
// Whitespace-only reference is treated as empty
|
||||
$this->assertSame(
|
||||
'https://base.example/a/b',
|
||||
URL::resolveLocation('https://base.example/a/b', ' ')
|
||||
);
|
||||
}
|
||||
|
||||
public function testResolveLocationPreservesAuthorityFromBase(): void
|
||||
{
|
||||
// user/pass survive relative resolution
|
||||
$this->assertSame(
|
||||
'https://user:pass@base.example/c',
|
||||
URL::resolveLocation('https://user:pass@base.example/a/b', '/c')
|
||||
);
|
||||
|
||||
// port survives, and `c` relative to `/a/b` resolves to `/a/c`
|
||||
// (RFC 3986 §5.2.3 merge replaces the last segment of the base path).
|
||||
$this->assertSame(
|
||||
'http://base.example:8080/a/c',
|
||||
URL::resolveLocation('http://base.example:8080/a/b', 'c')
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user