mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Bind project management authorization to path target (#13294)
This commit is contained in:
1 parent
46e8cc7bba
commit
b2208a3d11
3 files changed
+98
-15
No files matched your search
@@ -53,6 +53,7 @@ Http::init()
|
||||
->inject('dbForProject')
|
||||
->inject('auditContext')
|
||||
->inject('project')
|
||||
->inject('projectIdFromPath')
|
||||
->inject('user')
|
||||
->inject('session')
|
||||
->inject('servers')
|
||||
@@ -63,7 +64,7 @@ Http::init()
|
||||
->inject('lock')
|
||||
->inject('impersonatorUser')
|
||||
->inject('targetUser')
|
||||
->action(function (Route $route, Request $request, Database $dbForPlatform, Database $dbForProject, AuditContext $auditContext, Document $project, User $user, ?Document $session, array $servers, string $mode, Document $team, ?Key $apiKey, Authorization $authorization, Lock $lock, Document $impersonatorUser, User $targetUser) {
|
||||
->action(function (Route $route, Request $request, Database $dbForPlatform, Database $dbForProject, AuditContext $auditContext, Document $project, string $projectIdFromPath, User $user, ?Document $session, array $servers, string $mode, Document $team, ?Key $apiKey, Authorization $authorization, Lock $lock, Document $impersonatorUser, User $targetUser) {
|
||||
|
||||
/**
|
||||
* Handle user authentication and session validation.
|
||||
@@ -110,6 +111,17 @@ Http::init()
|
||||
* - Throw exception if factors incomplete
|
||||
*/
|
||||
|
||||
// Bind project management authorization to the project in the path.
|
||||
if ($projectIdFromPath !== '') {
|
||||
$headerProjectId = $request->getHeaderLine('x-appwrite-project', '');
|
||||
|
||||
foreach ([$headerProjectId, $project->getId()] as $contextProjectId) {
|
||||
if ($contextProjectId !== '' && $contextProjectId !== 'console' && $contextProjectId !== $projectIdFromPath) {
|
||||
throw new Exception(Exception::USER_UNAUTHORIZED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Step 1: Check if project is empty
|
||||
if ($project->isEmpty()) {
|
||||
throw new Exception(Exception::PROJECT_NOT_FOUND);
|
||||
@@ -293,8 +305,7 @@ Http::init()
|
||||
|
||||
$projectId = $project->getId();
|
||||
if ($projectId === 'console' && str_starts_with($route->getPath(), '/v1/projects/:projectId')) {
|
||||
$uri = $request->getURI();
|
||||
$projectId = explode('/', $uri)[3];
|
||||
$projectId = $projectIdFromPath;
|
||||
}
|
||||
|
||||
// Base scopes for admin users to allow listing teams and projects.
|
||||
|
||||
@@ -582,7 +582,22 @@ return function (Container $context): void {
|
||||
return $user;
|
||||
}, ['mode', 'project', 'console', 'request', 'response', 'dbForProject', 'dbForPlatform', 'store', 'proofForToken', 'authorization']);
|
||||
|
||||
$context->set('project', function ($dbForPlatform, $request, $console, $authorization, Http $utopia) {
|
||||
$context->set('projectIdFromPath', function (Request $request, Http $utopia): string {
|
||||
$match = $utopia->match($request);
|
||||
if (empty($match)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$path = (string) \parse_url($request->getURI(), PHP_URL_PATH);
|
||||
$segments = \array_values(\array_filter(\explode('/', $path), fn (string $segment) => $segment !== ''));
|
||||
if (($segments[0] ?? '') !== 'v1' || ($segments[1] ?? '') !== 'projects') {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $match->params['projectId'] ?? '';
|
||||
}, ['request', 'utopia']);
|
||||
|
||||
$context->set('project', function ($dbForPlatform, $request, $console, $authorization, Http $utopia, string $projectIdFromPath) {
|
||||
/** @var Appwrite\Utopia\Request $request */
|
||||
/** @var Utopia\Database\Database $dbForPlatform */
|
||||
/** @var Utopia\Database\Document $console */
|
||||
@@ -594,7 +609,7 @@ return function (Container $context): void {
|
||||
// For non-GET requests getParam() reads the body, so a project passed
|
||||
// as a query parameter (e.g. presigned artifact URLs) is only visible
|
||||
// via getQuery().
|
||||
if (empty($projectId)) {
|
||||
if ($projectId === '') {
|
||||
$projectId = (string) $request->getQuery('project', '');
|
||||
}
|
||||
|
||||
@@ -604,22 +619,22 @@ return function (Container $context): void {
|
||||
$deprecatedProjectPathPrefix = '/v1/projects/';
|
||||
$route = $utopia->match($request)?->route;
|
||||
if (!empty($route)) {
|
||||
$isDeprecatedAlias = \str_starts_with($request->getURI(), $deprecatedProjectPathPrefix) &&
|
||||
$isDeprecatedAlias = $projectIdFromPath !== '' &&
|
||||
!\str_starts_with($route->getPath(), $deprecatedProjectPathPrefix);
|
||||
|
||||
if ($isDeprecatedAlias) {
|
||||
$projectId = \explode('/', $request->getURI(), 5)[3] ?? '';
|
||||
$projectId = $projectIdFromPath;
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($projectId) || $projectId === 'console') {
|
||||
if ($projectId === '' || $projectId === 'console') {
|
||||
return $console;
|
||||
}
|
||||
|
||||
$project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $projectId));
|
||||
|
||||
return $project;
|
||||
}, ['dbForPlatform', 'request', 'console', 'authorization', 'utopia']);
|
||||
}, ['dbForPlatform', 'request', 'console', 'authorization', 'utopia', 'projectIdFromPath']);
|
||||
|
||||
$context->set('session', function (User $user, Store $store, Token $proofForToken) {
|
||||
if ($user->isEmpty()) {
|
||||
@@ -891,7 +906,7 @@ return function (Container $context): void {
|
||||
$mode = $request->getParam('mode', $request->getHeaderLine('x-appwrite-mode', APP_MODE_DEFAULT));
|
||||
|
||||
$projectId = $request->getParam('project', $request->getHeaderLine('x-appwrite-project', ''));
|
||||
if (!empty($projectId) && $project->getId() !== $projectId) {
|
||||
if ($projectId !== '' && $project->getId() !== $projectId) {
|
||||
$mode = APP_MODE_ADMIN;
|
||||
}
|
||||
|
||||
@@ -962,7 +977,7 @@ return function (Container $context): void {
|
||||
return $key;
|
||||
}, ['request', 'project', 'servers', 'dbForPlatform', 'authorization']);
|
||||
|
||||
$context->set('team', function (Document $project, Database $dbForPlatform, Http $utopia, Request $request, Authorization $authorization) {
|
||||
$context->set('team', function (Document $project, Database $dbForPlatform, Http $utopia, Request $request, Authorization $authorization, string $projectIdFromPath) {
|
||||
$teamInternalId = '';
|
||||
if ($project->getId() !== 'console') {
|
||||
$teamInternalId = $project->getAttribute('teamInternalId', '');
|
||||
@@ -971,9 +986,7 @@ return function (Container $context): void {
|
||||
$path = ! empty($route) ? $route->getPath() : $request->getURI();
|
||||
$orgHeader = $request->getHeaderLine('x-appwrite-organization', '');
|
||||
if (str_starts_with($path, '/v1/projects/:projectId')) {
|
||||
$uri = $request->getURI();
|
||||
$pid = explode('/', $uri)[3];
|
||||
$p = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $pid));
|
||||
$p = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $projectIdFromPath));
|
||||
$teamInternalId = $p->getAttribute('teamInternalId', '');
|
||||
} elseif ($path === '/v1/projects') {
|
||||
$teamId = $request->getParam('teamId', '');
|
||||
@@ -1003,7 +1016,7 @@ return function (Container $context): void {
|
||||
});
|
||||
|
||||
return $team;
|
||||
}, ['project', 'dbForPlatform', 'utopia', 'request', 'authorization']);
|
||||
}, ['project', 'dbForPlatform', 'utopia', 'request', 'authorization', 'projectIdFromPath']);
|
||||
|
||||
$context->set('previewHostname', function (Request $request, ?Key $apiKey) {
|
||||
$allowed = false;
|
||||
|
||||
@@ -338,6 +338,65 @@ final class ProjectsConsoleClientTest extends Scope
|
||||
$this->assertNotEmpty($response['body']['$id']);
|
||||
$this->assertEquals('Team 1 Project', $response['body']['name']);
|
||||
$this->assertEquals($team2, $response['body']['teamId']);
|
||||
|
||||
$victimProjectId = ID::unique();
|
||||
$response = $this->client->call(Client::METHOD_POST, '/projects', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
'x-appwrite-response-format' => '1.9.4',
|
||||
], $this->getHeaders()), [
|
||||
'projectId' => $victimProjectId,
|
||||
'name' => 'Victim Project',
|
||||
'teamId' => $team1,
|
||||
'region' => System::getEnv('_APP_REGION', 'default'),
|
||||
]);
|
||||
|
||||
$this->assertEquals(201, $response['headers']['status-code']);
|
||||
|
||||
$response = $this->client->call(Client::METHOD_PATCH, '/projects/' . $victimProjectId . '/team', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $projectId,
|
||||
'x-appwrite-mode' => 'admin',
|
||||
'x-appwrite-response-format' => '1.9.4',
|
||||
], $this->getHeaders()), [
|
||||
'teamId' => $team2,
|
||||
]);
|
||||
|
||||
$this->assertEquals(401, $response['headers']['status-code']);
|
||||
$this->assertEquals(Exception::USER_UNAUTHORIZED, $response['body']['type']);
|
||||
|
||||
$response = $this->client->call(Client::METHOD_PATCH, '//projects/anything/team', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $projectId,
|
||||
'x-appwrite-mode' => 'admin',
|
||||
'x-appwrite-response-format' => '1.9.4',
|
||||
], $this->getHeaders()), [
|
||||
'teamId' => $team2,
|
||||
]);
|
||||
|
||||
$this->assertEquals(401, $response['headers']['status-code']);
|
||||
$this->assertEquals(Exception::USER_UNAUTHORIZED, $response['body']['type']);
|
||||
|
||||
$response = $this->client->call(Client::METHOD_PATCH, '/projects/0/team', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $projectId,
|
||||
'x-appwrite-mode' => 'admin',
|
||||
'x-appwrite-response-format' => '1.9.4',
|
||||
], $this->getHeaders()), [
|
||||
'teamId' => $team2,
|
||||
]);
|
||||
|
||||
$this->assertEquals(401, $response['headers']['status-code']);
|
||||
$this->assertEquals(Exception::USER_UNAUTHORIZED, $response['body']['type']);
|
||||
|
||||
$response = $this->client->call(Client::METHOD_GET, '/projects/' . $victimProjectId, array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
'x-appwrite-response-format' => '1.9.4',
|
||||
], $this->getHeaders()));
|
||||
|
||||
$this->assertEquals(200, $response['headers']['status-code']);
|
||||
$this->assertEquals($team1, $response['body']['teamId']);
|
||||
}
|
||||
|
||||
#[Group('projectsCRUD')]
|
||||
|
||||
Reference in new issue
Block a user