Repository navigation
Check that a server's group belongs to the current user when creating or moving it. #10908 #10909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dpage
merged 3 commits into
pgadmin-org:master
from
dpage:fix/issue-10908-server-group-owner
Nov 30, 2026
+331
−26
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
web/pgadmin/browser/server_groups/servers/tests/test_server_group_ownership.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| ########################################################################## | ||
| # | ||
| # pgAdmin 4 - PostgreSQL Tools | ||
| # | ||
| # Copyright (C) 2013 - 2026, The pgAdmin Development Team | ||
| # This software is released under the PostgreSQL Licence | ||
| # | ||
| ########################################################################## | ||
|
|
||
| """A server can only be added to, or moved to, a server group that the | ||
| current user owns. This holds for every endpoint that takes a group from | ||
| the request: creating or updating a server, testing a connection, an ad hoc | ||
| workspace connection and a cloud deployment.""" | ||
|
|
||
| import json | ||
| import uuid | ||
|
|
||
| from pgadmin.model import db, Server, ServerGroup, User | ||
| from pgadmin.utils.route import BaseTestGenerator | ||
| from regression.python_test_utils import test_utils as utils | ||
| from .utils import restore_server_fixture_on_cleanup | ||
|
|
||
| NOT_FOUND = 'Server group not found.' | ||
|
|
||
|
|
||
| class ServerGroupOwnershipTestCase(BaseTestGenerator): | ||
|
|
||
| scenarios = [ | ||
| ("Create a server in another user's group is refused", | ||
| dict(action='create', group='other', expected=400)), | ||
| ("Create a server without a group is refused", | ||
| dict(action='create', group=None, expected=400)), | ||
| ("Move a server to another user's group is refused", | ||
| dict(action='update', group='other', expected=400)), | ||
| ("Move a server to another of the user's own groups is accepted", | ||
| dict(action='update', group='own', expected=200)), | ||
| ("Test connection in another user's group is refused", | ||
| dict(action='test_connection', group='other', expected=400)), | ||
| ("Ad hoc connection in another user's group is refused", | ||
| dict(action='adhoc', group='other', expected=400)), | ||
| ("Ad hoc connection without a group uses the user's own group", | ||
| dict(action='adhoc', group=None, expected=None)), | ||
| ("Cloud deployment in another user's group is refused", | ||
| dict(action='cloud', group='other', expected=400)), | ||
| ("Cloud deployment without a group is refused", | ||
| dict(action='cloud', group=None, expected=400)), | ||
| ] | ||
|
|
||
| def setUp(self): | ||
| restore_server_fixture_on_cleanup(self) | ||
| self.server_id = None | ||
| suffix = uuid.uuid4().hex[:12] | ||
| self.name = 'pga_sg_owner_{0}'.format(suffix) | ||
|
|
||
| with self.app.app_context(): | ||
| # Another user, owning a group of its own, and a second group | ||
| # owned by the user running the tests. | ||
| owner = User(username=self.name, | ||
| email='{0}@example.com'.format(self.name), | ||
| active=True, auth_source='internal') | ||
| db.session.add(owner) | ||
| db.session.flush() | ||
| current_owner = ServerGroup.query.filter_by( | ||
| id=utils.SERVER_GROUP).first().user_id | ||
| other = ServerGroup(user_id=owner.id, name=self.name) | ||
| own = ServerGroup(user_id=current_owner, name=self.name) | ||
| db.session.add_all([other, own]) | ||
| db.session.commit() | ||
| self.owner_id = owner.id | ||
| self.gids = {'other': other.id, 'own': own.id} | ||
|
|
||
| def _post(self, url, data): | ||
| return self.tester.post(url, data=json.dumps(data), | ||
| content_type='html/json') | ||
|
|
||
| def _servers_named(self): | ||
| with self.app.app_context(): | ||
| return Server.query.filter_by(name=self.name).count() | ||
|
|
||
| def _group_of(self, sid): | ||
| with self.app.app_context(): | ||
| return Server.query.filter_by(id=sid).first().servergroup_id | ||
|
|
||
| def runTest(self): | ||
| gid = self.gids.get(self.group) | ||
| server = dict(self.server, name=self.name) | ||
|
|
||
| if self.action == 'create': | ||
| response = self._post( | ||
| '/browser/server/obj/{0}/'.format(utils.SERVER_GROUP), | ||
| dict(server, gid=gid)) | ||
| elif self.action == 'update': | ||
| response = self._post( | ||
| '/browser/server/obj/{0}/'.format(utils.SERVER_GROUP), | ||
| server) | ||
| self.assertEqual(response.status_code, 200, response.data) | ||
| self.server_id = json.loads(response.data)['node']['_id'] | ||
| response = self.tester.put( | ||
| '/browser/server/obj/{0}/{1}'.format( | ||
| utils.SERVER_GROUP, self.server_id), | ||
| data=json.dumps({'id': self.server_id, 'gid': gid}), | ||
| content_type='html/json') | ||
| elif self.action == 'test_connection': | ||
| response = self._post( | ||
| '/browser/server/test_connection/{0}/'.format( | ||
| utils.SERVER_GROUP), | ||
| dict(server, gid=gid, password=server['db_password'])) | ||
| elif self.action == 'adhoc': | ||
| data = dict(server_name=self.name, did=server['db'], | ||
| host=server['host'], port=server['port'], | ||
| user=server['username']) | ||
| if gid is not None: | ||
| data['gid'] = gid | ||
| response = self.tester.post( | ||
| '/misc/workspace/adhoc_connect_server', | ||
| data=json.dumps(data), content_type='application/json') | ||
| else: | ||
| response = self._post('/cloud/deploy', dict( | ||
| cloud='aws', db_details={'gid': gid})) | ||
|
|
||
| if self.expected is None: | ||
| # Whether the connection succeeds does not matter here, only | ||
| # the group the new server was filed under. | ||
| with self.app.app_context(): | ||
| row = Server.query.filter_by(name=self.name).first() | ||
| self.assertIsNotNone(row, response.data) | ||
| self.assertEqual(row.servergroup_id, utils.SERVER_GROUP) | ||
| return | ||
|
|
||
| self.assertEqual(response.status_code, self.expected, response.data) | ||
|
|
||
| if self.expected == 200: | ||
| self.assertEqual(self._group_of(self.server_id), gid) | ||
| return | ||
|
|
||
| self.assertEqual(json.loads(response.data)['errormsg'], NOT_FOUND) | ||
| if self.server_id is not None: | ||
| # The server stays in the group it was created in. | ||
| self.assertEqual(self._group_of(self.server_id), | ||
| utils.SERVER_GROUP) | ||
| else: | ||
| self.assertEqual(self._servers_named(), 0) | ||
|
|
||
| def tearDown(self): | ||
| if self.server_id is not None: | ||
| utils.delete_server_with_api(self.tester, self.server_id) | ||
| with self.app.app_context(): | ||
| Server.query.filter_by(name=self.name).delete() | ||
| ServerGroup.query.filter( | ||
| ServerGroup.id.in_(self.gids.values())).delete() | ||
| User.query.filter_by(id=self.owner_id).delete() | ||
| db.session.commit() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.