mirror of
https://github.com/better-auth/better-auth.git
synced 2026-07-25 20:15:19 -05:00
168 lines
7.8 KiB
Plaintext
168 lines
7.8 KiB
Plaintext
---
|
||
title: Security
|
||
description: Better Auth security features.
|
||
---
|
||
|
||
This page contains information about security features of Better Auth.
|
||
|
||
|
||
## Password Hashing
|
||
|
||
Better Auth uses the `scrypt` algorithm to hash passwords by default. This algorithm is designed to be memory-hard and CPU-intensive, making it resistant to brute-force attacks. You can customize the password hashing function by setting the `password` option in the configuration. This option should include a `hash` function to hash passwords and a `verify` function to verify them.
|
||
|
||
## Session Management
|
||
|
||
### Session Expiration
|
||
|
||
Better Auth uses secure session management to protect user data. Sessions are stored in the database or a secondary storage, if configured, to prevent unauthorized access. By default, sessions expire after 7 days, but you can customize this value in the configuration. Additionally, each time a session is used, if it reaches the `updateAge` threshold, the expiration date is extended, which by default is set to 1 day.
|
||
|
||
### Session Revocation
|
||
|
||
Better Auth allows you to revoke sessions to enhance security. When a session is revoked, the user is logged out and can no longer access the application. A logged in user can also revoke their own sessions to log out from different devices or browsers.
|
||
|
||
See the [session management](/docs/concepts/session-management) for more details.
|
||
|
||
## CSRF Protection
|
||
|
||
Better Auth includes multiple safeguards to prevent Cross-Site Request Forgery (CSRF) attacks:
|
||
|
||
1. **Avoid simple requests**
|
||
See [Avoiding simple requests](https://developer.mozilla.org/en-US/docs/Web/Security/Attacks/CSRF#avoiding_simple_requests) for more details. Better Auth only allows requests with a non-simple header or a `Content-Type` header of `application/json`.
|
||
|
||
2. **Origin Validation**
|
||
Each request’s `Origin` header is verified to confirm it comes from your application or another explicitly trusted source. Requests from untrusted origins are rejected. By default, Better Auth trusts the base URL of your app, but you can specify additional trusted origins via the `trustedOrigins` configuration option.
|
||
|
||
3. **Secure Cookie Settings**
|
||
Session cookies use the `SameSite=Lax` attribute by default, preventing browsers from sending cookies with most cross-site requests. You can override this behavior using the `defaultCookieAttributes` option.
|
||
|
||
4. **No Mutations on GET Requests (with additional safeguards)**
|
||
`GET` requests are assumed to be read-only and should not alter the application’s state. In cases where a `GET` request must perform a mutation—such as during OAuth callbacks - Better Auth applies extra security measures, including validating `nonce` and `state` parameters to ensure the request’s authenticity.
|
||
|
||
You can skip the CSRF check for all requests by setting the `disableCSRFCheck` option to `true` in the configuration.
|
||
|
||
```typescript
|
||
{
|
||
advanced: {
|
||
disableCSRFCheck: true
|
||
}
|
||
}
|
||
```
|
||
|
||
You can skip the origin check for all requests by setting the `disableOriginCheck` option to `true` in the configuration.
|
||
|
||
```typescript
|
||
{
|
||
advanced: {
|
||
disableOriginCheck: true
|
||
}
|
||
}
|
||
```
|
||
|
||
<Callout type="warning">
|
||
Skipping csrf check will open your application to CSRF attacks. And skipping origin check may open up your application to other security vulnerabilities including open redirects.
|
||
</Callout>
|
||
|
||
## OAuth State and PKCE
|
||
|
||
To secure OAuth flows, Better Auth stores the OAuth state and PKCE (Proof Key for Code Exchange) in the database. The state helps prevent CSRF attacks, while PKCE protects against code injection threats. Once the OAuth process completes, these values are removed from the database.
|
||
|
||
## Cookies
|
||
|
||
Better Auth assigns secure cookies by default when the base URL uses `https`. These secure cookies are encrypted and only sent over secure connections, adding an extra layer of protection. They are also set with the `sameSite` attribute to `lax` by default to prevent cross-site request forgery attacks. And the `httpOnly` attribute is enabled to prevent client-side JavaScript from accessing the cookie.
|
||
|
||
For Cross-Subdomain Cookies, you can set the `crossSubDomainCookies` option in the configuration. This option allows cookies to be shared across subdomains, enabling seamless authentication across multiple subdomains.
|
||
|
||
### Customizing Cookies
|
||
|
||
You can customize cookie names to minimize the risk of fingerprinting attacks and set specific cookie options as needed for additional control. For more information, refer to the [cookie options](/docs/concepts/cookies).
|
||
|
||
Plugins can also set custom cookie options to align with specific security needs. If you're using Better Auth in non-browser environments, plugins offer ways to manage cookies securely in those contexts as well.
|
||
|
||
## Rate Limiting
|
||
|
||
Better Auth includes built-in rate limiting to safeguard against brute-force attacks. Rate limits are applied across all routes by default, with specific routes subject to stricter limits based on potential risk.
|
||
|
||
## IP Address Headers
|
||
|
||
Better Auth uses client IP addresses for rate limiting and security monitoring. By default, it reads the IP address from the standard `X-Forwarded-For` header. However, you can configure a specific trusted header to ensure accurate IP address detection and prevent IP spoofing attacks.
|
||
|
||
You can configure the IP address header in your Better Auth configuration:
|
||
|
||
```typescript
|
||
{
|
||
advanced: {
|
||
ipAddress: {
|
||
ipAddressHeaders: ['cf-connecting-ip'] // or any other custom header
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
This ensures that Better Auth only accepts IP addresses from your trusted proxy's header, making it more difficult for attackers to bypass rate limiting or other IP-based security measures by spoofing headers.
|
||
|
||
> **Important**: When setting a custom IP address header, ensure that your proxy or load balancer is properly configured to set this header, and that it cannot be set by end users directly.
|
||
|
||
## Trusted Origins
|
||
|
||
Trusted origins prevent CSRF attacks and block open redirects. You can set a list of trusted origins in the `trustedOrigins` configuration option. Requests from origins not on this list are automatically blocked.
|
||
|
||
### Basic Usage
|
||
|
||
The most basic usage is to specify exact origins:
|
||
|
||
```typescript
|
||
{
|
||
trustedOrigins: [
|
||
"https://example.com",
|
||
"https://app.example.com",
|
||
"http://localhost:3000"
|
||
]
|
||
}
|
||
```
|
||
|
||
### Wildcard Domains
|
||
|
||
Better Auth supports wildcard patterns in trusted origins, which allows you to trust multiple subdomains with a single entry:
|
||
|
||
```typescript
|
||
{
|
||
trustedOrigins: [
|
||
"*.example.com", // Trust all subdomains of example.com (any protocol)
|
||
"https://*.example.com", // Trust only HTTPS subdomains of example.com
|
||
"http://*.dev.example.com" // Trust all HTTP subdomains of dev.example.com
|
||
]
|
||
}
|
||
```
|
||
|
||
#### Protocol-specific wildcards
|
||
|
||
When using a wildcard pattern with a protocol prefix (like `https://`):
|
||
- The protocol must match exactly
|
||
- The domain can have any subdomain in place of the `*`
|
||
- Requests using a different protocol will be rejected, even if the domain matches
|
||
|
||
#### Protocol-agnostic wildcards
|
||
|
||
When using a wildcard pattern without a protocol prefix (like `*.example.com`):
|
||
- Any protocol (http, https, etc.) will be accepted
|
||
- The domain must match the wildcard pattern
|
||
|
||
### Custom Schemes
|
||
|
||
Trusted origins also support custom schemes for mobile apps and browser extensions:
|
||
|
||
```typescript
|
||
{
|
||
trustedOrigins: [
|
||
"myapp://", // Mobile app scheme
|
||
"chrome-extension://YOUR_EXTENSION_ID", // Browser extension
|
||
"exp://*/*", // Trust all Expo development URLs
|
||
"exp://10.0.0.*:*/*", // Trust 10.0.0.x IP range with any port
|
||
]
|
||
}
|
||
```
|
||
|
||
## Reporting Vulnerabilities
|
||
|
||
If you discover a security vulnerability in Better Auth, please report it to us at [security@better-auth.com](mailto:security@better-auth.com). We address all reports promptly, and credits will be given for validated discoveries.
|