Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

📧 fix: Normalize Token Emails on Write, as Every Read Already Does (#15536)

Danny Avila458c473d9eaf
diff --git a/packages/data-schemas/src/methods/token.spec.ts b/packages/data-schemas/src/methods/token.spec.ts
index bc4335bd84..5661aca552 100644
--- a/packages/data-schemas/src/methods/token.spec.ts
+++ b/packages/data-schemas/src/methods/token.spec.ts
@@ -1105,3 +1105,88 @@ describe('Token Methods - Detailed Tests', () => {
});
});
});
+
+describe('Token email normalization', () => {
+ const userId = new mongoose.Types.ObjectId();
+
+ const createFor = (email: string) =>
+ methods.createToken({
+ userId,
+ email,
+ token: 'hashed-token-value',
+ expiresIn: 3600,
+ });
+
+ it('stores the email normalized so reads can find it again', async () => {
+ await createFor('[email protected]');
+
+ const stored = await Token.findOne({ userId });
+ expect(stored?.email).toBe('[email protected]');
+ });
+
+ it('trims surrounding whitespace on write', async () => {
+ await createFor(' [email protected] ');
+
+ const stored = await Token.findOne({ userId });
+ expect(stored?.email).toBe('[email protected]');
+ });
+
+ it('finds a token created with mixed case, whatever case the lookup uses', async () => {
+ await createFor('[email protected]');
+
+ for (const lookup of ['[email protected]', '[email protected]', '[email protected]']) {
+ const found = await methods.findToken({ token: 'hashed-token-value', email: lookup });
+ expect(found).not.toBeNull();
+ }
+ });
+
+ it('finds a token created with padding when the lookup is clean', async () => {
+ await createFor(' [email protected] ');
+
+ const found = await methods.findToken({
+ token: 'hashed-token-value',
+ email: '[email protected]',
+ });
+
+ expect(found).not.toBeNull();
+ });
+
+ it('deletes a token created with mixed case', async () => {
+ await createFor('[email protected]');
+
+ const result = await methods.deleteTokens({ email: '[email protected]' });
+
+ expect(result.deletedCount).toBe(1);
+ expect(await Token.countDocuments({ userId })).toBe(0);
+ });
+
+ it('leaves a token written without an email alone', async () => {
+ await methods.createToken({
+ userId,
+ token: 'no-email-token',
+ expiresIn: 3600,
+ });
+
+ const stored = await Token.findOne({ userId });
+ expect(stored?.email).toBeUndefined();
+ });
+
+ it('leaves an explicitly null email as null, which the read side matches on', async () => {
+ /** `TokenCreateData.email` is `string | undefined`, so a null is only reachable by
+ * writing the model directly — but `findToken` and `deleteTokens` both branch on
+ * `email === null`, so the setters must not coerce it into something else. */
+ await Token.create({
+ userId,
+ email: null,
+ token: 'null-email-token',
+ createdAt: new Date(),
+ expiresAt: new Date(Date.now() + 3600000),
+ });
+
+ const stored = await Token.findOne({ userId });
+ expect(stored?.email).toBeNull();
+
+ const found = await methods.findToken({ token: 'null-email-token', email: null });
+ expect(found).not.toBeNull();
+ });
+});
diff --git a/packages/data-schemas/src/schema/token.ts b/packages/data-schemas/src/schema/token.ts
index 44410036d9..a9dac9c6fe 100644
--- a/packages/data-schemas/src/schema/token.ts
+++ b/packages/data-schemas/src/schema/token.ts
@@ -9,6 +9,13 @@ const tokenSchema: Schema<IToken> = new Schema({
},
email: {
type: String,
+ /** `findToken` and `deleteTokens` both apply `.trim().toLowerCase()` to the email
+ * before querying, so the write side has to match on both counts or a token
+ * created with mixed case or surrounding whitespace can never be found again —
+ * an invite issued to `[email protected]` was unredeemable. `User.email` sets
+ * `lowercase` alone; the read contract here also trims, so this adds `trim`. */
+ lowercase: true,
+ trim: true,
},
type: {
type: String,