Skip to content
This repository has been archived by the owner on Dec 12, 2023. It is now read-only.

Commit

Permalink
feat: make secure attribute of session cookie configurable (#49)
Browse files Browse the repository at this point in the history
* Updated readme with configurable secure attribute info

* Make secure attribute for session cookie configurable
  • Loading branch information
re-mxp authored Nov 30, 2022
1 parent d0300b3 commit 9c03dd3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ Here's what the full _default_ module configuration looks like:
storePrefix: 'sessions',
// The session cookie same site policy is `lax`
cookieSameSite: 'lax',
// `Secure` attribute of session cookie is set to `true`
cookieSecure: true,
// In-memory storage is used (these are `unjs/unstorage` options)
storageOptions: {
driver: 'memory',
Expand Down
1 change: 1 addition & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const defaults: FilledModuleOptions = {
idLength: 64,
storePrefix: 'sessions',
cookieSameSite: 'lax',
cookieSecure: true,
storageOptions: {
driver: 'memory',
options: {}
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/server/middleware/session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const SESSION_COOKIE_NAME = 'sessionId'
const safeSetCookie = (event: H3Event, name: string, value: string) => setCookie(event, name, value, {
// Max age of cookie in seconds
maxAge: useRuntimeConfig().session.session.expiryInSeconds,
// Only send cookie via HTTPs to mitigate man-in-the-middle attacks
secure: true,
// Wether to send cookie via HTTPs to mitigate man-in-the-middle attacks
secure: useRuntimeConfig().session.session.cookieSecure,
// Only send cookie via HTTP requests, do not allow access of cookie from JS to mitigate XSS attacks
httpOnly: true,
// Do not send cookies on many cross-site requests to mitigates CSRF and cross-site attacks, see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite#lax
Expand Down
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ export interface SessionOptions {
* @docs https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite
*/
cookieSameSite: SameSiteOptions
/**
* Wether to set the `Secure` attribute for the session cookie
* @default true
* @example false
* @type boolean
* @docs https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies
*/
cookieSecure: boolean
/**
* Driver configuration for session-storage. Per default in-memory storage is used
* @default { driver: 'memory', options: {} }
Expand Down

0 comments on commit 9c03dd3

Please sign in to comment.