Skip to content

fix(security): harden default API key, IP whitelist, and QR access#105

Open
Pakzone1 wants to merge 1 commit into
rmyndharis:mainfrom
Pakzone1:bugfix/harden-default-api-key-and-auth
Open

fix(security): harden default API key, IP whitelist, and QR access#105
Pakzone1 wants to merge 1 commit into
rmyndharis:mainfrom
Pakzone1:bugfix/harden-default-api-key-and-auth

Conversation

@Pakzone1
Copy link
Copy Markdown

Description

This PR tightens a few security gaps around API keys and session pairing.

On first startup, OpenWA used to create a fixed dev-admin-key whenever NODE_ENV was not set to production. Many Docker and staging setups never set that variable, so new installs could end up with a known admin key. Now the default is always a random key. The old predictable key is only used when you set ALLOW_DEV_API_KEY=true, which is meant for local development only. The dev Docker compose file turns that on so local workflows stay convenient.

API keys with an IP whitelist used to skip the check when the server could not see a client IP. That meant the restriction could be bypassed. If a whitelist is configured and the client IP is unknown, the request is now rejected.

The QR code endpoint for linking WhatsApp (GET /sessions/:id/qr) did not require operator permissions. A viewer key could fetch the QR and pair another device to someone else's session. That route now requires the OPERATOR role.

Docs were updated in .env.example and docs/README.md to explain ALLOW_DEV_API_KEY.

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Testing

After npm install, run:

  • npm test -- src/modules/auth/auth.service.spec.ts
  • npm run lint

Manual checks (optional):

  • Fresh install without ALLOW_DEV_API_KEY: startup log and data/.api-key should show a random owa_k1_... key.
  • With ALLOW_DEV_API_KEY=true: first boot uses dev-admin-key (dev only).
  • A VIEWER key calling GET /sessions/:id/qr should get 401.

Checklist

  • Tests added/updated
  • Documentation updated
  • Lint passes (run locally before merge)
  • Self-reviewed

Screenshots (if applicable)

N/A

Related Issues

No linked issue. This addresses insecure default API key behavior and related auth hardening.

Seed random admin key unless ALLOW_DEV_API_KEY=true is set explicitly.

Reject API keys with IP whitelist when client IP cannot be determined.

Require OPERATOR role for GET /sessions/:id/qr.

Document ALLOW_DEV_API_KEY in .env.example and docs README.

Opt in predictable dev key in docker-compose.dev.yml.

it('should reject when IP whitelist is set but client IP is unknown', async () => {
const key = createMockApiKey({
allowedIps: ['10.0.0.1'],
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It would be great if we could configure it via the .env file. Have you already thought about this, or is the configuration done via the web interface ?

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.

2 participants