Skip to content

Commit cc6754e

Browse files
committed
fix(auth): the development seed mark is staged only for a creation that proved the seed's own provisioning ticket
The operator class is wider than the seed: an administrator's create-user, a bulk import and directory provisioning earn it too. The mark now keys on the proven ticket alone, so `os dev`'s admin and each `demoStaff` account are marked and an administrator-created account is not. Pinned: an account an administrator creates in development carries no mark and signs in outside development mode; the phone-number password sign-in refuses the seed password. Docs and changeset: the operator reset states exactly what it checks, its reach over a host secondary storage for reset links and sessions alike with the remedy, and the release's present limit with its follow-up; the ADR-0087 marker says a system column is added. The verifier's fail-closed answer is commented with its effect on the sign-in lock. Claude-Session: https://claude.ai/code/session_01QSeKYn5CmmdShCJifm6WYj Co-authored-by: Claude <noreply@anthropic.com>
1 parent 222a618 commit cc6754e

7 files changed

Lines changed: 83 additions & 24 deletions

File tree

‎.changeset/dev-seed-credential-production-refusal.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fix(auth)!: a server outside development mode refuses a development seed credent
99
Clause-②: yes (narrowing) — narrowed: on a server whose `NODE_ENV` is not `development`, a password sign-in that submits the development seed password (`admin123`, or the value `OS_SEED_ADMIN_PASSWORD` sets) is refused on every account, and an account the development seed created opens no session while its stored password is still the one it was seeded with. Widened: the `os user set-password` command, the `sys_user.dev_seed_credential` column, and new `@objectstack/plugin-auth` exports `setOperatorPassword`, `findAccountByEmail`, `operatorPasswordRefusal`, `OPERATOR_PASSWORD_MIN_LENGTH`, `OPERATOR_PASSWORD_MAX_LENGTH` and the types `OperatorPasswordResetEngine`, `OperatorPasswordResetOutcome`, `OperatorPasswordResetRefusalReason`.
1010
Metadata-protocol: none
1111

12-
<!-- adr-0087: not-required (no-migration-prescription) No schema key, accepted value or metadata type is added, removed or renamed, and nothing stored is rewritten: the moved answer is whether a server outside development mode accepts a development seed credential at sign-in. The one-line fix for a refused account is in the body: set a new password on the server with os user set-password. -->
12+
<!-- adr-0087: not-required (no-migration-prescription) No authorable key, accepted value or metadata type is added, removed or renamed; a system column is added; nothing stored is rewritten: the moved answer is whether a server outside development mode accepts a development seed credential at sign-in. The one-line fix for a refused account is in the body: set a new password on the server with os user set-password. -->
1313

1414
**BREAKING** for a deployment that serves, outside development mode, a database `os dev` seeded, or any account whose password is the development seed password. Such a sign-in is refused. Set a new password on the server with `os user set-password <email>`; the refusal names the command. A server in development mode (`os dev`) is unchanged.
1515

@@ -20,6 +20,8 @@ Metadata-protocol: none
2020
- **A marked account still on its seed password opens no session**, on any sign-in door (password, emailed link, one-time code, passkey, social or SSO, impersonation), whatever password it was seeded with, `os dev --admin-password` included. A password check never confirms that seed password: a password sign-in answers it as a wrong password.
2121
- **No password change is offered at sign-in.** Whoever signed in first could perform it. The way out is the operator's reset on the server.
2222

23-
**`os user set-password <email>`** sets the account's password in the project database. It reads the new password from a terminal prompt (twice, not echoed) or from standard input with `--password-stdin`, never from the command line, and takes `--database-url` like the other one-shot commands. It refuses a development seed password and a password outside 8 to 128 characters, and writes nothing on a refusal. It deletes the account's outstanding password-reset links and codes from the project database before the new password lands; a server's secondary storage is out of its reach. It clears the account's development seed mark and any sign-in lock, records the change time, and ends every session and revokes every API key of the account, as an administrator's forced reset does. It asks for no password change at the next sign-in.
23+
**`os user set-password <email>`** sets the account's password in the project database. It reads the new password from a terminal prompt (twice, not echoed) or from standard input with `--password-stdin`, never from the command line, and takes `--database-url` like the other one-shot commands. It checks that the new password is not a development seed password and is 8 to 128 characters long; the deployment's own password policy and password history are not applied. It writes nothing on a refusal. It deletes the account's outstanding password-reset links and codes from the project database before the new password lands. It clears the account's development seed mark and any sign-in lock, records the change time, and ends every session and revokes every API key of the account, as an administrator's forced reset does. It asks for no password change at the next sign-in. It writes the project database only: a secondary storage the host configures for the auth service is out of its reach, for the reset links and the sessions alike. With one configured, run the command with the server stopped, or restart the server afterwards; a secondary storage kept outside the server process outlives a restart and is cleared of the account's entries by hand.
2424

2525
**Not detected:** a database seeded before this release with a custom seed password carries no mark and no well-known password. Set a new password with `os user set-password` on each account it seeded before serving it outside development mode.
26+
27+
**Not ended at boot:** this release refuses the development seed password and every new session for a seeded account still on it. Sessions and API keys a seeded account was issued before the server left development mode are ended by `os user set-password`; run it on each seeded account before serving the database. Ending them at boot is follow-up #35184.

‎content/docs/deployment/cli.mdx‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -878,17 +878,22 @@ What one run does:
878878
879879
- **The password never goes on the command line.** It is asked for on a terminal, or read
880880
from standard input.
881-
- **It refuses a password a server would refuse:** a development seed password, or one
882-
shorter than 8 or longer than 128 characters. An address no account holds, and an
883-
account with no password sign-in, are refused too. A refusal writes nothing.
881+
- **It checks two things about the new password:** it is not a development seed password,
882+
and it is 8 to 128 characters long. The deployment's own password policy and password
883+
history are not applied. An address no account holds, and an account with no password
884+
sign-in, are refused too. A refusal writes nothing.
884885
- **It sets the password with the hash every server verifies**, clears the account's
885886
development seed mark and a sign-in lock, and records the change time.
886887
- **It deletes the account's outstanding password-reset links and codes from the project
887-
database** before the new password lands. A server's secondary storage (a host cache) is
888-
out of its reach.
888+
database** before the new password lands.
889889
- **It ends every session and revokes every API key of the account**, as an
890890
administrator's forced reset does: the sessions and keys issued under the old password do
891891
not outlive it. It asks for no password change at the next sign-in.
892+
- **It writes the project database only.** A secondary storage the host configures for the
893+
auth service (a cache that can hold reset links and sessions) is out of its reach, for the
894+
reset links and the sessions alike. With one configured, run the command with the server
895+
stopped, or restart the server afterwards; a secondary storage kept outside the server
896+
process outlives a restart and is cleared of the account's entries by hand.
892897
893898
### Build & Validate
894899

‎content/docs/deployment/self-hosting.mdx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,12 @@ serve a database `os dev` wrote, set each seeded account's password on the
625625
server with [`os user set-password`](/docs/deployment/cli#os-user-set-password)
626626
first; the refusal names that command.
627627

628+
What this release refuses is the seed password and every new session for a
629+
seeded account still on it. Sessions and API keys a seeded account was issued
630+
before the server left development mode are ended by `os user set-password`, so
631+
run it on each seeded account before serving the database; ending them at boot
632+
is follow-up #35184.
633+
628634
### A seeded directory opens no bootstrap window
629635

630636
Both paths above assume the deployment's **first login** is still ahead of it.

‎packages/plugins/plugin-auth/src/auth-manager-membership-admission.ts‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ export async function validateAudienceAdmission(
235235
const provisioningTicket = (ctx as { body?: Record<string, unknown> } | undefined)?.body?.[
236236
AuthManager.OPERATOR_PROVISIONING_TICKET_FIELD
237237
];
238-
const creationClass: AudienceCreationClass = this.isOperatorProvisioning(email, provisioningTicket)
238+
const seedTicketProven = this.isOperatorProvisioning(email, provisioningTicket);
239+
const creationClass: AudienceCreationClass = seedTicketProven
239240
? 'operator'
240241
: classifyCreationMethod(data?.source, {
241242
enterpriseOAuthProviderIds: this.enterpriseOAuthProviderIds(),
@@ -320,12 +321,14 @@ export async function validateAudienceAdmission(
320321
) {
321322
this.stageOwnerVerifiedStamp(email);
322323
}
323-
// An operator-class creation is the development seed's own (`os dev`'s
324-
// admin, a stack's `demoStaff`): only its provisioning ticket earns the
325-
// class. Its seed stamp is staged here, where the ticket is proven, and
326-
// written when the creation's password credential lands
327-
// (`dev-seed-credential.ts`).
328-
if (email && creationClass === 'operator') await stageDevSeedCredential(email);
324+
// The development seed's own creation (`os dev`'s admin, a stack's
325+
// `demoStaff`) is the one that proved the boot's provisioning ticket for
326+
// this address. The operator CLASS is wider (an administrator's
327+
// create-user, a bulk import, directory provisioning all earn it from
328+
// `classifyCreationMethod`), so the mark is staged on the proven ticket
329+
// alone, never on the class. It is written when the creation's password
330+
// credential lands (`dev-seed-credential.ts`).
331+
if (email && seedTicketProven) await stageDevSeedCredential(email);
329332
// Admitted by its invitation alone (`isAdmittedByInvitationAlone`): the
330333
// membership settle does not bind this sign-up while its address is
331334
// unverified (`settleMembership`).

‎packages/plugins/plugin-auth/src/dev-seed-credential-production.test.ts‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ import 'better-auth/plugins/bearer';
4242
import 'better-auth/plugins/custom-session';
4343
import 'better-auth/plugins/organization';
4444
import 'better-auth/plugins/magic-link';
45+
import 'better-auth/plugins/admin';
46+
import 'better-auth/plugins/phone-number';
4547
import '@better-auth/core/context';
4648
import '@better-auth/core/error';
4749

@@ -54,6 +56,8 @@ const SEED_PASSWORD = 'admin123';
5456
const CUSTOM_SEED_PASSWORD = 'Custom!Seed-Passw0rd-42';
5557
const NEW_PASSWORD = 'Operator!Chosen-Passw0rd-7';
5658
const STAFF_EMAIL = 'staff1@demo.example';
59+
const ADMIN_CREATED_EMAIL = 'colleague@company.example';
60+
const ADMIN_CHOSEN_PASSWORD = 'Admin-Chosen-Pass-29';
5761
const RESET_COMMAND = 'os user set-password';
5862

5963
const ENV_KEYS = [
@@ -150,14 +154,19 @@ function doorsOf(manager: AuthManager) {
150154
* The development boot: `os dev` seeding the admin and one demo account into a
151155
* fresh database. `adminPassword` is `os dev --admin-password`.
152156
*/
153-
async function bootDevelopment(options: { adminPassword?: string } = {}) {
157+
async function bootDevelopment(options: { adminPassword?: string; adminPlugin?: boolean } = {}) {
154158
process.env.NODE_ENV = 'development';
155159
if (options.adminPassword) process.env.OS_SEED_ADMIN_PASSWORD = options.adminPassword;
156160
kernel = new ObjectKernel({ logger: { level: 'silent' } });
157161
await kernel.use(sqliteDriverPlugin());
158162
await kernel.use(new ObjectQLPlugin());
159163
await kernel.use(
160-
new AuthPlugin({ secret: SECRET, baseUrl: ORIGIN, demoStaff: [{ name: 'Demo Staff 1', email: STAFF_EMAIL }] as never }),
164+
new AuthPlugin({
165+
secret: SECRET,
166+
baseUrl: ORIGIN,
167+
demoStaff: [{ name: 'Demo Staff 1', email: STAFF_EMAIL }] as never,
168+
...(options.adminPlugin ? { plugins: { admin: true } } : {}),
169+
} as never),
161170
);
162171
await kernel.use(new SecurityPlugin());
163172
await kernel.bootstrap();
@@ -175,13 +184,14 @@ type Seeded = Awaited<ReturnType<typeof bootDevelopment>>;
175184
* Leave development: the server that serves the database next. The seed's env
176185
* is not passed on, as `os start` does not pass it.
177186
*/
178-
function productionServer(seeded: Seeded, options: { mails?: any[] } = {}) {
187+
function productionServer(seeded: Seeded, options: { mails?: any[]; phoneNumber?: boolean } = {}) {
179188
process.env.NODE_ENV = 'production';
180189
delete process.env.OS_SEED_ADMIN_PASSWORD;
181190
const manager = new AuthManager({
182191
secret: SECRET,
183192
baseUrl: ORIGIN,
184193
dataEngine: seeded.ql as never,
194+
...(options.phoneNumber ? { plugins: { phoneNumber: true } } : {}),
185195
...(options.mails
186196
? {
187197
plugins: { magicLink: true },
@@ -258,6 +268,25 @@ describe('a server outside development mode refuses a development seed credentia
258268
expect(DEV_SEED_CREDENTIAL_FIELD in row).toBe(false);
259269
});
260270

271+
it('an account an administrator creates in development carries no seed stamp, and signs in outside development mode', async () => {
272+
const seeded = await bootDevelopment({ adminPlugin: true });
273+
const api: any = await seeded.manager.getApi();
274+
expect(typeof api.createUser, 'the admin plugin serves create-user on this boot').toBe('function');
275+
// The administrator's create-user, as the `/admin/create-user` route drives it.
276+
await api.createUser({ body: { email: ADMIN_CREATED_EMAIL, name: 'Admin Created', password: ADMIN_CHOSEN_PASSWORD } });
277+
const createdId = await seeded.userId(ADMIN_CREATED_EMAIL);
278+
expect(createdId, 'the administrator-created account exists').toBeTypeOf('string');
279+
expect(await isStamped(seeded, createdId)).toBe(false);
280+
// The seed's own accounts on the same boot stay marked.
281+
expect(await isStamped(seeded, await seeded.userId(SEED_EMAIL))).toBe(true);
282+
expect(await isStamped(seeded, await seeded.userId(STAFF_EMAIL))).toBe(true);
283+
284+
const server = productionServer(seeded);
285+
const res = await server.doors.signIn(ADMIN_CREATED_EMAIL, ADMIN_CHOSEN_PASSWORD);
286+
expect(res.status, await res.clone().text()).toBe(200);
287+
expect(await server.doors.sessionUser(cookieHeader(res))).toBe(createdId);
288+
});
289+
261290
it('refuses a sign-in that submits the seed password to the seed admin, naming the operator reset command, and opens no session', async () => {
262291
const seeded = await bootDevelopment();
263292
const adminId = await seeded.userId(SEED_EMAIL);
@@ -284,6 +313,13 @@ describe('a server outside development mode refuses a development seed credentia
284313
expect(unknown).toEqual(known);
285314
});
286315

316+
it('refuses the seed password on the phone-number password sign-in, naming the operator reset command', async () => {
317+
const seeded = await bootDevelopment();
318+
const server = productionServer(seeded, { phoneNumber: true });
319+
const answer = await answerOf(await server.doors.post('/sign-in/phone-number', { phoneNumber: '+15555550123', password: SEED_PASSWORD }));
320+
expectSeedPasswordRefusal(answer);
321+
});
322+
287323
it('refuses the seed password on an account that carries no seed stamp, as a database seeded before the stamp does', async () => {
288324
const seeded = await bootDevelopment();
289325
const adminId = await seeded.userId(SEED_EMAIL);

‎packages/plugins/plugin-auth/src/dev-seed-credential.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,11 @@
5656
* through any door, writes a new salted hash and the stamp stops matching.
5757
*
5858
* It is written once, when the seed creates the account, in development: the
59-
* audience gate (`validateAudienceAdmission`) stages it for an operator-class
60-
* creation, the class only the seed's own provisioning ticket earns
61-
* ({@link stageDevSeedCredential}), and `account.create.after` writes it when
59+
* audience gate (`validateAudienceAdmission`) stages it for a creation that
60+
* proved the seed's own provisioning ticket for its address, and no other
61+
* (an administrator's create-user, a bulk import and directory provisioning
62+
* share the operator class, never the ticket; {@link stageDevSeedCredential}),
63+
* and `account.create.after` writes it when
6264
* that creation's password credential lands ({@link stampDevSeedCredential}).
6365
*
6466
* ## What it does not detect
@@ -228,6 +230,10 @@ export function withDevSeedPasswordRefusal(
228230
);
229231
return false;
230232
} catch {
233+
// Fail closed, on purpose: when the mark cannot be read, a correct
234+
// password is answered as a wrong one rather than accepted unchecked.
235+
// That answer is a failed sign-in like any other, so it counts toward
236+
// the account's sign-in lock.
231237
return false;
232238
}
233239
},
@@ -306,8 +312,8 @@ const stagedSeedCreation = defineRequestState<{ email: string } | undefined>(()
306312

307313
/**
308314
* Stage the stamp for the creation running in this request: called by the
309-
* audience gate for an operator-class creation, the class only the
310-
* development seed's provisioning ticket earns. Nothing is staged outside
315+
* audience gate for a creation that proved the development seed's own
316+
* provisioning ticket (`os dev`'s admin, a stack's `demoStaff`). Nothing is staged outside
311317
* development mode. Never throws.
312318
*/
313319
export async function stageDevSeedCredential(email: string): Promise<void> {

‎packages/plugins/plugin-auth/src/operator-password-reset.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
*
1212
* ## What one reset does
1313
*
14-
* - **Refuses a password a server would refuse.** A development seed password
15-
* is never set, and the length is held to better-auth's bounds (8 to 128).
14+
* - **Checks two things about the new password:** it is not a development
15+
* seed password, and its length is within better-auth's default bounds (8
16+
* to 128). The deployment's own password policy and history are not applied.
1617
* - **Writes the account's password credential** (`sys_account`, provider
1718
* `credential`) with better-auth's own hash, the one every server verifies.
1819
* An account with no password credential is refused: this sets a password,

0 commit comments

Comments
 (0)