Skip to content

Security: varshu55/WorkSphere

Security

docs/SECURITY.md

WorkSphere Security Guide

Overview

Security is a core aspect of the WorkSphere platform. The project uses modern authentication, authorization, request validation, and webhook verification mechanisms to help protect user accounts, application resources, and backend services.

WorkSphere primarily relies on Clerk for authentication, Svix for secure webhook verification, Prisma with PostgreSQL for database access, and Zod for request validation. These technologies work together to reduce unauthorized access, validate incoming data, and maintain consistency between external identity providers and the local database.

This document explains the security architecture currently implemented in the repository and provides additional recommendations that contributors can follow when extending the application.

It is intended for:

  • Contributors
  • Maintainers
  • Security reviewers
  • Backend developers
  • DevOps engineers

Security Objectives

The primary security goals of WorkSphere are:

  • Protect authenticated user sessions.
  • Restrict access to protected routes.
  • Verify incoming webhook requests.
  • Validate incoming request payloads.
  • Protect administrator-only functionality.
  • Reduce invalid or malicious input.
  • Secure communication with external services.
  • Encourage secure deployment practices.

Security Architecture

At a high level, WorkSphere follows a layered security model.

Browser / Client
        │
        ▼
Clerk Authentication
        │
        ▼
Next.js Middleware
        │
        ▼
Protected API Routes
        │
        ▼
Request Validation (Zod)
        │
        ▼
Business Logic
        │
        ▼
Prisma ORM
        │
        ▼
PostgreSQL Database

Each layer performs a different responsibility before a request reaches the database.

  • Authentication verifies user identity.
  • Middleware protects restricted routes.
  • Validation checks incoming request data.
  • Authorization limits privileged operations.
  • Database access is handled through Prisma.

This layered approach reduces the likelihood of unauthorized access and invalid data reaching the application's core services.


Authentication

Authentication in WorkSphere is powered by Clerk, which provides secure user authentication, session management, and identity verification.

Instead of implementing a custom authentication system, the project relies on Clerk to manage user identities while the application focuses on authorization and business logic.

The authentication layer is responsible for:

  • User registration
  • User sign in
  • Session management
  • Identity verification
  • Secure session cookies
  • User synchronization through webhooks

This approach reduces the amount of authentication-related code maintained inside the project while relying on a well-tested authentication provider.


Authentication Flow

A typical authentication request follows the sequence below.

User
   │
   ▼
Clerk Sign In
   │
   ▼
Authenticated Session
   │
   ▼
Next.js Middleware
   │
   ▼
Protected Route
   │
   ▼
Application Logic

Every request targeting protected resources passes through the middleware before reaching the application.

If the user is authenticated, the request continues.

Otherwise, access is denied according to Clerk's authentication flow.


Clerk Authentication

WorkSphere uses Clerk as the primary identity provider.

Within the repository, authenticated user information is accessed using Clerk server-side helpers such as:

  • auth()
  • currentUser()

These helpers are used across multiple API routes and server components to obtain the currently authenticated user.

Typical operations include:

  • Reading the authenticated user's ID.
  • Fetching profile information.
  • Synchronizing user records with the local database.
  • Protecting private application resources.

Using Clerk allows authentication logic to remain centralized while reducing custom security code inside the application.


Session-Based Authentication

For browser users, Clerk manages authenticated sessions automatically.

Authenticated requests include the required session information, allowing protected API routes to identify the current user without requiring developers to manually manage session tokens.

This simplifies authentication while reducing the risk of insecure session handling.


User Identification

Many API endpoints retrieve the authenticated user through Clerk before executing protected operations.

Examples include:

  • Favorites
  • Conversations
  • Reservations
  • Analytics
  • Memory
  • Uploads
  • Venue ratings
  • Social features

If authentication fails, the protected operation is not performed.

This prevents unauthorized users from accessing private resources.


Route Protection

Not every endpoint within WorkSphere requires authentication.

Some pages and API routes are intentionally exposed to allow users to access public resources such as the landing page, authentication pages, and webhook endpoints.

All remaining application routes are protected through the project's authentication middleware.

This approach follows the principle of protecting application resources by default while explicitly allowing only trusted public endpoints.


Middleware Protection

WorkSphere uses Clerk Middleware to protect application routes.

The middleware intercepts incoming requests before they reach the application's business logic.

A simplified request flow is shown below.

Incoming Request
        │
        ▼
Next.js Middleware
        │
        ▼
Is Route Public?
   │           │
  Yes         No
   │           │
   ▼           ▼
 Continue   Authentication Required
                 │
                 ▼
          Clerk Verification
                 │
          ┌──────┴──────┐
          │             │
       Success       Failure
          │             │
          ▼             ▼
   Continue Request   Access Denied

This ensures that protected endpoints are not executed until authentication has been successfully completed.


Public Routes

The repository explicitly allows several routes to remain publicly accessible.

Examples include:

  • Home page (/)
  • Sign In
  • Sign Up
  • Privacy Policy
  • Terms of Service
  • Clerk Webhook Endpoint

These routes either provide public information or are required for authentication and webhook synchronization.

The webhook endpoint is intentionally public because requests originate from Clerk rather than authenticated application users.

Although publicly reachable, webhook requests are still protected through Svix signature verification.


Protected Routes

Routes that are not included in the public allow-list require authentication.

Examples include:

  • Dashboard
  • Reservations
  • Favorites
  • Conversations
  • Analytics
  • Venue Rating APIs
  • Upload APIs
  • Memory APIs
  • Social Features

Attempting to access these endpoints without a valid authenticated session will result in the request being rejected before reaching the application's business logic.


Authorization

Authentication verifies who the user is.

Authorization determines what the user is allowed to do.

WorkSphere separates these responsibilities to reduce the risk of unauthorized access.

While authenticated users may access normal application features, certain administrative functionality requires additional authorization checks.


Administrator Access

Administrative endpoints use role-based authorization.

The project verifies the authenticated user's role before granting access to privileged resources.

Supported administrator roles currently include:

  • admin
  • super_admin
  • superadmin

Users without one of these roles are denied access to administrator-only functionality.

This additional verification prevents authenticated users from accessing sensitive administrative operations.


Authorization Flow

Authenticated User
        │
        ▼
Retrieve User Information
        │
        ▼
Read User Role
        │
        ▼
Is Administrator?
   │             │
  Yes            No
   │             │
   ▼             ▼
Allow Access   Return HTTP 403

When authorization fails, the application returns an appropriate error response instead of exposing administrative resources.

This layered approach combines authentication and authorization to provide stronger protection for privileged operations.


JWT Authentication

WorkSphere supports authenticated API access through Clerk-managed sessions.

For browser-based users, authentication is handled automatically through Clerk's session management.

For external API clients or testing tools, authenticated requests can be made using a valid JWT in the standard Authorization header.

Example:

Authorization: Bearer <CLERK_JWT_SESSION_TOKEN>

The JWT represents the authenticated user and allows protected endpoints to identify the request origin.

JWT validation is handled through Clerk's authentication infrastructure rather than custom token verification logic implemented within the application.

This approach reduces the complexity of authentication while relying on an established identity provider.


Authentication Request Flow

Client
   │
   ▼
Authorization Header
   │
   ▼
Clerk Authentication
   │
   ▼
Verified User
   │
   ▼
Protected API

If the token or authenticated session is invalid, protected endpoints are not executed.


API Security

Every protected endpoint should assume that incoming requests are untrusted until authentication and validation have completed.

The project follows several security principles:

  • Authenticate the request.
  • Validate request data.
  • Execute business logic.
  • Return only the required data.

This sequence helps reduce the likelihood of unauthorized access or invalid requests reaching backend services.


Authentication Failures

If authentication cannot be completed successfully, the request is rejected before protected operations are performed.

Typical responses include:

  • Unauthorized requests
  • Forbidden administrator requests
  • Invalid webhook requests

Returning appropriate HTTP status codes makes API behavior more predictable while avoiding unnecessary processing.


Webhook Security

WorkSphere synchronizes user information from Clerk using secure webhooks.

Incoming webhook requests are processed through the dedicated webhook endpoint.

Before any webhook event is accepted, its authenticity is verified using Svix.

This prevents unauthorized third parties from sending forged webhook payloads to the application.


Webhook Verification Flow

Clerk
   │
   ▼
Webhook Request
   │
   ▼
Svix Headers
   │
   ▼
Signature Verification
   │
   ▼
Verified Event
   │
   ▼
Database Synchronization

Only successfully verified webhook events continue to the synchronization process.

Requests that fail verification are rejected immediately.


Svix Headers

The webhook endpoint verifies several headers supplied by Clerk.

These include:

  • svix-id
  • svix-timestamp
  • svix-signature

Each header contributes to the verification process.

If any required header is missing, the request is rejected before processing.


Webhook Secret

Webhook verification depends on the WEBHOOK_SECRET environment variable.

This secret is used to validate incoming webhook signatures.

If the secret is unavailable, webhook verification cannot be performed and the application intentionally stops processing the request.

The webhook secret should always remain confidential and must never be committed to the repository.


Signature Verification

After receiving the request body, WorkSphere verifies the payload using the Svix SDK.

Only events with valid signatures are accepted.

If signature verification fails:

  • The request is rejected.
  • Database operations are skipped.
  • An error response is returned.

This prevents forged or modified webhook payloads from affecting application data.


Supported Webhook Events

The current implementation synchronizes user information from Clerk using three primary webhook events.

These events help keep the local PostgreSQL database consistent with the authentication provider.

Currently supported events include:

Event Purpose
user.created Creates a new user record in the local database.
user.updated Updates user profile information.
user.deleted Removes the user from the local database.

Each event is processed independently after successful webhook verification.


User Creation

When a new user registers through Clerk, a user.created webhook is delivered to the application.

After verifying the webhook signature, WorkSphere creates a corresponding user record inside the database.

Typical information synchronized includes:

  • User ID
  • Email address
  • First name
  • Last name

Keeping a local copy of user information allows the application to associate reservations, conversations, favorites, analytics, and other application data with authenticated users.


User Update

When profile information changes inside Clerk, a user.updated webhook is received.

The application updates the existing database record with the latest information.

Typical updates include:

  • Email changes
  • First name
  • Last name

Synchronizing profile information helps maintain consistency between Clerk and the local database.


User Deletion

When a user account is removed from Clerk, a user.deleted webhook is triggered.

After successful verification, the corresponding user record is deleted from the local database.

Removing unused records helps maintain database consistency while preventing orphaned user entries.


User Synchronization Flow

The synchronization process follows the sequence below.

User Action
      │
      ▼
Clerk
      │
      ▼
Webhook Event
      │
      ▼
Svix Verification
      │
      ▼
Database Operation
      │
      ▼
Synchronization Complete

Each webhook event is verified before any database operation is performed.


Local User Synchronization

Although webhook synchronization is the primary mechanism for keeping user records up to date, WorkSphere also includes a fallback synchronization strategy.

The project provides a helper that ensures a local database record exists for the authenticated user.

This improves reliability in situations where webhook delivery may be delayed or temporarily unavailable.

The synchronization helper performs the following steps:

  1. Check whether the user already exists locally.
  2. If found, return the existing record.
  3. Otherwise, retrieve the authenticated user from Clerk.
  4. Create a new local database record.
  5. Return the synchronized user.

This approach improves resilience without replacing webhook-based synchronization.


Synchronization Flow

Authenticated User
        │
        ▼
Check Local Database
        │
   ┌────┴────┐
   │         │
Exists     Missing
   │         │
   ▼         ▼
Return   Retrieve From Clerk
               │
               ▼
      Create Local User
               │
               ▼
          Continue Request

This additional synchronization layer helps reduce failures caused by temporary webhook delays.


Error Handling

Webhook processing is designed to fail safely.

If an unexpected database error occurs during synchronization, the application records the error without exposing sensitive internal details.

Similarly, invalid webhook requests are rejected before reaching database operations.

This defensive approach helps maintain data integrity while reducing unnecessary webhook retries.


Input Validation and Request Sanitization

WorkSphere validates incoming request data before it reaches application logic or the database.

The project primarily uses Zod schemas to validate API request payloads and ensure that only well-formed data is processed.

This validation layer helps protect the application against malformed requests, unexpected input types, and invalid user data.


Schema-Based Validation

Validation schemas are centralized within the project and reused across multiple API routes.

Examples include validation for:

  • Chat requests
  • Venue creation
  • Venue ratings
  • Conversation creation
  • Messages
  • Favorites
  • Geographic coordinates

Each schema defines acceptable data types, value ranges, required fields, and optional properties.

Typical validation rules include:

  • Minimum and maximum string lengths
  • Numeric range validation
  • Enumeration checks
  • Required object structures
  • Optional field handling

Requests that fail validation are rejected before reaching business logic.


Safe Parsing

The application uses Zod's safeParse() method when validating incoming data.

Using safeParse() prevents unexpected runtime exceptions by returning structured validation results instead of throwing errors.

A typical validation flow is:

Incoming Request
        │
        ▼
Parse JSON Body
        │
        ▼
Zod Validation
        │
   ┌────┴────┐
   │         │
 Valid    Invalid
   │         │
   ▼         ▼
Continue   Return Validation Error

This approach keeps request handling predictable and improves API reliability.


Request Sanitization

Input validation also serves as the application's first layer of request sanitization.

Before data is processed, validation rules help reject:

  • Missing required fields
  • Invalid numeric ranges
  • Unexpected enum values
  • Incorrect data types
  • Oversized text fields

Rejecting invalid input early reduces the likelihood of malformed data reaching the database or downstream services.


Authentication and Account Recovery

Several authentication-related endpoints validate user input before processing requests.

Examples include:

  • Forgot password
  • Password reset
  • OTP verification
  • OTP resend

These endpoints validate request bodies before performing authentication or account recovery operations.

This helps reduce invalid requests while improving overall security and consistency.


Validation Error Handling

When validation fails, the application returns structured error information instead of continuing request processing.

Typical validation failures include:

  • Invalid email format
  • Missing required fields
  • Invalid coordinate values
  • Unsupported enum values
  • Values outside accepted ranges

Returning validation errors early helps API consumers identify incorrect requests while protecting backend services from malformed input.


Security Best Practices

Contributors are encouraged to follow these practices when introducing new endpoints or features.

  • Validate every external request before processing it.
  • Prefer shared validation schemas instead of duplicating logic.
  • Reject malformed input before accessing the database.
  • Keep authentication checks separate from business logic.
  • Avoid exposing sensitive implementation details in error responses.
  • Store secrets using environment variables rather than hardcoding credentials.
  • Verify webhook signatures before performing database operations.
  • Follow the principle of least privilege for administrative functionality.

Applying these practices helps maintain a secure, predictable, and maintainable codebase as the project evolves.


Summary

WorkSphere's security model combines multiple layers of protection rather than relying on a single mechanism.

These layers include:

  • Clerk authentication
  • Middleware-based route protection
  • Svix webhook signature verification
  • JWT-based authenticated requests
  • Role-based administrator authorization
  • Zod request validation
  • Local user synchronization
  • Secure environment variable management

Together, these mechanisms provide a foundation for protecting user accounts, validating requests, and maintaining data integrity throughout the application.


Last updated: July 2026

There aren't any published security advisories