Skip to content

Exposing encryptedSessionId leaks secret to database #52

Description

@cameronmbell

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

3.25.3

Plugin version

6.3.0

Node.js version

14.15.1

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

10

Description

Compared to express session, fastify session exposes the encrypted session id to the database. This could be used to determine the session secret given a database leak and thus leaking temporary password equivalent. It also wastes space by including the sessionId twice and expiry date three times.

express-session

"_id":"abcdefghijklmnopqrstuvwxyz",
"expires":"2022-03-08T09:43:51.997+00:00",
"session":{
    "cookie":{
        "originalMaxAge":"5184000000",
        "expires":"2022-03-08T09:43:35.786+00:00",
        "secure":false,
        "httpOnly":true,
        "domain":null,
        "path":"/",
        "sameSite":null
    }
}

fastify/session

"_id":"abcdefghijklmnopqrstuvwxyz",
"expires":"2022-03-08T09:43:51.997+00:00",
"session":{
    "expires":"2022-03-08T09:43:51.997+00:00",
    "cookie":{
        "originalMaxAge":"5184000000",
        "expires":"2022-03-08T09:43:35.786+00:00",
        "secure":false,
        "httpOnly":true,
        "domain":null,
        "path":"/",
        "sameSite":"strict"
    },
    "sessionId":"abcdefghijklmnopqrstuvwxyz",
    "encryptedSessionId":"qwertyuiopasdfghjklzxcvbnm"
}

Steps to Reproduce

import fp from 'fastify-plugin';
import cookie from 'fastify-cookie';
import session from '@fastify/session';

// https://github.com/jdesboeufs/connect-mongo/issues/441
import _ from 'express-session';
import MongoStore from 'connect-mongo';

export default fp(async (fastify, options) => {
    if (!fastify.dbClient) {
        throw new Error('Must register db plugin');
    }

    fastify.register(cookie);
    fastify.register(session, {
        secret: process.env.SECRET,
        saveUninitialized: false,
        cookieName: '__Host-sessionId',
        cookie: {
            sameSite: 'strict',
            httpOnly: true,
            secure: false
        },
        store: MongoStore.create({
            stringify: false,
            client: /* ... */
        })
    });
});

Expected Behavior

Do not write encryptedSessionId to database.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions