express-sessionのセッションIDの生成を浅く確認する。

expressセッションIDの生成についても読んでみる。

var uid = require('uid-safe').sync

(中略)

function session(options) {
  var opts = options || {}

  // get the cookie options
  var cookieOptions = opts.cookie || {}

  // get the session id generate function
  var generateId = opts.genid || generateSessionId

(中略)

  // generates the new session
  store.generate = function(req){
    req.sessionID = generateId(req);
    req.session = new Session(req);
    req.session.cookie = new Cookie(cookieOptions);

(中略)

/**
 * Generate a session ID for a new session.
 *
 * @return {String}
 * @private
 */
function generateSessionId(sess) {
  return uid(24);
}

デフォルトの(MAC生成する前の)セッションID生成は、uid-safeのsyncを使って生成されていることが確認できる。 uid-safeについて調べてみると、cookieとurlでの使用に対して、暗号的で保護されたUIDを生成してくれるらしい。

https://www.npmjs.com/package/uid-safe

Create cryptographically secure UIDs safe for both cookie and URL usage.

uid.syncはバイト長を指定して生成する様子。

uid.sync(byteLength)

A synchronous version of above.