Regenerate all MDC rules with fresh Exa data and December 2025 best practices

- Refreshed all 241 library rules with current best practices
- Updated Exa search results for all libraries with 2025 content
- Fixed --regenerate-all flag logic to properly process all libraries
- Removed duplicate astro entry in rules.json (merged tags)
- Added missing docker.mdc and vim.mdc files
- All rules now include modern patterns and anti-patterns
- Updated with framework-specific recommendations for 2025

Technical improvements:
- Fixed retry_failed_only logic in generate_mdc_files.py
- Improved progress tracking for regeneration scenarios
- Ensured proper handling of --refresh-exa flag
This commit is contained in:
sanjeed5
2025-12-17 22:40:28 +05:30
parent 331fb6ffac
commit 8fbf269085
244 changed files with 66701 additions and 52708 deletions
+215 -190
View File
@@ -1,236 +1,261 @@
---
description: This rule file outlines comprehensive best practices for developing applications using the Clerk library, focusing on security, performance, code organization, and testing to ensure robust and maintainable authentication implementations.
globs: *.js,*.jsx,*.ts,*.tsx
description: This guide establishes definitive best practices for integrating Clerk, focusing on robust security, efficient session management, and secure token handling in line with modern OAuth 2.0 (RFC 9700) and JWT (RFC 8725) standards.
globs: **/*
---
---
## Clerk Library Best Practices
# Clerk Best Practices
This document provides comprehensive guidelines for developing applications using the Clerk library. It covers various aspects, including code organization, common patterns, performance considerations, security, testing, common pitfalls, and tooling, aiming to help developers build robust, secure, and maintainable applications.
Integrating Clerk requires a security-first mindset. Authentication is a primary attack surface; therefore, all Clerk integrations MUST adhere to the strictest security standards. This guide provides actionable, opinionated best practices to ensure your application's identity layer is robust and future-proof.
### 1. Code Organization and Structure
## 1. Security Best Practices
* **Directory Structure:**
* `src/`: Contains the main source code of your application.
* `src/components/`: Reusable UI components. Further categorize components based on their functionality (e.g., `src/components/auth/`, `src/components/profile/`).
* `src/pages/`: (If using a framework like Next.js or Remix) Defines the application's routes.
* `src/lib/clerk/`: Clerk-specific utilities and customizations. This isolates Clerk configuration and extension logic.
* `src/lib/clerk/hooks.ts`: Custom hooks related to Clerk for reusability.
* `src/lib/clerk/utils.ts`: Utility functions for common Clerk tasks (e.g., formatting user data).
* `src/middleware.ts` (or similar): Middleware for authentication checks and redirects.
* `tests/`: Unit, integration, and end-to-end tests.
* `config/`: Configuration files (e.g., Clerk API keys, environment variables).
### 1.1. Enforce Authorization Code Flow with PKCE
* **File Naming Conventions:**
* Components: Use PascalCase (e.g., `UserProfile.jsx`, `SignInForm.tsx`).
* Utilities: Use camelCase (e.g., `formatUserData.js`, `clerkApi.ts`).
* Pages/Routes: Follow the framework's conventions (e.g., `index.js` for the homepage).
* Configuration files: `clerk.config.js`, `environment.config.ts`
**NEVER** use the implicit grant flow. RFC 9700 explicitly deprecates it due to inherent security weaknesses. Always use the Authorization Code Flow with Proof Key for Code Exchange (PKCE). Clerk's client-side SDKs handle PKCE automatically when correctly configured.
* **Module Organization:**
* Group related functionality into modules. For example, all Clerk-related functions and components could be placed within a `src/lib/clerk/` module.
* Use clear and descriptive names for modules and files.
* Favor small, focused modules over large, monolithic ones.
❌ **BAD: Relying on deprecated flows (e.g., implicit grant)**
```javascript
// Avoid configuring Clerk in ways that force implicit grants
// (e.g., older SDK versions or misconfigurations)
// Ensure your Clerk application settings are configured for PKCE.
```
* **Component Architecture:**
* **Presentational Components:** Focus on UI rendering and receive data and callbacks as props.
* **Container Components:** Handle data fetching, state management, and pass data to presentational components.
* Use Higher-Order Components (HOCs) or render props for cross-cutting concerns like authentication checks.
* Utilize custom hooks to abstract Clerk logic and enhance reusability.
✅ **GOOD: Utilize Clerk's Authorization Code Flow with PKCE (default for modern SDKs)**
```javascript
// Clerk's modern SDKs (v5.x+) use PKCE by default.
// Ensure your Clerk application is configured to require PKCE.
// Example: Initializing Clerk in a Next.js app
import { ClerkProvider } from '@clerk/nextjs';
* **Code Splitting:**
* **Route-Based Splitting:** Leverage dynamic imports or framework features to split the application bundle based on routes. This ensures users only download the code necessary for the initial page load.
* **Component-Based Splitting:** Use `React.lazy` or similar techniques to lazy-load components that are not immediately visible or essential.
* Consider using tools like Webpack Bundle Analyzer to identify large dependencies and optimize bundle size.
function MyApp({ Component, pageProps }) {
return (
<ClerkProvider {...pageProps}>
<Component {...pageProps} />
</ClerkProvider>
);
}
```
### 2. Common Patterns and Anti-patterns
### 1.2. Centralize Authentication Logic
* **Design Patterns:**
* **Provider Pattern:** Use the Clerk provider to wrap your application and make Clerk's functionality accessible to all components.
* **Hook Pattern:** Create custom hooks to abstract Clerk logic (e.g., a `useUser` hook to fetch user data).
* **Strategy Pattern:** Implement different authentication strategies (e.g., email/password, social login) using a common interface.
All authentication and authorization checks MUST occur server-side and be handled by a centralized, security-vetted library (Clerk's backend SDKs). Never perform critical authentication decisions solely on the client.
* **Recommended Approaches:**
* Use Clerk's pre-built UI components for common authentication flows (sign-in, sign-up, user profile) to save development time and ensure a consistent user experience.
* Leverage Clerk's middleware for authentication checks on protected routes.
* Use environment variables to store Clerk API keys and other sensitive configuration data.
* Implement role-based access control (RBAC) to manage user permissions.
❌ **BAD: Client-side authorization decisions**
```javascript
// In a React component:
if (clerk.user.publicMetadata.role === 'admin') {
// Render admin-only content
}
// This is easily bypassed by manipulating client-side state.
```
* **Anti-patterns:**
* **Directly Manipulating Clerk's State:** Avoid modifying Clerk's internal state directly. Use the provided API methods and hooks.
* **Storing Sensitive Data in the Client:** Never store Clerk API keys or user credentials in the client-side code.
* **Ignoring Error Handling:** Always handle errors that may occur during authentication flows.
* **Over-Customizing UI Components:** While customization is possible, avoid making excessive changes to Clerk's UI components, as this can lead to maintenance issues and compatibility problems during upgrades.
* **Performing complex operations in middleware:** Keep middleware logic lean and focused on authentication checks and redirects to avoid performance bottlenecks.
✅ **GOOD: Server-side authorization with Clerk's backend SDKs**
```typescript
// In a Next.js API route or server component:
import { getAuth } from '@clerk/nextjs/server';
import { NextApiRequest, NextApiResponse } from 'next';
* **State Management:**
* For simple applications, Clerk's built-in state management may suffice. Avoid mixing it with external state if possible. It can lead to unpredictable behavior.
* For complex applications, consider using a state management library like Redux, Zustand or React Context in conjunction with Clerk, making sure to keep the Clerk state separate, and sync the user object to your state manager after Clerk authenticates the user.
* Use appropriate selectors to access user data from the state.
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { userId, orgRole } = getAuth(req);
* **Error Handling:**
* Use `try...catch` blocks to handle errors that may occur during authentication flows.
* Display user-friendly error messages to the user.
* Log errors to a server-side logging service for debugging and monitoring.
* Implement retry mechanisms for transient errors.
* Centralize error handling logic in a dedicated module for consistency.
if (!userId) {
return res.status(401).json({ error: 'Unauthorized' });
}
### 3. Performance Considerations
if (orgRole !== 'admin') {
return res.status(403).json({ error: 'Forbidden' });
}
* **Optimization Techniques:**
* **Caching:** Cache user data and API responses to reduce the number of requests to Clerk's servers.
* **Code Splitting:** Implement code splitting to reduce the initial bundle size.
* **Lazy Loading:** Lazy-load components and resources that are not immediately needed.
* **Prefetching:** Prefetch data for routes that the user is likely to visit next.
// Proceed with admin-only logic
res.status(200).json({ message: 'Welcome, admin!' });
}
```
* **Memory Management:**
* Avoid creating unnecessary objects or data structures.
* Release resources when they are no longer needed.
* Use memory profiling tools to identify and fix memory leaks.
* Be mindful of the size of user objects stored in the state. Avoid storing unnecessary data.
### 1.3. Securely Store API Keys and Secrets
* **Rendering Optimization:** (If applicable, depends on the UI framework used with Clerk)
* Use memoization techniques to prevent unnecessary re-renders.
* Optimize rendering performance with tools like React Profiler.
* Use virtualization for large lists of data.
API keys, client secrets, and Clerk private keys MUST NEVER be hardcoded or stored directly in source code or configuration files. Use a managed Secrets Manager (e.g., AWS Secrets Manager, Azure Key Vault, HashiCorp Vault) and reference them at runtime via environment variables.
* **Bundle Size Optimization:**
* Use a bundler like Webpack or Parcel to optimize the bundle size.
* Remove unused code and dependencies.
* Use tree shaking to remove dead code.
* Minimize the use of large libraries, use alternative smaller ones.
❌ **BAD: Hardcoding secrets**
```javascript
// .env
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY='pk_test_YOUR_PUBLISHABLE_KEY' // OK for client-side
CLERK_SECRET_KEY='sk_test_YOUR_SECRET_KEY_HARDCODED' // DANGER!
* **Lazy Loading:**
* Lazy-load components using `React.lazy` or dynamic imports.
* Lazy-load images and other assets using a library like `react-lazyload`.
// app/api/webhook.ts
const clerkSecretKey = 'sk_test_YOUR_SECRET_KEY_HARDCODED'; // NEVER DO THIS
```
### 4. Security Best Practices
✅ **GOOD: Using environment variables and a Secrets Manager**
```javascript
// .env.local (for local development, not committed)
CLERK_SECRET_KEY=${CLERK_SECRET_KEY_FROM_SECRETS_MANAGER}
* **Common Vulnerabilities:**
* **Cross-Site Scripting (XSS):** Prevent XSS by sanitizing user input and using a Content Security Policy (CSP).
* **Cross-Site Request Forgery (CSRF):** Implement CSRF protection using tokens.
* **Authentication and Authorization Flaws:** Ensure that authentication and authorization are implemented correctly.
* **Sensitive Data Exposure:** Protect sensitive data by encrypting it and storing it securely.
// In production, CLERK_SECRET_KEY is injected from your Secrets Manager.
// app/api/webhook.ts
const clerkSecretKey = process.env.CLERK_SECRET_KEY;
if (!clerkSecretKey) {
throw new Error('CLERK_SECRET_KEY is not set.');
}
// Use clerkSecretKey for server-side operations
```
* **Input Validation:**
* Validate all user input on both the client and server sides.
* Use a library like `joi` or `yup` to define and enforce validation schemas.
* Escape user input before rendering it to prevent XSS attacks.
### 1.4. Enforce HTTPS Everywhere
* **Authentication and Authorization:**
* Use Clerk's built-in authentication and authorization features.
* Implement role-based access control (RBAC) to manage user permissions.
* Use multi-factor authentication (MFA) to enhance security.
* Enforce strong password policies.
All communication involving authentication credentials or sensitive data MUST occur over HTTPS. This is non-negotiable. Configure your application and infrastructure to redirect all HTTP traffic to HTTPS.
* **Data Protection:**
* Encrypt sensitive data at rest and in transit.
* Use HTTPS to secure communication between the client and server.
* Store API keys and other sensitive data in environment variables.
* Regularly audit your application for security vulnerabilities.
❌ **BAD: Allowing HTTP connections**
```
http://your-app.com/login // Insecure
```
* **Secure API Communication:**
* Use HTTPS for all API requests.
* Validate API responses to prevent data injection attacks.
* Implement rate limiting to prevent denial-of-service attacks.
* Use a secure API gateway to manage API access and security.
✅ **GOOD: Requiring HTTPS**
```
https://your-app.com/login // Secure
```
### 5. Testing Approaches
### 1.5. Implement Strong Password Policies and MFA
* **Unit Testing:**
* Write unit tests for individual components and functions.
* Use a testing framework like Jest or Mocha.
* Mock dependencies to isolate units under test.
* Focus on testing the logic within components, not the rendering details (if applicable).
* Test edge cases and error conditions.
Leverage Clerk's robust password policy configuration to enforce NIST SP 800-63B guidelines:
* Minimum length: 8 characters with MFA, 15 without MFA.
* Maximum length: At least 64 characters (allow passphrases).
* Allow all characters (unicode, whitespace).
* Block common/breached passwords (Clerk integrates with services like HaveIBeenPwned).
* Enable Multi-Factor Authentication (MFA) for all users, especially for high-risk actions or sensitive accounts.
* **Integration Testing:**
* Write integration tests to verify the interaction between different components and modules.
* Test the integration between Clerk and your application's backend.
* Use a testing library like React Testing Library to test component interactions.
❌ **BAD: Weak password policy**
```javascript
// Clerk dashboard configured for:
// Minimum password length: 6
// No MFA required
```
* **End-to-End Testing:**
* Write end-to-end tests to verify the entire application flow.
* Use a testing framework like Cypress or Puppeteer.
* Test common user scenarios, such as signing up, signing in, and accessing protected resources.
* Run end-to-end tests in a CI/CD pipeline.
✅ **GOOD: Robust password policy and MFA**
```javascript
// Clerk dashboard configured for:
// Minimum password length: 15 (or 8 with mandatory MFA)
// MFA required for all users
// Password complexity rules enabled
// Breached password detection enabled
```
* **Test Organization:**
* Organize tests into separate directories based on the type of test (unit, integration, end-to-end).
* Use clear and descriptive names for test files and test cases.
* Follow a consistent testing style.
* Run tests automatically on every commit.
### 1.6. Validate JWTs Rigorously
* **Mocking and Stubbing:**
* Use mocking libraries like Jest's `jest.mock` to mock Clerk's API and dependencies during testing.
* Create stubbed Clerk responses to simulate different scenarios.
* Avoid mocking implementation details. Mock the API calls and return known responses.
When handling Clerk's JWT-based sessions (e.g., in custom backend services), always validate the token's signature, audience (`aud`), issuer (`iss`), and expiration (`exp`). Use strong signing algorithms (RS256 or ES256). Clerk's backend SDKs handle this automatically; avoid manual JWT parsing and validation unless absolutely necessary and with extreme caution.
### 6. Common Pitfalls and Gotchas
❌ **BAD: Trusting JWTs without full validation**
```javascript
// In a custom backend:
const token = req.headers.authorization?.split(' ')[1];
// const decoded = jwt.decode(token); // DANGER: Does not verify signature or claims
// if (decoded.userId) { /* grant access */ }
```
* **Frequent Mistakes:**
* **Misconfiguring Clerk's API Keys:** Ensure that Clerk's API keys are configured correctly in your environment.
* **Incorrectly Implementing Authentication Checks:** Double-check that authentication checks are implemented correctly on protected routes.
* **Failing to Handle Errors:** Always handle errors that may occur during authentication flows.
* **Not Using Environment Variables:** Avoid hardcoding sensitive data in the code. Use environment variables instead.
* **Mixing server-side and client-side rendering approaches:** Understand which components are rendered where, and the impact on auth state.
✅ **GOOD: Using Clerk's verified session/token validation**
```typescript
// In a custom backend (e.g., Express middleware):
import { ClerkExpressWithAuth } from '@clerk/clerk-sdk-node';
* **Edge Cases:**
* **Handling Expired Sessions:** Implement a mechanism to handle expired sessions gracefully.
* **Dealing with Network Errors:** Handle network errors that may occur during API requests.
* **Supporting Different Browsers:** Test your application in different browsers to ensure compatibility.
* **Handling User Deletion:** Handle user deletion and associated data appropriately.
* **Implementing account recovery flows**: Consider how users will reset passwords or regain access to accounts
app.use(
ClerkExpressWithAuth({
jwtKey: process.env.CLERK_JWT_VERIFICATION_KEY, // Use JWKS or Clerk's provided key
}),
(req, res, next) => {
// req.auth will contain validated userId, sessionId, etc.
if (!req.auth.userId) {
return res.status(401).json({ error: 'Unauthorized' });
}
next();
}
);
```
* **Version-Specific Issues:**
* Check the Clerk's changelog for breaking changes and migration guides when upgrading to a new version.
* Be aware of deprecated features and plan for their removal.
* Consult Clerk's documentation for version-specific instructions.
## 2. Session Management and Token Handling
* **Compatibility Concerns:**
* Ensure that Clerk is compatible with the other technologies used in your application (e.g., React, Next.js, Node.js).
* Check for compatibility issues when upgrading dependencies.
### 2.1. Enforce Short-Lived Access Tokens
* **Debugging Strategies:**
* Use browser developer tools to inspect network requests and console logs.
* Use a debugger to step through the code and identify issues.
* Check Clerk's server logs for error messages.
* Enable verbose logging in Clerk's configuration to get more detailed information.
* Use remote debugging tools when debugging on mobile devices.
Configure Clerk sessions to use short-lived access tokens. This minimizes the window of opportunity for token compromise. Refresh tokens should be used to obtain new access tokens.
### 7. Tooling and Environment
❌ **BAD: Long-lived access tokens**
```javascript
// Clerk session configuration allowing very long access token lifetimes (e.g., days)
```
* **Recommended Development Tools:**
* **IDE:** VS Code, WebStorm
* **Bundler:** Webpack, Parcel
* **Testing Framework:** Jest, Mocha
* **Linting:** ESLint
* **Formatting:** Prettier
* **Version Control:** Git
* **Package Manager:** npm, yarn, pnpm
✅ **GOOD: Short-lived access tokens**
```javascript
// Clerk session configuration with access token lifetimes in minutes/hours, not days.
// Clerk handles refresh token rotation automatically for its SDKs.
```
* **Build Configuration:**
* Use a build tool to automate the build process.
* Configure the build tool to optimize the bundle size.
* Use environment variables to configure the build process.
* Generate sourcemaps for debugging production code.
* Automate deployments using CI/CD pipelines.
### 2.2. Keep Clerk SDKs Up-to-Date
* **Linting and Formatting:**
* Use ESLint to enforce code style and identify potential errors.
* Use Prettier to automatically format code.
* Integrate ESLint and Prettier into your IDE and build process.
* Use consistent code style rules across the entire project.
Regularly update your Clerk client and server SDKs to the latest stable versions. This ensures you benefit from security patches, performance improvements, and adherence to the latest best practices. As of early 2025, the CDN release is 5.62.0.
* **Deployment:**
* Deploy your application to a reliable hosting provider (e.g., Vercel, Netlify, AWS).
* Use a CDN to serve static assets.
* Configure HTTPS to secure communication between the client and server.
* Monitor your application for performance and security issues.
❌ **BAD: Sticking to old SDK versions**
```json
// package.json
"dependencies": {
"@clerk/nextjs": "^4.x.x", // Outdated, missing critical updates
}
```
* **CI/CD Integration:**
* Use a CI/CD tool (e.g., GitHub Actions, GitLab CI, Jenkins) to automate the build, test, and deployment process.
* Run unit tests, integration tests, and end-to-end tests in the CI/CD pipeline.
* Automate deployments to staging and production environments.
* Use code review processes to ensure code quality.
✅ **GOOD: Updating Clerk SDKs regularly**
```json
// package.json
"dependencies": {
"@clerk/nextjs": "^5.x.x", // Always use the latest major/minor version
}
// Run `npm update @clerk/nextjs` or `yarn upgrade @clerk/nextjs`
```
By adhering to these best practices, developers can build robust, secure, and maintainable applications using the Clerk library.
## 3. Code Organization and Vulnerability Prevention
### 3.1. Isolate Clerk Initialization
Initialize Clerk at the highest possible level in your application (e.g., `_app.js` in Next.js, `main.tsx` in React). This ensures consistent behavior and avoids re-initialization issues.
❌ **BAD: Multiple Clerk initializations or conditional loading**
```javascript
// In multiple components:
// const clerk = new Clerk('pk_...'); // Inefficient and error-prone
```
✅ **GOOD: Single, top-level Clerk initialization**
```javascript
// pages/_app.tsx (Next.js)
import { ClerkProvider } from '@clerk/nextjs';
import type { AppProps } from 'next/app';
function MyApp({ Component, pageProps }: AppProps) {
return (
<ClerkProvider {...pageProps}>
<Component {...pageProps} />
</ClerkProvider>
);
}
export default MyApp;
```
### 3.2. Implement Automated Security Scans and Code Reviews
Integrate static analysis tools, linters, and dependency scanners into your CI/CD pipeline. Require mandatory code reviews for any changes touching authentication flows, Clerk integration, or security-sensitive areas.
❌ **BAD: Manual, ad-hoc security checks**
```
// Relying solely on manual testing for security vulnerabilities.
```
✅ **GOOD: Automated and human-verified security**
```yaml
# .github/workflows/ci.yml
name: CI/CD Pipeline
jobs:
security-scan:
steps:
- uses: actions/checkout@v3
- name: Run ESLint
run: npm run lint
- name: Run Snyk/Dependabot
run: snyk test # or dependabot/npm_and_yarn
# ... other security checks
```