Skip to content

Add optional Bearer token authentication for management API#15

Merged
n3-rd merged 6 commits intomainfrom
copilot/update-api-authorization-logic
Jan 29, 2026
Merged

Add optional Bearer token authentication for management API#15
n3-rd merged 6 commits intomainfrom
copilot/update-api-authorization-logic

Conversation

Copy link
Contributor

Copilot AI commented Jan 24, 2026

Adds opt-in authentication to protect write operations on the management API. Authorization is disabled by default for backward compatibility.

Changes

Core Implementation (core/api/server.js)

  • Load admin token from MULTIPB_ADMIN_TOKEN env var or config.adminToken (env takes precedence)
  • Enforce Bearer token auth on write operations (POST/PUT/DELETE/PATCH) when token is configured
  • GET requests remain unauthenticated for health monitoring
  • Return 401 on invalid/missing token

Configuration

  • Add adminToken field to config.example.json
  • Document MULTIPB_ADMIN_TOKEN in env.example

Tests (tests/api/auth.test.js)

  • Token validation logic (valid/invalid/missing tokens)
  • Empty/null token disables auth
  • Case-insensitive Bearer keyword

Documentation

  • Authorization section in API reference with curl examples
  • Security best practices in configuration guide

Example Usage

# Enable auth via environment
export MULTIPB_ADMIN_TOKEN=your-secret-token

# Authenticated request
curl -X POST http://localhost:25983/api/instances \
  -H "Authorization: Bearer your-secret-token" \
  -H "Content-Type: application/json" \
  -d '{"name": "myapp"}'

When no token is configured, API behavior is unchanged.

Original prompt

Goal: Improve security and robustness while preserving current behavior by making management API authorization optional (enabled when configured), validating instance names, running supervised programs as a non-root user, and verifying PocketBase download integrity.

Scope of changes:

  1. API Authorization (optional) and CORS
  • File: core/api/server.js
  • Add support for an optional admin token sourced from environment MULTIPB_ADMIN_TOKEN or config.json (config.adminToken). If a token is configured, require Authorization: Bearer or X-Admin-Token: for all /api/* endpoints. If no token is set, behavior remains unchanged but log a startup warning once.
  • Implement lightweight CORS handling: respond to OPTIONS preflight, include Access-Control-Allow-Origin (configurable via env MULTIPB_CORS_ORIGIN or default "*") and Access-Control-Allow-Headers (include Authorization, Content-Type), and Access-Control-Allow-Methods for typical verbs.
  • Add a utility ensureSafeName(name) to enforce a safe instance name pattern (e.g., ^a-z0-9?$) to avoid path traversal and special characters. Reject names that don’t match with 400 errors. Apply checks on endpoints that accept instance names (create/start/stop/remove/restore/upgrade, backups, proxy to PB, etc.).
  • Keep existing routes and functionality intact when token is not set.
  1. Shell script input validation
  • Files:
    • core/cli/add-instance.sh
    • core/cli/start-instance.sh
    • core/cli/stop-instance.sh
    • core/cli/remove-instance.sh
    • core/cli/restore-instance.sh
  • Add a validate_name() function with the same regex rule as server.js. Use it early in each script to error out for invalid names. Disallow characters such as '/', '..', whitespace, and leading/trailing hyphens.
  1. Run supervised programs as non-root user
  • File: Dockerfile
  • Create a dedicated non-root user (e.g., user "multipb") and group during image build, e.g., adduser -D -h /var/multipb multipb.
  • Chown runtime directories (/var/multipb/*, /var/log/multipb, /etc/caddy, /etc/supervisor/conf.d) to the multipb user where appropriate.
  • File: core/entrypoint.sh (supervisord config block generation)
    • Update [program:caddy], [program:api-server], and [program:log-streamer] entries to run with user=multipb. Leave [supervisord] user as root to manage sockets and process control cleanly without larger refactors.
    • Ensure the supervisord socket and logs remain accessible.
  1. PocketBase integrity verification
  • File: Dockerfile
  • After downloading pocketbase_${PB_VERSION}_linux_${PB_ARCH}.zip, download checksums.txt from the same release and verify the SHA256 for the zip matches before unzipping. Fail the build if mismatch.
  • Keep Caddy download unchanged to avoid complexity (Caddy API’s checksum handling varies); we can note future enhancement.
  1. Documentation updates
  • File: README.md
  • Add a "Security" section explaining:
    • Setting MULTIPB_ADMIN_TOKEN (or config.adminToken) enables authorization for all /api/* endpoints. Without it, the API is open (not recommended in production).
    • Instance name rules (allowed characters, length constraints).
    • Non-root program execution under supervisord.
    • PocketBase checksum verification now in build.
  • Briefly mention recommended deployment practices (behind an authenticated reverse proxy, limit exposure, rotate credentials).

Acceptance Criteria

  • Building the image still succeeds; dashboard and API function normally when no admin token is set.
  • When MULTIPB_ADMIN_TOKEN or config.adminToken is set, requests to /api/* without valid token return 401. Authorized requests proceed.
  • Shell scripts reject invalid instance names consistently.
  • Supervised programs run as multipb user; logs continue to stream; Caddy and API still operational.
  • PocketBase archive checksum verification halts the build on mismatch.

Notes

  • Tests should continue to pass since authorization is optional when token is absent.
  • This PR does not remove the Caddy /_instances endpoint; a follow-up can gate or remove it if desired.
  • Future work: enforce token by default, add checksum verification for Caddy, migrate manifest to a lock-safe store, add CI security scanners.

This pull request was created from Copilot chat.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 4 commits January 24, 2026 21:13
Co-authored-by: n3-rd <56776094+n3-rd@users.noreply.github.com>
Co-authored-by: n3-rd <56776094+n3-rd@users.noreply.github.com>
Co-authored-by: n3-rd <56776094+n3-rd@users.noreply.github.com>
Co-authored-by: n3-rd <56776094+n3-rd@users.noreply.github.com>
Copilot AI changed the title [WIP] Improve security with optional API authorization and CORS handling Add optional Bearer token authentication for management API Jan 24, 2026
Copilot AI requested a review from n3-rd January 24, 2026 21:21
…lock modal, and refactor API calls to use new utility.
@n3-rd n3-rd marked this pull request as ready for review January 29, 2026 00:55
@n3-rd n3-rd merged commit b60e625 into main Jan 29, 2026
2 checks passed
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