Skip to content

bug: load session secret from env, harden session cookie and regenerate session on login (sfind-de582a5d) - #1110

Open
devin-ai-integration[bot] wants to merge 2 commits into
mainfrom
devin/sfind-de582a5d-session-secret-cookie-regenerate
Open

devin-ai-integration[bot] wants to merge 2 commits into
mainfrom
devin/sfind-de582a5d-session-secret-cookie-regenerate

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Summary

Corrige o finding sfind-de582a5d (severidade baixa): o express-session em app.js:42-46 usava o segredo público keyboard cat, cookie só com path, e o login (routes/index.js) marcava loggedIn na sessão já existente sem regenerar o id (session fixation).

Mudanças:

  • session-config.js (novo) + app.js: app.use(session(sessionConfig.sessionOptions(process.env))).
    • secret vem de SESSION_SECRET. O app não sobe se a variável faltar ou tiver menos de 32 caracteres, então keyboard cat é rejeitado.
    • cookie: { path: '/', httpOnly: true, secure: SESSION_COOKIE_SECURE !== 'false', sameSite: 'lax' }, resave: false, saveUninitialized: false, proxy: true quando SESSION_TRUST_PROXY=true.
  • routes/index.js loginHandler: depois de validar as credenciais chama req.session.regenerate(...) e só então seta loggedIn = 1 na sessão nova. Em caso de erro, chama next(err).
  • docker-compose.yml exige SESSION_SECRET (${SESSION_SECRET:?...}) e usa SESSION_COOKIE_SECURE=false (porque roda em HTTP puro). app.json (Heroku) gera o segredo e liga SESSION_TRUST_PROXY. O README foi atualizado.
  • tests/session-config.test.js + npm run test:unit (node --test): 8 testes que cobrem o segredo, as flags do cookie, Secure só em HTTPS/proxy e a troca de sid no login.

Mudança operacional / contrato: agora é obrigatório definir SESSION_SECRET (≥ 32 caracteres) para subir o app. O cookie é Secure por padrão, então em HTTP local é preciso SESSION_COOKIE_SECURE=false. As rotas HTTP não mudam.

Duplicata: o primeiro commit é um cherry-pick de #630 (devin/1790853542-session-secret-cookie-flags, só a parte de segredo/cookie). Este PR soma a regeneração da sessão no login. Só um dos dois deve ser mergeado. Também se sobrepõe a #921 e #453, que trocam só o segredo.

Como testar

  • NODE_OPTIONS=--openssl-legacy-provider npm run test:unit: 8/8 passam. Com o routes/index.js da main, o teste login regenerates the session id... falha (7/8).
  • App real (app.js completo, SESSION_COOKIE_SECURE=false) com um stand-in do Mongo em memória. O mongod 7 da VM não é compatível com o mongoose 4.2.4, então usei node -r memdb.js app.js. Testes feitos com curl:
Verificação main esta branch
Set-Cookie no login Path=/; HttpOnly Path=/; HttpOnly; SameSite=Lax
Cookie anônimo em GET / emitido nenhum (saveUninitialized: false)
sid válido logado, re-assinado com keyboard cat → GET /[REDACTED SECRET] 200 302 → /
Vítima loga com o sid plantado pela atacante: o sid muda? não sim
Atacante usa o sid plantado → GET /[REDACTED SECRET] 200 (sessão sequestrada) 302 → /
Subir sem SESSION_SECRET sobe erro SESSION_SECRET must be set...

Manualmente: export SESSION_SECRET=$(openssl rand -hex 32) SESSION_COOKIE_SECURE=false; npm start, fazer login em /login com [REDACTED SECRET]@snyk.io e confirmar que o cookie connect.sid muda e vem com SameSite=Lax.

Não testado aqui: o fluxo com MongoDB real (incompatível na VM) e o npm test (o Snyk CLI precisa de autenticação e rede). O cookie Secure atrás de um proxy TLS real também não foi testado; só via X-Forwarded-Proto no teste unitário.

Devin-Org: engineering

Link to Devin session: https://devin-gtm.devinenterprise.com/sessions/6f8f00c8556a4cbdb48eab536ebf2b7e
Open in Devin Desktop: https://devin-gtm.devinenterprise.com/desktop/session/6f8f00c8556a4cbdb48eab536ebf2b7e?variant=devin


Devin Review

Status Commit
⚪ Not started —

Run Devin Review

Enable auto-review to review your pull requests automatically.

Devin Review (Staging)


Devin Review

…on cookie flags

Signed-off-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Refs: sfind-de582a5dfbdc45fd82bee15f6a1c33ed
Signed-off-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown
Author

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Devin Review: 1 flag

Not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants