mirror of
https://github.com/outline/outline.git
synced 2026-11-22 14:01:41 +03:00
Prevent guests from joining groups and creating custom emoji (#14606)
* Prevent guests from joining groups and creating custom emoji Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Enhance user retrieval with transaction lock Add lock option to User.findByPk for transaction safety Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
12 files changed
+155
-14
No files matched your search
@@ -177,7 +177,7 @@ const EmojiPanel = ({
|
||||
onChange={handleFilter}
|
||||
/>
|
||||
<SkinTonePicker skinTone={skinTone} onChange={handleSkinChange} />
|
||||
{can.update && (
|
||||
{can.createEmoji && (
|
||||
<MenuButton
|
||||
onClick={handleUploadClick}
|
||||
aria-label={t("Upload emoji")}
|
||||
@@ -196,12 +196,14 @@ const EmojiPanel = ({
|
||||
onIconActive={handleEmojiActive}
|
||||
onOverflowChange={setHasMoreBelow}
|
||||
empty={
|
||||
<IconButton
|
||||
onClick={handleUploadClick}
|
||||
aria-label={t("Upload emoji")}
|
||||
>
|
||||
<PlusIcon />
|
||||
</IconButton>
|
||||
can.createEmoji ? (
|
||||
<IconButton
|
||||
onClick={handleUploadClick}
|
||||
aria-label={t("Upload emoji")}
|
||||
>
|
||||
<PlusIcon />
|
||||
</IconButton>
|
||||
) : undefined
|
||||
}
|
||||
/>
|
||||
<IconPreview
|
||||
|
||||
@@ -182,7 +182,7 @@ export default class UsersStore extends Store<User> {
|
||||
);
|
||||
const userIds = groupUsers.map((groupUser) => groupUser.userId);
|
||||
const users = this.activeOrInvited.filter(
|
||||
(user) => !userIds.includes(user.id)
|
||||
(user) => !user.isGuest && !userIds.includes(user.id)
|
||||
);
|
||||
return queriedUsers(users, query);
|
||||
};
|
||||
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
GroupUser,
|
||||
} from "@server/models";
|
||||
import { sequelize } from "@server/storage/database";
|
||||
import { buildGroup, buildUser } from "@server/test/factories";
|
||||
import { buildGroup, buildGuestUser, buildUser } from "@server/test/factories";
|
||||
import groupsSyncer from "./groupsSyncer";
|
||||
|
||||
describe("groupsSyncer", () => {
|
||||
@@ -40,6 +40,27 @@ describe("groupsSyncer", () => {
|
||||
expect(groups.map((g) => g.name).sort()).toEqual(["Design", "Engineering"]);
|
||||
});
|
||||
|
||||
it("should not add guests to synced groups", async () => {
|
||||
const user = await buildGuestUser();
|
||||
const team = await user.$get("team");
|
||||
const authenticationProvider = await AuthenticationProvider.findOne({
|
||||
where: { teamId: user.teamId },
|
||||
rejectOnEmpty: true,
|
||||
});
|
||||
|
||||
const result = await sequelize.transaction(async (transaction) =>
|
||||
groupsSyncer(createContext({ user, transaction, ip }), {
|
||||
user,
|
||||
team: team!,
|
||||
authenticationProvider,
|
||||
externalGroups: [{ id: "ext-1", name: "Engineering" }],
|
||||
})
|
||||
);
|
||||
|
||||
expect(result.membershipsAdded).toEqual(0);
|
||||
expect(await GroupUser.count({ where: { userId: user.id } })).toEqual(0);
|
||||
});
|
||||
|
||||
it("should update internal group name when external name changes", async () => {
|
||||
const user = await buildUser();
|
||||
const team = await user.$get("team")!;
|
||||
|
||||
@@ -123,6 +123,11 @@ async function groupsSyncer(
|
||||
result.groupsCreated++;
|
||||
}
|
||||
|
||||
// Guests cannot be members of groups
|
||||
if (user.isGuest) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Add user to group if not already a member
|
||||
const [, membershipCreated] = await GroupUser.findOrCreateWithCtx(
|
||||
ctx,
|
||||
|
||||
@@ -1,7 +1,26 @@
|
||||
import { buildEmoji, buildTeam, buildUser } from "@server/test/factories";
|
||||
import {
|
||||
buildEmoji,
|
||||
buildGuestUser,
|
||||
buildTeam,
|
||||
buildUser,
|
||||
} from "@server/test/factories";
|
||||
import { serialize } from "./index";
|
||||
|
||||
describe("policies/emoji", () => {
|
||||
it("should allow members to create emoji", async () => {
|
||||
const team = await buildTeam();
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
const abilities = serialize(user, team);
|
||||
expect(abilities.createEmoji).toBeTruthy();
|
||||
});
|
||||
|
||||
it("should not allow guests to create emoji", async () => {
|
||||
const team = await buildTeam();
|
||||
const user = await buildGuestUser({ teamId: team.id });
|
||||
const abilities = serialize(user, team);
|
||||
expect(abilities.createEmoji).toEqual(false);
|
||||
});
|
||||
|
||||
it("should allow the owner to update and delete", async () => {
|
||||
const team = await buildTeam();
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
|
||||
@@ -2,7 +2,13 @@ import { User, Emoji, Team } from "@server/models";
|
||||
import { allow } from "./cancan";
|
||||
import { and, isOwner, isTeamAdmin, isTeamModel, or } from "./utils";
|
||||
|
||||
allow(User, "createEmoji", Team, isTeamModel);
|
||||
allow(User, "createEmoji", Team, (actor, team) =>
|
||||
and(
|
||||
//
|
||||
isTeamModel(actor, team),
|
||||
!actor.isGuest
|
||||
)
|
||||
);
|
||||
|
||||
allow(User, "read", Emoji, isTeamModel);
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
buildAttachment,
|
||||
buildDocument,
|
||||
buildEmoji,
|
||||
buildGuestUser,
|
||||
buildUser,
|
||||
} from "@server/test/factories";
|
||||
import { getTestServer } from "@server/test/support";
|
||||
@@ -197,6 +198,25 @@ describe("#emojis.update", () => {
|
||||
});
|
||||
|
||||
describe("#emojis.create", () => {
|
||||
it("should not allow guests to create an emoji", async () => {
|
||||
const user = await buildGuestUser();
|
||||
const attachment = await buildAttachment({
|
||||
teamId: user.teamId,
|
||||
userId: user.id,
|
||||
documentId: null,
|
||||
});
|
||||
|
||||
const res = await server.post("/api/emojis.create", user, {
|
||||
body: {
|
||||
name: "party",
|
||||
attachmentId: attachment.id,
|
||||
},
|
||||
});
|
||||
|
||||
expect(res.status).toEqual(403);
|
||||
expect(await Emoji.count({ where: { teamId: user.teamId } })).toEqual(0);
|
||||
});
|
||||
|
||||
it("should create an emoji", async () => {
|
||||
const user = await buildUser();
|
||||
const attachment = await buildAttachment({
|
||||
|
||||
@@ -173,6 +173,7 @@ router.post(
|
||||
const { name, attachmentId } = ctx.input.body;
|
||||
const { user } = ctx.state.auth;
|
||||
const { transaction } = ctx.state;
|
||||
authorize(user, "createEmoji", user.team);
|
||||
|
||||
const attachment = await Attachment.findByPk(attachmentId, {
|
||||
transaction,
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
buildAdmin,
|
||||
buildGroup,
|
||||
buildGroupUser,
|
||||
buildGuestUser,
|
||||
} from "@server/test/factories";
|
||||
import { getTestServer } from "@server/test/support";
|
||||
import { GroupPermission } from "@shared/types";
|
||||
@@ -838,6 +839,24 @@ describe("#groups.memberships", () => {
|
||||
});
|
||||
|
||||
describe("#groups.add_user", () => {
|
||||
it("should not allow adding a guest to a group", async () => {
|
||||
const admin = await buildAdmin();
|
||||
const guest = await buildGuestUser({ teamId: admin.teamId });
|
||||
const group = await buildGroup({ teamId: admin.teamId });
|
||||
|
||||
const res = await server.post("/api/groups.add_user", admin, {
|
||||
body: {
|
||||
id: group.id,
|
||||
userId: guest.id,
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(400);
|
||||
expect(body.message).toEqual("Guests cannot be added to groups");
|
||||
const users = await group.$get("users");
|
||||
expect(users.length).toEqual(0);
|
||||
});
|
||||
|
||||
it("should add user to group", async () => {
|
||||
const user = await buildAdmin();
|
||||
const group = await buildGroup({
|
||||
|
||||
@@ -475,9 +475,16 @@ router.post(
|
||||
const actor = ctx.state.auth.user;
|
||||
const { transaction } = ctx.state;
|
||||
|
||||
const user = await User.findByPk(userId, { transaction });
|
||||
const user = await User.findByPk(userId, {
|
||||
transaction,
|
||||
lock: transaction.LOCK.NO_KEY_UPDATE,
|
||||
});
|
||||
authorize(actor, "read", user);
|
||||
|
||||
if (user.isGuest) {
|
||||
throw ValidationError("Guests cannot be added to groups");
|
||||
}
|
||||
|
||||
// Load group with group users for authorization
|
||||
const group = await Group.findByPk(id, {
|
||||
transaction,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { faker } from "@faker-js/faker";
|
||||
import { EmailDisplay, Plan, TeamPreference, UserRole } from "@shared/types";
|
||||
import ConfirmUpdateEmail from "@server/emails/templates/ConfirmUpdateEmail";
|
||||
import { Team, TeamDomain } from "@server/models";
|
||||
import { GroupUser, Team, TeamDomain } from "@server/models";
|
||||
import {
|
||||
buildTeam,
|
||||
buildAdmin,
|
||||
@@ -9,6 +9,8 @@ import {
|
||||
buildUser,
|
||||
buildInvite,
|
||||
buildViewer,
|
||||
buildGroup,
|
||||
buildGroupUser,
|
||||
} from "@server/test/factories";
|
||||
import { getTestServer } from "@server/test/support";
|
||||
|
||||
@@ -1336,6 +1338,31 @@ describe("#users.update_role", () => {
|
||||
expect((await user.reload()).role).toEqual(UserRole.Guest);
|
||||
});
|
||||
|
||||
it("should remove group memberships when changing role to guest", async () => {
|
||||
const planSpy = vi
|
||||
.spyOn(Team.prototype, "plan", "get")
|
||||
.mockReturnValue(Plan.Business);
|
||||
onTestFinished(() => planSpy.mockRestore());
|
||||
const team = await buildTeam();
|
||||
const admin = await buildAdmin({ teamId: team.id });
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
const group = await buildGroup({ teamId: team.id });
|
||||
await buildGroupUser({
|
||||
groupId: group.id,
|
||||
teamId: team.id,
|
||||
userId: user.id,
|
||||
});
|
||||
|
||||
const res = await server.post("/api/users.update_role", admin, {
|
||||
body: {
|
||||
id: user.id,
|
||||
role: UserRole.Guest,
|
||||
},
|
||||
});
|
||||
expect(res.status).toEqual(200);
|
||||
expect(await GroupUser.count({ where: { userId: user.id } })).toEqual(0);
|
||||
});
|
||||
|
||||
it("should promote", async () => {
|
||||
const team = await buildTeam();
|
||||
const admin = await buildAdmin({ teamId: team.id });
|
||||
|
||||
@@ -16,7 +16,7 @@ import auth from "@server/middlewares/authentication";
|
||||
import { rateLimiter } from "@server/middlewares/rateLimiter";
|
||||
import { transaction } from "@server/middlewares/transaction";
|
||||
import validate from "@server/middlewares/validate";
|
||||
import { User, Team } from "@server/models";
|
||||
import { GroupUser, User, Team } from "@server/models";
|
||||
import {
|
||||
buildWhere,
|
||||
combineFilters,
|
||||
@@ -413,6 +413,20 @@ async function updateRole(ctx: APIContext<T.UsersChangeRoleReq>) {
|
||||
}
|
||||
|
||||
await user.updateWithCtx(ctx, { role }, { name });
|
||||
|
||||
// Guests cannot be members of groups, remove any existing memberships
|
||||
if (role === UserRole.Guest) {
|
||||
const groupUsers = await GroupUser.unscoped().findAll({
|
||||
where: { userId: user.id },
|
||||
transaction,
|
||||
lock: transaction.LOCK.UPDATE,
|
||||
});
|
||||
|
||||
for (const groupUser of groupUsers) {
|
||||
await groupUser.destroyWithCtx(ctx, { name: "remove_user" });
|
||||
}
|
||||
}
|
||||
|
||||
const includeDetails = !!can(actor, "readDetails", user);
|
||||
|
||||
ctx.body = {
|
||||
|
||||
Reference in new issue
Block a user