From 39214431f4b694a098f6417309af1ed9c857a2f1 Mon Sep 17 00:00:00 2001 From: Hazel K Date: Mon, 7 Oct 2024 11:45:58 -0400 Subject: [PATCH] check for host --- .../backend/src/misc/is-system-account.ts | 9 ++++- .../test/unit/misc/is-system-account.ts | 37 ++++++++++++------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/packages/backend/src/misc/is-system-account.ts b/packages/backend/src/misc/is-system-account.ts index 0b699944b3..a34882f494 100644 --- a/packages/backend/src/misc/is-system-account.ts +++ b/packages/backend/src/misc/is-system-account.ts @@ -3,9 +3,14 @@ * SPDX-License-Identifier: AGPL-3.0-only */ +interface UserLike { + readonly username: string; + readonly host: string | null; +} + /** * Checks if the given user represents a system account, such as instance.actor. */ -export function isSystemAccount(user: { readonly username: string }): boolean { - return user.username.includes('.'); +export function isSystemAccount(user: UserLike): boolean { + return user.host == null && user.username.includes('.'); } diff --git a/packages/backend/test/unit/misc/is-system-account.ts b/packages/backend/test/unit/misc/is-system-account.ts index 045fe04477..d0ab152630 100644 --- a/packages/backend/test/unit/misc/is-system-account.ts +++ b/packages/backend/test/unit/misc/is-system-account.ts @@ -7,30 +7,41 @@ import { isSystemAccount } from '@/misc/is-system-account.js'; describe(isSystemAccount, () => { it('should return true for instance.actor', () => { - expect(isSystemAccount({ username: 'instance.actor' })).toBeTruthy(); + expect(isSystemAccount({ username: 'instance.actor', host: null })).toBeTruthy(); }); it('should return true for relay.actor', () => { - expect(isSystemAccount({ username: 'relay.actor' })).toBeTruthy(); + expect(isSystemAccount({ username: 'relay.actor', host: null })).toBeTruthy(); }); it('should return true for any username with a dot', () => { - expect(isSystemAccount({ username: 'some.user' })).toBeTruthy(); - expect(isSystemAccount({ username: 'some.' })).toBeTruthy(); - expect(isSystemAccount({ username: '.user' })).toBeTruthy(); - expect(isSystemAccount({ username: '.' })).toBeTruthy(); + expect(isSystemAccount({ username: 'some.user', host: null })).toBeTruthy(); + expect(isSystemAccount({ username: 'some.', host: null })).toBeTruthy(); + expect(isSystemAccount({ username: '.user', host: null })).toBeTruthy(); + expect(isSystemAccount({ username: '.', host: null })).toBeTruthy(); }); it('should return true for usernames with multiple dots', () => { - expect(isSystemAccount({ username: 'some.user.account' })).toBeTruthy(); - expect(isSystemAccount({ username: '..' })).toBeTruthy(); + expect(isSystemAccount({ username: 'some.user.account', host: null })).toBeTruthy(); + expect(isSystemAccount({ username: '..', host: null })).toBeTruthy(); }); it('should return false for usernames without a dot', () => { - expect(isSystemAccount({ username: 'instance_actor' })).toBeFalsy(); - expect(isSystemAccount({ username: 'instanceactor' })).toBeFalsy(); - expect(isSystemAccount({ username: 'relay_actor' })).toBeFalsy(); - expect(isSystemAccount({ username: 'relayactor' })).toBeFalsy(); - expect(isSystemAccount({ username: '' })).toBeFalsy(); + expect(isSystemAccount({ username: 'instance_actor', host: null })).toBeFalsy(); + expect(isSystemAccount({ username: 'instanceactor', host: null })).toBeFalsy(); + expect(isSystemAccount({ username: 'relay_actor', host: null })).toBeFalsy(); + expect(isSystemAccount({ username: 'relayactor', host: null })).toBeFalsy(); + expect(isSystemAccount({ username: '', host: null })).toBeFalsy(); + }); + + it('should return false for users from another instance', () => { + expect(isSystemAccount({ username: 'instance.actor', host: 'example.com' })).toBeFalsy(); + expect(isSystemAccount({ username: 'relay.actor', host: 'example.com' })).toBeFalsy(); + expect(isSystemAccount({ username: 'some.user', host: 'example.com' })).toBeFalsy(); + expect(isSystemAccount({ username: 'some.', host: 'example.com' })).toBeFalsy(); + expect(isSystemAccount({ username: '.user', host: 'example.com' })).toBeFalsy(); + expect(isSystemAccount({ username: '.', host: 'example.com' })).toBeFalsy(); + expect(isSystemAccount({ username: 'some.user.account', host: 'example.com' })).toBeFalsy(); + expect(isSystemAccount({ username: '..', host: 'example.com' })).toBeFalsy(); }); });