📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
migrate to SECRET_KEY_BASE from separate JWT_* variables
Blue Mouse00c65089239d
diff --git a/backend/.env.example b/backend/.env.exampleindex c8ecb2bae..4acd54a32 100644--- a/backend/.env.example+++ b/backend/.env.example@@ -4,9 +4,9 @@ TWO_FACTOR_AUTHENTICATION_APP_NAME=Swetrix# AppCLIENT_URL='http://localhost:3000'-# JWT Tokens-JWT_ACCESS_TOKEN_SECRET='jwt-access-token-secret'-JWT_REFRESH_TOKEN_SECRET='jwt-refresh-token-secret'+# Secret key base that will be used for stuff like JWT auth tokens+# Can be generated via: openssl rand -base64 48+SECRET_KEY_BASE=# Time in seconds# 30 minutesdiff --git a/backend/Dockerfile b/backend/Dockerfileindex 45d87a179..8462ed110 100644--- a/backend/Dockerfile+++ b/backend/Dockerfile@@ -14,8 +14,7 @@ LABEL maintainer="swetrix.com <[email protected]>"# System and security configsENV TZ=UTC \NODE_ENV=production \- JWT_ACCESS_TOKEN_SECRET=jwt-access-token-secret \- JWT_REFRESH_TOKEN_SECRET=jwt-refresh-token-secret+ SECRET_KEY_BASE=secret-key-base-tokens# Service connectionsENV REDIS_HOST=localhost \diff --git a/backend/apps/cloud/src/auth/auth.service.ts b/backend/apps/cloud/src/auth/auth.service.tsindex a5f8ec902..bccca1808 100644--- a/backend/apps/cloud/src/auth/auth.service.ts+++ b/backend/apps/cloud/src/auth/auth.service.ts@@ -41,6 +41,7 @@ import {JWT_ACCESS_TOKEN_SECRET,JWT_ACCESS_TOKEN_LIFETIME,JWT_REFRESH_TOKEN_LIFETIME,+ JWT_REFRESH_TOKEN_SECRET,} from '../common/constants'import { getGeoDetails } from '../common/utils'import { TelegramService } from '../integrations/telegram/telegram.service'@@ -425,7 +426,7 @@ export class AuthService {{algorithm: 'HS256',expiresIn: JWT_REFRESH_TOKEN_LIFETIME,- secret: this.configService.get('JWT_REFRESH_TOKEN_SECRET'),+ secret: JWT_REFRESH_TOKEN_SECRET,},)diff --git a/backend/apps/cloud/src/auth/strategies/jwt-refresh-token.strategy.ts b/backend/apps/cloud/src/auth/strategies/jwt-refresh-token.strategy.tsindex ecb72b03b..6a7633362 100644--- a/backend/apps/cloud/src/auth/strategies/jwt-refresh-token.strategy.ts+++ b/backend/apps/cloud/src/auth/strategies/jwt-refresh-token.strategy.ts@@ -1,19 +1,19 @@import { ForbiddenException, Injectable } from '@nestjs/common'-import { ConfigService } from '@nestjs/config'import { PassportStrategy } from '@nestjs/passport'import { Request } from 'express'import { ExtractJwt, Strategy } from 'passport-jwt'import { IJwtPayload } from '../interfaces'+import { JWT_REFRESH_TOKEN_SECRET } from '../../common/constants'@Injectable()export class JwtRefreshTokenStrategy extends PassportStrategy(Strategy,'jwt-refresh-token',) {- constructor(public readonly configService: ConfigService) {+ constructor() {super({jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),- secretOrKey: configService.get('JWT_REFRESH_TOKEN_SECRET'),+ secretOrKey: JWT_REFRESH_TOKEN_SECRET,passReqToCallback: true,})}diff --git a/backend/apps/cloud/src/common/constants.ts b/backend/apps/cloud/src/common/constants.tsindex 28b2b433b..6adcda78a 100644--- a/backend/apps/cloud/src/common/constants.ts+++ b/backend/apps/cloud/src/common/constants.ts@@ -2,7 +2,7 @@ import Redis from 'ioredis'import path from 'path'import _toNumber from 'lodash/toNumber'import 'dotenv/config'-import { hash } from './utils'+import { deriveKey, hash } from './utils'const { PAYPAL_CLIENT_ID, PAYPAL_CLIENT_SECRET, EMAIL_ACTION_ENCRYPTION_KEY } =process.env@@ -22,8 +22,10 @@ redis.defineCommand('countKeysByPattern', {lua: "return #redis.call('keys', ARGV[1])",})+export const JWT_ACCESS_TOKEN_SECRET = deriveKey('access-token')+export const JWT_REFRESH_TOKEN_SECRET = deriveKey('refresh-token')+const {- JWT_ACCESS_TOKEN_SECRET,// 30 daysJWT_REFRESH_TOKEN_LIFETIME = 60 * 60 * 24 * 30,// 30 minutes@@ -198,7 +200,6 @@ export {CAPTCHA_SECRET_KEY_LENGTH,PRODUCTION_ORIGIN,REDIS_SSO_UUID,- JWT_ACCESS_TOKEN_SECRET,NUMBER_JWT_REFRESH_TOKEN_LIFETIME as JWT_REFRESH_TOKEN_LIFETIME,NUMBER_JWT_ACCESS_TOKEN_LIFETIME as JWT_ACCESS_TOKEN_LIFETIME,getRedisUserUsageInfoKey,diff --git a/backend/apps/cloud/src/common/utils.ts b/backend/apps/cloud/src/common/utils.tsindex da31d4ae7..b3e5899b3 100644--- a/backend/apps/cloud/src/common/utils.ts+++ b/backend/apps/cloud/src/common/utils.ts@@ -27,6 +27,20 @@ export const hash = (content: string): string => {.substring(0, 32)}+/**+ * Derives a key from the SECRET_KEY_BASE environment variable using the HKDF algorithm.+ */+export const deriveKey = (+ purpose: 'refresh-token' | 'access-token',+ length = 32,+) => {+ const base = Buffer.from(process.env.SECRET_KEY_BASE || '', 'utf8')+ const info = Buffer.from(purpose, 'utf8')+ return Buffer.from(+ crypto.hkdfSync('sha256', base, '', info, length),+ ).toString('hex')+}+export const generateRandomId = (alphabet: string, size: number) => {const bytes = crypto.randomBytes(size)let id = ''diff --git a/backend/apps/community/src/app.module.ts b/backend/apps/community/src/app.module.tsindex d0231ead0..0106c5825 100644--- a/backend/apps/community/src/app.module.ts+++ b/backend/apps/community/src/app.module.ts@@ -25,11 +25,7 @@ const modules = [}),NodeMailerModule.forRootAsync({useFactory: () => {- const hasSmtpConfig =- !!process.env.SMTP_HOST &&- !!process.env.SMTP_PORT &&- !!process.env.SMTP_USER &&- !!process.env.SMTP_PASSWORD+ const hasSmtpConfig = !!process.env.SMTP_HOST && !!process.env.SMTP_PORTconst defaults = {from: `"Swetrix Community Edition" <${process.env.FROM_EMAIL || '[email protected]'}>`,diff --git a/backend/apps/community/src/auth/auth.service.ts b/backend/apps/community/src/auth/auth.service.tsindex f61fb6fc7..fd369bab6 100644--- a/backend/apps/community/src/auth/auth.service.ts+++ b/backend/apps/community/src/auth/auth.service.ts@@ -28,6 +28,7 @@ import {REDIS_OIDC_SESSION_KEY,redis,IS_REGISTRATION_DISABLED,+ JWT_REFRESH_TOKEN_SECRET,} from '../common/constants'import { UserService } from '../user/user.service'import { User } from '../common/types'@@ -221,7 +222,7 @@ export class AuthService {{algorithm: 'HS256',expiresIn: JWT_REFRESH_TOKEN_LIFETIME,- secret: this.configService.get('JWT_REFRESH_TOKEN_SECRET'),+ secret: JWT_REFRESH_TOKEN_SECRET,},)diff --git a/backend/apps/community/src/auth/strategies/jwt-refresh-token.strategy.ts b/backend/apps/community/src/auth/strategies/jwt-refresh-token.strategy.tsindex ecb72b03b..6a7633362 100644--- a/backend/apps/community/src/auth/strategies/jwt-refresh-token.strategy.ts+++ b/backend/apps/community/src/auth/strategies/jwt-refresh-token.strategy.ts@@ -1,19 +1,19 @@import { ForbiddenException, Injectable } from '@nestjs/common'-import { ConfigService } from '@nestjs/config'import { PassportStrategy } from '@nestjs/passport'import { Request } from 'express'import { ExtractJwt, Strategy } from 'passport-jwt'import { IJwtPayload } from '../interfaces'+import { JWT_REFRESH_TOKEN_SECRET } from '../../common/constants'@Injectable()export class JwtRefreshTokenStrategy extends PassportStrategy(Strategy,'jwt-refresh-token',) {- constructor(public readonly configService: ConfigService) {+ constructor() {super({jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),- secretOrKey: configService.get('JWT_REFRESH_TOKEN_SECRET'),+ secretOrKey: JWT_REFRESH_TOKEN_SECRET,passReqToCallback: true,})}diff --git a/backend/apps/community/src/common/constants.ts b/backend/apps/community/src/common/constants.tsindex fdf070670..2fdb6d2d2 100644--- a/backend/apps/community/src/common/constants.ts+++ b/backend/apps/community/src/common/constants.ts@@ -2,6 +2,7 @@ import Redis from 'ioredis'import _toNumber from 'lodash/toNumber'import 'dotenv/config'+import { deriveKey } from './utils'const redis = new Redis(_toNumber(process.env.REDIS_PORT),@@ -18,8 +19,10 @@ redis.defineCommand('countKeysByPattern', {lua: "return #redis.call('keys', ARGV[1])",})+export const JWT_ACCESS_TOKEN_SECRET = deriveKey('access-token')+export const JWT_REFRESH_TOKEN_SECRET = deriveKey('refresh-token')+const {- JWT_ACCESS_TOKEN_SECRET,// 30 daysJWT_REFRESH_TOKEN_LIFETIME = 60 * 60 * 24 * 30,// 30 minutes@@ -119,7 +122,6 @@ export {IP_REGEX,ORIGINS_REGEX,isDevelopment,- JWT_ACCESS_TOKEN_SECRET,NUMBER_JWT_REFRESH_TOKEN_LIFETIME as JWT_REFRESH_TOKEN_LIFETIME,NUMBER_JWT_ACCESS_TOKEN_LIFETIME as JWT_ACCESS_TOKEN_LIFETIME,TRAFFIC_COLUMNS,diff --git a/backend/apps/community/src/common/utils.ts b/backend/apps/community/src/common/utils.tsindex 37814e0ce..da844d398 100644--- a/backend/apps/community/src/common/utils.ts+++ b/backend/apps/community/src/common/utils.ts@@ -83,6 +83,20 @@ export const hash = (content: string): string => {.substring(0, 32)}+/**+ * Derives a key from the SECRET_KEY_BASE environment variable using the HKDF algorithm.+ */+export const deriveKey = (+ purpose: 'refresh-token' | 'access-token',+ length = 32,+) => {+ const base = Buffer.from(process.env.SECRET_KEY_BASE || '', 'utf8')+ const info = Buffer.from(purpose, 'utf8')+ return Buffer.from(+ crypto.hkdfSync('sha256', base, '', info, length),+ ).toString('hex')+}+export const generateRandomId = (alphabet: string, size: number) => {const bytes = crypto.randomBytes(size)let id = ''