Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(config): add trustedOrigins configuration (#195)

Corentin Thomasset4dd15527c064
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.ts
new file mode 100644
index 00000000..cd290353
--- /dev/null
+++ b/apps/papra-server/src/modules/app/auth/auth.models.test.ts
@@ -0,0 +1,63 @@
+import type { Config } from '../../config/config.types';
+import { describe, expect, test } from 'vitest';
+import { getTrustedOrigins } from './auth.models';
+
+describe('auth models', () => {
+ describe('getTrustedOrigins', () => {
+ test('by default the trusted origins are only the baseUrl', () => {
+ const config = {
+ client: {
+ baseUrl: 'http://localhost:3000',
+ },
+ server: {
+ trustedOrigins: [] as string[],
+ },
+ } as Config;
+
+ const { trustedOrigins } = getTrustedOrigins({ config });
+
+ expect(trustedOrigins).to.deep.equal(['http://localhost:3000']);
+ });
+
+ test('if the user defined a list of trusted origins, it returns the client baseUrl and the trustedOrigins deduplicated', () => {
+ const config = {
+ client: {
+ baseUrl: 'http://localhost:3000',
+ },
+ server: {
+ trustedOrigins: [
+ 'http://localhost:3000',
+ 'http://localhost:3001',
+ 'http://localhost:3001',
+ 'http://localhost:3002',
+ ],
+ },
+ } as Config;
+
+ const { trustedOrigins } = getTrustedOrigins({ config });
+
+ expect(
+ trustedOrigins,
+ ).to.deep.equal([
+ 'http://localhost:3000',
+ 'http://localhost:3001',
+ 'http://localhost:3002',
+ ]);
+ });
+
+ test('if the user disables the trusted origins, by setting the trustedOrigins to *, the trustedOrigins are not enabled', () => {
+ const config = {
+ client: {
+ baseUrl: 'http://localhost:3000',
+ },
+ server: {
+ trustedOrigins: '*',
+ },
+ } as Config;
+
+ const { trustedOrigins } = getTrustedOrigins({ config });
+
+ expect(trustedOrigins).to.equal(undefined);
+ });
+ });
+});
diff --git a/apps/papra-server/src/modules/app/auth/auth.models.ts b/apps/papra-server/src/modules/app/auth/auth.models.ts
index 68b11db0..79295561 100644
--- a/apps/papra-server/src/modules/app/auth/auth.models.ts
+++ b/apps/papra-server/src/modules/app/auth/auth.models.ts
@@ -1,4 +1,6 @@
+import type { Config } from '../../config/config.types';
import type { Context } from '../server.types';
+import { uniq } from 'lodash-es';
import { createError } from '../../shared/errors/errors';
export function getUser({ context }: { context: Context }) {
@@ -24,3 +26,12 @@ export function getSession({ context }: { context: Context }) {
return { session };
}
+
+export function getTrustedOrigins({ config }: { config: Config }) {
+ const { baseUrl } = config.client;
+ const { trustedOrigins } = config.server;
+
+ return {
+ trustedOrigins: trustedOrigins === '*' ? undefined : uniq([baseUrl, ...trustedOrigins]),
+ };
+}
diff --git a/apps/papra-server/src/modules/app/auth/auth.services.ts b/apps/papra-server/src/modules/app/auth/auth.services.ts
index 9e4e5318..f6076e40 100644
--- a/apps/papra-server/src/modules/app/auth/auth.services.ts
+++ b/apps/papra-server/src/modules/app/auth/auth.services.ts
@@ -6,6 +6,7 @@ import { betterAuth } from 'better-auth';
import { drizzleAdapter } from 'better-auth/adapters/drizzle';
import { createLogger } from '../../shared/logger/logger';
import { usersTable } from '../../users/users.table';
+import { getTrustedOrigins } from './auth.models';
import { accountsTable, sessionsTable, verificationsTable } from './auth.tables';
export type Auth = ReturnType<typeof getAuth>['auth'];
@@ -25,10 +26,12 @@ export function getAuth({
}) {
const { secret } = config.auth;
+ const { trustedOrigins } = getTrustedOrigins({ config });
+
const auth = betterAuth({
secret,
baseURL: config.server.baseUrl,
- trustedOrigins: [config.client.baseUrl],
+ trustedOrigins,
logger: {
disabled: false,
log: (baseLevel, message) => {
diff --git a/apps/papra-server/src/modules/config/config.schemas.test.ts b/apps/papra-server/src/modules/config/config.schemas.test.ts
index b5a38847..33361bdf 100644
--- a/apps/papra-server/src/modules/config/config.schemas.test.ts
+++ b/apps/papra-server/src/modules/config/config.schemas.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, test } from 'vitest';
-import { booleanishSchema } from './config.schemas';
+import { booleanishSchema, trustedOriginsSchema } from './config.schemas';
describe('config schemas', () => {
describe('booleanishSchema', () => {
@@ -24,4 +24,26 @@ describe('config schemas', () => {
expect(booleanishSchema.parse(2)).toBe(false);
});
});
+
+ describe('trustedOriginsSchema', () => {
+ test('this schema validates and coerces a comma separated string to an array of urls or a literal *', () => {
+ expect(trustedOriginsSchema.parse('*')).toEqual('*');
+ expect(trustedOriginsSchema.parse('http://localhost:3000')).toEqual(['http://localhost:3000']);
+ expect(trustedOriginsSchema.parse('http://localhost:3000,http://localhost:3001')).toEqual([
+ 'http://localhost:3000',
+ 'http://localhost:3001',
+ ]);
+ expect(trustedOriginsSchema.parse([
+ 'http://localhost:3000',
+ 'http://localhost:3001',
+ ])).toEqual([
+ 'http://localhost:3000',
+ 'http://localhost:3001',
+ ]);
+ });
+
+ test('otherwise it throws an error', () => {
+ expect(() => trustedOriginsSchema.parse('non-url')).toThrow();
+ });
+ });
});
diff --git a/apps/papra-server/src/modules/config/config.schemas.ts b/apps/papra-server/src/modules/config/config.schemas.ts
index dc25aca2..5bd7a859 100644
--- a/apps/papra-server/src/modules/config/config.schemas.ts
+++ b/apps/papra-server/src/modules/config/config.schemas.ts
@@ -7,3 +7,9 @@ export const booleanishSchema = z
.toLowerCase()
.transform(x => ['true', '1'].includes(x))
.pipe(z.boolean());
+
+export const trustedOriginsSchema = z.union([
+ z.literal('*'),
+ z.array(z.string().url()),
+ z.string().transform(value => value.split(',')).pipe(z.array(z.string().url())),
+]);
diff --git a/apps/papra-server/src/modules/config/config.ts b/apps/papra-server/src/modules/config/config.ts
index 6180f4db..23154ade 100644
--- a/apps/papra-server/src/modules/config/config.ts
+++ b/apps/papra-server/src/modules/config/config.ts
@@ -17,7 +17,7 @@ import { createLogger } from '../shared/logger/logger';
import { subscriptionsConfig } from '../subscriptions/subscriptions.config';
import { tasksConfig } from '../tasks/tasks.config';
import { trackingConfig } from '../tracking/tracking.config';
-import { booleanishSchema } from './config.schemas';
+import { booleanishSchema, trustedOriginsSchema } from './config.schemas';
export const configDefinition = {
env: {
@@ -33,6 +33,12 @@ export const configDefinition = {
default: 'http://localhost:1221',
env: 'SERVER_BASE_URL',
},
+ trustedOrigins: {
+ doc: 'A comma separated list of origins that are trusted to make requests to the server, it\'ll include the baseUrl by default. You can set it to "*" to allow all origins (not recommended).',
+ schema: trustedOriginsSchema,
+ default: [],
+ env: 'TRUSTED_ORIGINS',
+ },
port: {
doc: 'The port to listen on when using node server',
schema: z.coerce.number().min(1024).max(65535),