The minimalistic document archiving platform.
feat(auth): restrict signup email domains (#1545)
Corentin Thomasset193d860caf36
diff --git a/.changeset/sunny-trains-repeat.md b/.changeset/sunny-trains-repeat.mdnew file mode 100644index 00000000..393f528e--- /dev/null+++ b/.changeset/sunny-trains-repeat.md@@ -0,0 +1,5 @@+---+'@papra/app': patch+---++Added an option to restrict registration to specific email domains using `AUTH_ALLOWED_EMAIL_DOMAINS`. Forbidden domains still take precedence.diff --git a/apps/papra-server/src/modules/app/auth/auth.config.schemas.test.ts b/apps/papra-server/src/modules/app/auth/auth.config.schemas.test.tsindex 30684e51..ec49a690 100644--- a/apps/papra-server/src/modules/app/auth/auth.config.schemas.test.ts+++ b/apps/papra-server/src/modules/app/auth/auth.config.schemas.test.ts@@ -1,55 +1,61 @@import * as v from 'valibot';import { describe, expect, test } from 'vitest';-import { forbiddenEmailDomainsSchema } from './auth.config.schemas';+import { domainListSchema } from './auth.config.schemas';describe('auth config schemas', () => {- describe('forbiddenEmailDomainsSchema', () => {+ describe('domainListSchema', () => {test('create a set of domain from a comma separated string or an array of strings', () => {- expect(v.parse(forbiddenEmailDomainsSchema, 'example.com, test.com')).to.eql(+ expect(v.parse(domainListSchema, 'example.com, test.com')).toEqual(new Set(['example.com', 'test.com']),);- expect(v.parse(forbiddenEmailDomainsSchema, ['example.com', 'test.com'])).to.eql(+ expect(v.parse(domainListSchema, ['example.com', 'test.com'])).toEqual(new Set(['example.com', 'test.com']),);});test('the domains are trimmed and lowercased', () => {- expect(v.parse(forbiddenEmailDomainsSchema, ' Example.com , TEST.com ')).to.eql(+ expect(v.parse(domainListSchema, ' Example.com , TEST.com ')).toEqual(new Set(['example.com', 'test.com']),);- expect(v.parse(forbiddenEmailDomainsSchema, [' Example.com ', ' TEST.com '])).to.eql(+ expect(v.parse(domainListSchema, [' Example.com ', ' TEST.com '])).toEqual(new Set(['example.com', 'test.com']),);});test('empty domains are filtered out', () => {- expect(v.parse(forbiddenEmailDomainsSchema, 'example.com, , test.com, ')).to.eql(+ expect(v.parse(domainListSchema, 'example.com, , test.com, ')).toEqual(new Set(['example.com', 'test.com']),);- expect(v.parse(forbiddenEmailDomainsSchema, ['example.com', '', 'test.com', ' '])).to.eql(+ expect(v.parse(domainListSchema, ['example.com', '', 'test.com', ' '])).toEqual(new Set(['example.com', 'test.com']),);});+ test('empty lists produce an empty set', () => {+ expect(v.parse(domainListSchema, '')).toEqual(new Set());+ expect(v.parse(domainListSchema, ' , , ')).toEqual(new Set());+ expect(v.parse(domainListSchema, [])).toEqual(new Set());+ });+test('duplicate domains are removed', () => {- expect(v.parse(forbiddenEmailDomainsSchema, 'example.com, test.com, example.com')).to.eql(+ expect(v.parse(domainListSchema, 'example.com, test.com, example.com')).toEqual(new Set(['example.com', 'test.com']),);- expect(- v.parse(forbiddenEmailDomainsSchema, ['example.com', 'test.com', 'example.com']),- ).to.eql(new Set(['example.com', 'test.com']));+ expect(v.parse(domainListSchema, ['example.com', 'test.com', 'example.com'])).toEqual(+ new Set(['example.com', 'test.com']),+ );});test('invalid domains are rejected', () => {- expect(() =>- v.parse(forbiddenEmailDomainsSchema, 'example.com, inVAlid-domain, test.com'),- ).to.throw('Invalid domain: Received "invalid-domain"');+ expect(() => v.parse(domainListSchema, 'example.com, inVAlid-domain, test.com')).toThrow(+ 'Invalid domain: Received "invalid-domain"',+ );- expect(() => v.parse(forbiddenEmailDomainsSchema, ['@example.com'])).to.throw(+ expect(() => v.parse(domainListSchema, ['@example.com'])).toThrow('Invalid domain: Received "@example.com"',);});diff --git a/apps/papra-server/src/modules/app/auth/auth.config.schemas.ts b/apps/papra-server/src/modules/app/auth/auth.config.schemas.tsindex a42a1176..4782795e 100644--- a/apps/papra-server/src/modules/app/auth/auth.config.schemas.ts+++ b/apps/papra-server/src/modules/app/auth/auth.config.schemas.ts@@ -1,6 +1,6 @@import * as v from 'valibot';-export const forbiddenEmailDomainsSchema = v.pipe(+export const domainListSchema = v.pipe(v.union([v.pipe(v.string(),diff --git a/apps/papra-server/src/modules/app/auth/auth.config.ts b/apps/papra-server/src/modules/app/auth/auth.config.tsindex 266bcd3e..f252819d 100644--- a/apps/papra-server/src/modules/app/auth/auth.config.ts+++ b/apps/papra-server/src/modules/app/auth/auth.config.ts@@ -1,7 +1,7 @@import type { ConfigDefinition } from 'figue';import * as v from 'valibot';import { booleanishSchema, urlSchema } from '../../config/config.schemas';-import { forbiddenEmailDomainsSchema } from './auth.config.schemas';+import { domainListSchema } from './auth.config.schemas';import { DEFAULT_AUTH_SECRET } from './auth.constants';const customOAuthProviderSchema = v.object({@@ -81,9 +81,15 @@ export const authConfig = {default: ['x-forwarded-for'],env: 'AUTH_IP_ADDRESS_HEADERS',},+ allowedEmailDomains: {+ doc: 'A comma separated list of email domains allowed for registration (e.g. "foo.com,bar.com"). When empty, no allowlist restriction is applied. Domains are matched exactly and case-insensitively. Forbidden domains take precedence over this list.',+ schema: domainListSchema,+ default: [],+ env: 'AUTH_ALLOWED_EMAIL_DOMAINS',+ },forbiddenEmailDomains: {doc: 'A comma separated list of email domains that are forbidden for registration (e.g. "foo.com,bar.com"), if set, it will override the default forbidden domains.',- schema: forbiddenEmailDomainsSchema,+ schema: domainListSchema,default: ['papra.app', 'papra.email', 'owlrelay.email', 'callback.email', 'clb.email'],env: 'AUTH_FORBIDDEN_EMAIL_DOMAINS',},diff --git a/apps/papra-server/src/modules/app/auth/auth.models.test.ts b/apps/papra-server/src/modules/app/auth/auth.models.test.tsindex d0b496d0..4aaa0c39 100644--- a/apps/papra-server/src/modules/app/auth/auth.models.test.ts+++ b/apps/papra-server/src/modules/app/auth/auth.models.test.ts@@ -247,7 +247,7 @@ describe('auth models', () => {});describe('isEmailDomainAllowed', () => {- test('when no forbidden domains are configured, all email domains are allowed', () => {+ test('when no domain restrictions are configured, all email domains are allowed', () => {expect(isEmailDomainAllowed({email: '[email protected]',@@ -263,6 +263,100 @@ describe('auth models', () => {).to.eql(true);});+ test('an empty allowlist imposes no restriction but still respects forbidden domains', () => {+ const allowedEmailDomains = new Set<string>();+ const forbiddenEmailDomains = new Set(['tempmail.com']);++ expect(+ isEmailDomainAllowed({+ email: '[email protected]',+ allowedEmailDomains,+ forbiddenEmailDomains,+ }),+ ).toEqual(true);++ expect(+ isEmailDomainAllowed({+ email: '[email protected]',+ allowedEmailDomains,+ forbiddenEmailDomains,+ }),+ ).toEqual(false);+ });++ test('a non-empty allowlist only permits listed domains', () => {+ const allowedEmailDomains = new Set(['example.com', 'test.com']);++ expect(+ isEmailDomainAllowed({+ email: '[email protected]',+ allowedEmailDomains,+ }),+ ).toEqual(true);++ expect(+ isEmailDomainAllowed({+ email: '[email protected]',+ allowedEmailDomains,+ forbiddenEmailDomains: new Set(['tempmail.com']),+ }),+ ).toEqual(true);++ expect(+ isEmailDomainAllowed({+ email: '[email protected]',+ allowedEmailDomains,+ forbiddenEmailDomains: new Set(),+ }),+ ).toEqual(false);+ });++ test('forbidden domains take precedence over allowed domains', () => {+ expect(+ isEmailDomainAllowed({+ email: '[email protected]',+ allowedEmailDomains: new Set(['example.com']),+ forbiddenEmailDomains: new Set(['example.com']),+ }),+ ).toEqual(false);+ });++ test('an allowed domain does not implicitly allow its subdomains', () => {+ expect(+ isEmailDomainAllowed({+ email: '[email protected]',+ allowedEmailDomains: new Set(['example.com']),+ }),+ ).toEqual(false);+ });++ test('allowlist matching ignores case and surrounding domain whitespace', () => {+ expect(+ isEmailDomainAllowed({+ email: 'user@ EXAMPLE.COM ',+ allowedEmailDomains: new Set(['example.com']),+ }),+ ).toEqual(true);+ });++ test('emails without a domain are rejected when only an allowlist is configured', () => {+ const allowedEmailDomains = new Set(['example.com']);++ expect(+ isEmailDomainAllowed({+ email: 'notanemail',+ allowedEmailDomains,+ }),+ ).toEqual(false);++ expect(+ isEmailDomainAllowed({+ email: 'user@',+ allowedEmailDomains,+ }),+ ).toEqual(false);+ });+test('when an email domain is in the forbidden list, the email is rejected', () => {const forbiddenEmailDomains = new Set(['tempmail.com', 'disposable.email']);diff --git a/apps/papra-server/src/modules/app/auth/auth.models.ts b/apps/papra-server/src/modules/app/auth/auth.models.tsindex 6dad5b44..5a9c0ef6 100644--- a/apps/papra-server/src/modules/app/auth/auth.models.ts+++ b/apps/papra-server/src/modules/app/auth/auth.models.ts@@ -88,15 +88,14 @@ export function isAuthenticationValid({export function isEmailDomainAllowed({email,+ allowedEmailDomains,forbiddenEmailDomains,}: {email: string;- /**- * A set of toLowerCased forbidden email domains (e.g. "papra.app", "callback.email")- */+ allowedEmailDomains?: Set<string>;forbiddenEmailDomains?: Set<string>;}): boolean {- if (isNil(forbiddenEmailDomains) || forbiddenEmailDomains.size === 0) {+ if (!forbiddenEmailDomains?.size && !allowedEmailDomains?.size) {return true;}@@ -106,5 +105,9 @@ export function isEmailDomainAllowed({return false;}- return !forbiddenEmailDomains.has(emailDomain);+ if (forbiddenEmailDomains?.has(emailDomain)) {+ return false;+ }++ return !allowedEmailDomains?.size || allowedEmailDomains.has(emailDomain);}diff --git a/apps/papra-server/src/modules/app/auth/auth.services.ts b/apps/papra-server/src/modules/app/auth/auth.services.tsindex 32cff0be..9c8f7754 100644--- a/apps/papra-server/src/modules/app/auth/auth.services.ts+++ b/apps/papra-server/src/modules/app/auth/auth.services.ts@@ -85,6 +85,7 @@ export function getAuth({if (!isEmailDomainAllowed({email,+ allowedEmailDomains: config.auth.allowedEmailDomains,forbiddenEmailDomains: config.auth.forbiddenEmailDomains,})) {