Dough Git

commit

protect myself from being yeeted

commit
c7d31220455f218d7b0bae1c0cae7b463a2db4b1
author
Clove <[email protected]>
date
2026-11-24 19:38

diff --git a/migrations/0008_protected_owner.sql b/migrations/0008_protected_owner.sql
new file mode 100644
index 0000000..96bd462
--- /dev/null
+++ b/migrations/0008_protected_owner.sql
@@ -0,0 +1,48 @@
+-- migrations/0008_protected_owner.sql
+-- Copyright (c) 2026 Clove Nytrix Doughmination Twilight
+-- Licensed under the DASL-1.2 Licence.
+
+ALTER TABLE users ADD COLUMN protected_owner INTEGER NOT NULL DEFAULT 0;
+
+UPDATE users
+SET protected_owner = 1, disabled = 0
+WHERE username = 'clove';
+
+INSERT OR IGNORE INTO groups (name, description, created_at)
+SELECT 'auth-admin', 'Can manage users, groups and applications on this server.', unixepoch()
+WHERE EXISTS (SELECT 1 FROM users WHERE protected_owner = 1);
+
+INSERT OR IGNORE INTO user_groups (user_id, group_name)
+SELECT id, 'auth-admin' FROM users WHERE protected_owner = 1;
+
+CREATE TRIGGER protect_owner_user_update
+BEFORE UPDATE OF id, protected_owner, disabled ON users
+WHEN OLD.protected_owner = 1
+  AND (NEW.id IS NOT OLD.id OR NEW.protected_owner <> 1 OR NEW.disabled <> 0)
+BEGIN
+  SELECT RAISE(ABORT, 'The protected owner account must stay enabled.');
+END;
+
+CREATE TRIGGER protect_owner_user_delete
+BEFORE DELETE ON users
+WHEN OLD.protected_owner = 1
+BEGIN
+  SELECT RAISE(ABORT, 'The protected owner account cannot be deleted.');
+END;
+
+CREATE TRIGGER protect_owner_admin_delete
+BEFORE DELETE ON user_groups
+WHEN OLD.group_name = 'auth-admin'
+  AND EXISTS (SELECT 1 FROM users WHERE id = OLD.user_id AND protected_owner = 1)
+BEGIN
+  SELECT RAISE(ABORT, 'The protected owner must stay in auth-admin.');
+END;
+
+CREATE TRIGGER protect_owner_admin_update
+BEFORE UPDATE ON user_groups
+WHEN OLD.group_name = 'auth-admin'
+  AND (NEW.user_id <> OLD.user_id OR NEW.group_name <> OLD.group_name)
+  AND EXISTS (SELECT 1 FROM users WHERE id = OLD.user_id AND protected_owner = 1)
+BEGIN
+  SELECT RAISE(ABORT, 'The protected owner must stay in auth-admin.');
+END;
diff --git a/src/api/account.ts b/src/api/account.ts
index 209db1f..fa06756 100644
--- a/src/api/account.ts
+++ b/src/api/account.ts
@@ -308,6 +308,7 @@ account.post("/api/account/sessions/:id/revoke", async (c) => {
 
 account.post("/api/account/delete", async (c) => {
   const user = await me(c);
+  if (user.protected_owner) return fail(c, "The protected owner account cannot be deleted.", 403);
   const b = await body(c);
 
   if (str(b.confirm).trim().toLowerCase() !== user.username.toLowerCase()) {
diff --git a/src/api/admin.ts b/src/api/admin.ts
index d91c1a7..d572cec 100644
--- a/src/api/admin.ts
+++ b/src/api/admin.ts
@@ -132,6 +132,7 @@ admin.get("/api/admin/users/:id", async (c) => {
     emailVerified: Boolean(user.email_verified),
     picture: user.picture,
     disabled: Boolean(user.disabled),
+    protectedOwner: Boolean(user.protected_owner),
     createdAt: user.created_at,
     lastLoginAt: user.last_login_at,
     hasPassword: Boolean(user.password_hash),
@@ -188,6 +189,11 @@ admin.post("/api/admin/users/:id/profile", async (c) => {
 admin.post("/api/admin/users/:id/groups", async (c) => {
   const id = c.req.param("id");
   const groups = strings((await body(c)).groups);
+  const user = await getUser(c.env, id);
+  if (!user) return notFound(c, "user");
+  if (user.protected_owner && !groups.includes(ADMIN_GROUP)) {
+    return fail(c, `The protected owner must stay in ${ADMIN_GROUP}.`, 403);
+  }
   if (id === c.get("user")!.id && !groups.includes(ADMIN_GROUP)) {
     return fail(c, `You can't remove yourself from ${ADMIN_GROUP}. Ask another admin.`);
   }
@@ -273,6 +279,9 @@ admin.post("/api/admin/users/:id/signout", async (c) => {
 
 admin.post("/api/admin/users/:id/disable", async (c) => {
   const id = c.req.param("id");
+  const user = await getUser(c.env, id);
+  if (!user) return notFound(c, "user");
+  if (user.protected_owner) return fail(c, "The protected owner account must stay enabled.", 403);
   if (id === c.get("user")!.id) return fail(c, "You can't disable your own account.");
   await setDisabled(c.env, id, true);
   await revokeLoginLinks(c.env, id);
@@ -293,6 +302,7 @@ admin.post("/api/admin/users/:id/delete", async (c) => {
   const id = c.req.param("id");
   const user = await getUser(c.env, id);
   if (!user) return notFound(c, "user");
+  if (user.protected_owner) return fail(c, "The protected owner account cannot be deleted.", 403);
   if (id === c.get("user")!.id) return fail(c, "You can't delete your own account.");
   if (normaliseUsername(str((await body(c)).confirm)) !== user.username) {
     return fail(c, "Type the username exactly to delete the account.");
diff --git a/src/data/users.ts b/src/data/users.ts
index 9ec0afd..2e1d8d6 100644
--- a/src/data/users.ts
+++ b/src/data/users.ts
@@ -18,6 +18,7 @@ export interface UserRow {
   totp_pending: string | null;
   totp_last_step: number;
   disabled: number;
+  protected_owner: number;
   created_at: number;
   updated_at: number;
   last_login_at: number | null;
@@ -237,7 +238,10 @@ export async function deleteUser(env: Env, id: string): Promise<void> {
 
 export async function setUserGroups(env: Env, userId: string, groups: string[]): Promise<void> {
   await env.DB.batch([
-    env.DB.prepare("DELETE FROM user_groups WHERE user_id = ?").bind(userId),
+    env.DB.prepare(
+      `DELETE FROM user_groups
+       WHERE user_id = ? AND group_name NOT IN (SELECT value FROM json_each(?))`,
+    ).bind(userId, JSON.stringify(groups)),
     ...groups.map((g) =>
       env.DB.prepare(
         "INSERT OR IGNORE INTO user_groups (user_id, group_name) SELECT ?, name FROM groups WHERE name = ?",
diff --git a/web/src/components/GroupChecks.svelte b/web/src/components/GroupChecks.svelte
index 203a4d5..6c22bd1 100644
--- a/web/src/components/GroupChecks.svelte
+++ b/web/src/components/GroupChecks.svelte
@@ -6,7 +6,15 @@
 </script>
 
 <script lang="ts">
-  let { groups, selected = $bindable([]) }: { groups: GroupOption[]; selected?: string[] } = $props();
+  let {
+    groups,
+    selected = $bindable([]),
+    lockedGroup,
+  }: {
+    groups: GroupOption[];
+    selected?: string[];
+    lockedGroup?: string;
+  } = $props();
 </script>
 
 {#if groups.length === 0}
@@ -15,7 +23,7 @@
   <div class="checks">
     {#each groups as g (g.name)}
       <label class="check">
-        <input type="checkbox" value={g.name} bind:group={selected} />
+        <input type="checkbox" value={g.name} bind:group={selected} disabled={g.name === lockedGroup} />
         <span>
           {g.name}
           {#if g.description}<small class="muted"> — {g.description}</small>{/if}
diff --git a/web/src/pages/admin/UserDetail.svelte b/web/src/pages/admin/UserDetail.svelte
index 0943dd0..826f644 100644
--- a/web/src/pages/admin/UserDetail.svelte
+++ b/web/src/pages/admin/UserDetail.svelte
@@ -22,6 +22,7 @@
     emailVerified: boolean;
     picture: string | null;
     disabled: boolean;
+    protectedOwner: boolean;
     createdAt: number;
     lastLoginAt: number | null;
     hasPassword: boolean;
@@ -143,7 +144,11 @@
   <Section title="Groups" description="Sent to apps in the `groups` claim, and used to limit who can open each application.">
     <Notes ok={groupsAction.message} error={groupsAction.error} />
     <form class="stack" onsubmit={saveGroups}>
-      <GroupChecks groups={allGroups} bind:selected={groups} />
+      <GroupChecks
+        groups={allGroups}
+        bind:selected={groups}
+        lockedGroup={user.protectedOwner ? "auth-admin" : undefined}
+      />
       <div>
         <button type="submit" disabled={groupsAction.busy}><Save class="icon" aria-hidden="true" /> Save groups</button>
       </div>
@@ -249,7 +254,7 @@
       >
         Sign out everywhere
       </button>
-      {#if !user.self}
+      {#if !user.self && !user.protectedOwner}
         {#if user.disabled}
           <button type="button" disabled={accessAction.busy} onclick={() => act(accessAction, "/enable")}>Enable account</button>
         {:else}
@@ -266,7 +271,7 @@
     </div>
   </Section>
 
-  {#if !user.self}
+  {#if !user.self && !user.protectedOwner}
     <Section title="Delete account" description="Permanent. Apps keep whatever they stored about this person; a new account will get a new subject id.">
       <Notes error={deleteAction.error} />
       <form class="inline-edit" onsubmit={remove}>