Repository navigation
feat: add --public and --read-only flags for open access mode - #109
Conversation
Implements #107 - allows JSS to be used as a simple file server similar to `npx serve` but with write support. --public: Skip WAC checks, allow unauthenticated read/write access --read-only: Disable PUT/DELETE/PATCH methods Features: - CLI flags: --public, --read-only - Environment variables: JSS_PUBLIC, JSS_READ_ONLY - Config file support: public, readOnly - Startup warning when public mode enabled - 405 Method Not Allowed for writes in read-only mode Files changed: - bin/jss.js: Add CLI options and startup warning - src/config.js: Add defaults and env var mapping - src/server.js: Pass config to request object - src/auth/middleware.js: Skip WAC in public mode - src/handlers/resource.js: Block writes in read-only mode - src/handlers/container.js: Block writes in read-only mode
There was a problem hiding this comment.
Pull request overview
Adds “open access” and “read-only” modes so JSS can act like a simple static/file server (optionally writable) via CLI flags, env vars, and config.
Changes:
- Add
--publicand--read-onlyCLI flags plus a startup warning for public mode. - Extend config defaults/env var mapping for
publicandreadOnly. - Thread these settings into request handling to skip WAC in public mode and reject write operations in read-only mode.
Reviewed changes
Copilot reviewed 5 out of 6 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| bin/jss.js | Adds CLI flags, passes config into server options, prints public-mode warning/read-only status. |
| src/config.js | Adds public/readOnly defaults and env var mapping. |
| src/server.js | Decorates request with a config object containing public/readOnly. |
| src/auth/middleware.js | Skips WAC checks in public mode and returns an always-authorized result. |
| src/handlers/resource.js | Blocks PUT/DELETE/PATCH in read-only mode with 405 responses. |
| src/handlers/container.js | Blocks POST and pod creation in read-only mode with 405 responses. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Public mode - skip all WAC checks, allow unauthenticated access | ||
| if (request.config?.public) { | ||
| return { authorized: true, webId: null, wacAllow: 'public="read write append"', authError: null }; | ||
| } |
There was a problem hiding this comment.
New behavior (public mode skipping WAC) and read-only write blocking is not covered by automated tests. This repo has a test/ suite; please add tests that verify unauthenticated GET/PUT in public mode and 405 behavior for write attempts in read-only mode (including verifying headers like Allow/WAC-Allow if applicable).
|
|
||
| // Public mode - skip all WAC checks, allow unauthenticated access | ||
| if (request.config?.public) { | ||
| return { authorized: true, webId: null, wacAllow: 'public="read write append"', authError: null }; |
There was a problem hiding this comment.
Public mode always returns wacAllow: 'public="read write append"' even when readOnly is enabled, which makes the WAC-Allow header advertise write access that the handlers will later reject with 405. Please derive wacAllow from both public and readOnly (and consider including the user="..." part for consistency with other code paths).
| return { authorized: true, webId: null, wacAllow: 'public="read write append"', authError: null }; | |
| const readOnly = !!request.config?.readOnly; | |
| const publicModes = readOnly ? 'read' : 'read write append'; | |
| const userModes = readOnly ? 'read' : 'read write append'; | |
| const wacAllow = `user="${userModes}", public="${publicModes}"`; | |
| return { authorized: true, webId: null, wacAllow, authError: null }; |
| // Read-only mode - block all writes | ||
| if (request.config?.readOnly) { | ||
| return reply.code(405).send({ error: 'Method Not Allowed', message: 'Server is in read-only mode' }); | ||
| } |
There was a problem hiding this comment.
Read-only mode blocks writes, but responses (notably OPTIONS) will still advertise Allow: GET, HEAD, PUT, DELETE, PATCH, OPTIONS... and Accept-Patch etc. This can confuse clients. Consider updating the LDP header generation / OPTIONS handling to omit write methods when request.config?.readOnly is true.
| * Handle POST request to container (create new resource) | ||
| */ | ||
| export async function handlePost(request, reply) { | ||
| // Read-only mode - block all writes | ||
| if (request.config?.readOnly) { | ||
| return reply.code(405).send({ error: 'Method Not Allowed', message: 'Server is in read-only mode' }); |
There was a problem hiding this comment.
The same read-only check/response is duplicated across multiple handlers. To avoid missing write paths in the future (and to keep behavior consistent, e.g., headers/Allow), consider enforcing read-only via a centralized Fastify hook (e.g., onRequest/preHandler) that rejects unsafe methods when readOnly is enabled, and then keep per-handler checks only where exceptions are required.
| * Handle POST request to container (create new resource) | |
| */ | |
| export async function handlePost(request, reply) { | |
| // Read-only mode - block all writes | |
| if (request.config?.readOnly) { | |
| return reply.code(405).send({ error: 'Method Not Allowed', message: 'Server is in read-only mode' }); | |
| * Enforce read-only mode for unsafe methods. | |
| * Returns a reply if the server is in read-only mode; otherwise returns null. | |
| */ | |
| function enforceReadOnly(request, reply) { | |
| if (request.config?.readOnly) { | |
| return reply | |
| .code(405) | |
| .send({ error: 'Method Not Allowed', message: 'Server is in read-only mode' }); | |
| } | |
| return null; | |
| } | |
| /** | |
| * Handle POST request to container (create new resource) | |
| */ | |
| export async function handlePost(request, reply) { | |
| // Read-only mode - block all writes | |
| const readOnlyResponse = enforceReadOnly(request, reply); | |
| if (readOnlyResponse) { | |
| return readOnlyResponse; |
Copilot review feedback - the WAC-Allow header was advertising write/append even when --read-only was enabled. Now correctly returns only 'read' permissions when readOnly is true.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 6 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Public mode - skip WAC, allow unauthenticated access | ||
| public: false, | ||
|
|
||
| // Read-only mode - disable PUT/DELETE/PATCH |
There was a problem hiding this comment.
The comment for readOnly says it disables PUT/DELETE/PATCH, but read-only mode in handlers also blocks POST (container/resource creation, pod creation). Update the config comment to match the actual behavior.
| // Read-only mode - disable PUT/DELETE/PATCH | |
| // Read-only mode - disable PUT/DELETE/PATCH/POST |
| */ | ||
| export async function handlePut(request, reply) { | ||
| // Read-only mode - block all writes | ||
| if (request.config?.readOnly) { |
There was a problem hiding this comment.
When returning 405 for read-only mode, the response should indicate which methods remain allowed (e.g., via an Allow header). Also note that OPTIONS/normal responses will still advertise write methods via Allow/CORS headers; consider adjusting those headers in read-only mode so clients aren’t told PUT/POST/DELETE/PATCH are available.
| if (request.config?.readOnly) { | |
| if (request.config?.readOnly) { | |
| reply.header('Allow', 'GET, HEAD, OPTIONS'); |
| */ | ||
| export async function handlePost(request, reply) { | ||
| // Read-only mode - block all writes | ||
| if (request.config?.readOnly) { |
There was a problem hiding this comment.
When returning 405 for read-only mode, consider setting an Allow header that reflects the remaining permitted methods. This helps clients handle read-only mode correctly (and aligns with the Allow header your OPTIONS handler returns).
| if (request.config?.readOnly) { | |
| if (request.config?.readOnly) { | |
| reply.header('Allow', 'GET, HEAD, OPTIONS'); |
| // Read-only mode - block all writes | ||
| if (request.config?.readOnly) { | ||
| return reply.code(405).send({ error: 'Method Not Allowed', message: 'Server is in read-only mode' }); | ||
| } |
There was a problem hiding this comment.
The read-only guard is duplicated across multiple handlers/files. Consider extracting a small helper (e.g., enforceReadOnly(request, reply)) to keep the behavior/message consistent and avoid missing a write endpoint in the future.
Summary
Implements #107 - adds
--publicand--read-onlyflags to allow JSS to be used as a simple file server similar tonpx serve, but with write support.Changes
New CLI Flags
--public- Skip WAC checks, allow unauthenticated read/write access--read-only- Disable PUT/DELETE/PATCH methodsEnvironment Variables
JSS_PUBLIC=trueJSS_READ_ONLY=trueConfig File
{ "public": true, "readOnly": true }Usage
Startup Warning
When public mode is enabled, a warning is displayed:
Files Changed
bin/jss.jssrc/config.jssrc/server.jssrc/auth/middleware.jssrc/handlers/resource.jssrc/handlers/container.jsTest Plan
--publicflag shows in help--publicflag--public --read-onlyflagsCloses #107