mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-17 07:30:41 +09:00
Drop subscriptions when a user loses permission for them
The permission for a subscription was only checked when subscribing, so a control connection kept receiving events after the user's roles or role permissions changed to no longer allow it. Add SubscriptionController.unsubscribeUnpermitted which rechecks every subscription of a link and removes the ones the user is no longer permitted, and call it for the affected control connections from userPermissionsUpdated and rolesUpdated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ce6bfe282f
commit
beec2f523a
4 files changed
+103
No files matched your search
@@ -1086,6 +1086,7 @@ export default class Controller {
|
||||
userPermissionsUpdated(user: Readonly<User>) {
|
||||
for (let controlConnection of this.wsServer.controlConnections.values()) {
|
||||
if (controlConnection.user === user) {
|
||||
this.subscriptions.unsubscribeUnpermitted(controlConnection, user);
|
||||
controlConnection.send(
|
||||
new lib.AccountUpdateEvent([...user.roles].map(r => ({
|
||||
name: r.name,
|
||||
@@ -1104,6 +1105,7 @@ export default class Controller {
|
||||
for (const role of roles) {
|
||||
for (let controlConnection of this.wsServer.controlConnections.values()) {
|
||||
if (controlConnection.user.roles.has(role)) {
|
||||
this.subscriptions.unsubscribeUnpermitted(controlConnection, controlConnection.user);
|
||||
controlConnection.send(
|
||||
new lib.AccountUpdateEvent(
|
||||
[...controlConnection.user.roles].map(r => ({
|
||||
|
||||
@@ -329,6 +329,31 @@ export class SubscriptionController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsubscribe a link from all events the user no longer has permission to subscribe to.
|
||||
* Used when the permissions of a user changes.
|
||||
* @param link - Link to check the subscriptions of.
|
||||
* @param user - User the link is authenticated as.
|
||||
*/
|
||||
unsubscribeUnpermitted(link: Link, user: IUser) {
|
||||
for (const [eventName, eventData] of this._events) {
|
||||
for (const [addressIndex, subscriber] of eventData.subscriptions) {
|
||||
if (subscriber.link !== link) {
|
||||
continue;
|
||||
}
|
||||
const request = new SubscriptionRequest(eventName, "subscribe", 0, subscriber.filters);
|
||||
const message = new MessageRequest(
|
||||
0, subscriber.address, new Address(Address.controller, 0), "SubscriptionRequest", request.toJSON(),
|
||||
);
|
||||
try {
|
||||
SubscriptionRequest.permission(user, message);
|
||||
} catch {
|
||||
eventData.subscriptions.delete(addressIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle incoming subscription requests on a link
|
||||
* @param link - Link message was received on
|
||||
|
||||
Reference in new issue
Block a user