-
Notifications
You must be signed in to change notification settings - Fork 234
feat(admin): add authenticated settings API endpoints #690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8608b26
feat(admin): extract shared settings-config module for CLI and admin UI
Ferryx349 89f09b9
fix(settings-config): harden path parsing against prototype pollution
Ferryx349 1e3e8b7
Merge branch 'main' into Settings
Ferryx349 4f261ab
Merge branch 'main' into Settings
Ferryx349 cc5c7b3
feat(admin): add authenticated settings API endpoints
Ferryx349 3878d55
Merge upstream/main into settings-API
Ferryx349 e7c0e78
ci: retrigger checks
Ferryx349 0447dcd
Merge branch 'main' into settings-api
cameri d2b424c
Merge branch 'main' into settings-api
cameri 733b79f
fix(admin): improve settings persistence and validation
Ferryx349 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "nostream": minor | ||
| --- | ||
|
|
||
| feat: add authenticated admin settings API endpoints |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { Request, Response } from 'express' | ||
|
|
||
| import { IController } from '../../@types/controllers' | ||
| import { Settings } from '../../@types/settings' | ||
| import { loadDefaults, loadMergedSettings, filterSettingsAgainstDefaults } from '../../utils/settings-config' | ||
| import { redactSettingsSecrets } from '../../utils/settings-redaction' | ||
|
|
||
| export class GetAdminSettingsController implements IController { | ||
| public async handleRequest(_request: Request, response: Response): Promise<void> { | ||
| const merged = loadMergedSettings() | ||
| const defaults = loadDefaults() | ||
| const filtered = filterSettingsAgainstDefaults(merged, defaults) as Settings | ||
| const settings = redactSettingsSecrets(filtered) | ||
|
|
||
| response.status(200).setHeader('content-type', 'application/json').send({ settings }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { Request, Response } from 'express' | ||
|
|
||
| import { IController } from '../../@types/controllers' | ||
| import { guidedSettingCategories } from '../../utils/settings-guided-schema' | ||
|
|
||
| export class GetAdminSettingsSchemaController implements IController { | ||
| public async handleRequest(_request: Request, response: Response): Promise<void> { | ||
| response.status(200).setHeader('content-type', 'application/json').send({ categories: guidedSettingCategories }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { Request, Response } from 'express' | ||
|
|
||
| import { Settings } from '../../@types/settings' | ||
| import { IController } from '../../@types/controllers' | ||
| import { adminSettingsPatchBodySchema } from '../../schemas/admin-settings-schema' | ||
| import { | ||
| getByPath, | ||
| loadMergedSettings, | ||
| loadUserSettings, | ||
| saveSettings, | ||
| setByPath, | ||
| validatePathAgainstDefaults, | ||
| validateSettings, | ||
| } from '../../utils/settings-config' | ||
| import { | ||
| isWriteProtectedSettingsPath, | ||
| redactSettingsValue, | ||
| } from '../../utils/settings-redaction' | ||
| import { validateSchema } from '../../utils/validation' | ||
|
|
||
| export class PatchAdminSettingsController implements IController { | ||
| public async handleRequest(request: Request, response: Response): Promise<void> { | ||
| const validation = validateSchema(adminSettingsPatchBodySchema)(request.body) | ||
| if (validation.error) { | ||
| response.status(400).setHeader('content-type', 'application/json').send({ error: 'Invalid request' }) | ||
| return | ||
| } | ||
|
|
||
| const { path, value } = validation.value | ||
|
|
||
| if (isWriteProtectedSettingsPath(path)) { | ||
| response | ||
| .status(400) | ||
| .setHeader('content-type', 'application/json') | ||
| .send({ | ||
| error: 'Validation failed', | ||
| issues: [{ path, message: 'Path is write-protected' }], | ||
| }) | ||
| return | ||
| } | ||
|
|
||
| const pathIssues = validatePathAgainstDefaults(path) | ||
| if (pathIssues.length > 0) { | ||
| response | ||
| .status(400) | ||
| .setHeader('content-type', 'application/json') | ||
| .send({ error: 'Validation failed', issues: pathIssues }) | ||
| return | ||
|
Ferryx349 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| const userSettings = loadUserSettings() as unknown as Record<string, unknown> | ||
| const nextUserSettings = setByPath(userSettings, path, value) | ||
|
|
||
| const merged = loadMergedSettings() as unknown as Record<string, unknown> | ||
| const mergedNext = setByPath(merged, path, getByPath(nextUserSettings, path)) | ||
| const validationIssues = validateSettings(mergedNext as unknown as Settings) | ||
|
|
||
| if (validationIssues.length > 0) { | ||
| response | ||
| .status(400) | ||
| .setHeader('content-type', 'application/json') | ||
| .send({ error: 'Validation failed', issues: validationIssues }) | ||
| return | ||
| } | ||
|
|
||
| saveSettings(nextUserSettings as unknown as Settings) | ||
|
|
||
|
Ferryx349 marked this conversation as resolved.
|
||
| const updatedValue = redactSettingsValue(path, getByPath(nextUserSettings, path)) | ||
|
|
||
| response.status(200).setHeader('content-type', 'application/json').send({ | ||
| ok: true, | ||
| path, | ||
| value: updatedValue, | ||
| }) | ||
| } | ||
| } | ||
17 changes: 17 additions & 0 deletions
17
src/controllers/admin/post-settings-validate-controller.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { Request, Response } from 'express' | ||
|
|
||
| import { IController } from '../../@types/controllers' | ||
| import { loadMergedSettings, validateSettings } from '../../utils/settings-config' | ||
|
|
||
| export class PostAdminSettingsValidateController implements IController { | ||
| public async handleRequest(_request: Request, response: Response): Promise<void> { | ||
| const issues = validateSettings(loadMergedSettings()) | ||
|
|
||
| if (issues.length === 0) { | ||
| response.status(200).setHeader('content-type', 'application/json').send({ valid: true, issues: [] }) | ||
| return | ||
| } | ||
|
|
||
| response.status(200).setHeader('content-type', 'application/json').send({ valid: false, issues }) | ||
| } | ||
| } |
6 changes: 6 additions & 0 deletions
6
src/factories/controllers/get-admin-settings-controller-factory.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import { GetAdminSettingsController } from '../../controllers/admin/get-settings-controller' | ||
| import { IController } from '../../@types/controllers' | ||
|
|
||
| export const createGetAdminSettingsController = (): IController => { | ||
| return new GetAdminSettingsController() | ||
| } |
6 changes: 6 additions & 0 deletions
6
src/factories/controllers/get-admin-settings-schema-controller-factory.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import { GetAdminSettingsSchemaController } from '../../controllers/admin/get-settings-schema-controller' | ||
| import { IController } from '../../@types/controllers' | ||
|
|
||
| export const createGetAdminSettingsSchemaController = (): IController => { | ||
| return new GetAdminSettingsSchemaController() | ||
| } |
6 changes: 6 additions & 0 deletions
6
src/factories/controllers/patch-admin-settings-controller-factory.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import { PatchAdminSettingsController } from '../../controllers/admin/patch-settings-controller' | ||
| import { IController } from '../../@types/controllers' | ||
|
|
||
| export const createPatchAdminSettingsController = (): IController => { | ||
| return new PatchAdminSettingsController() | ||
| } |
6 changes: 6 additions & 0 deletions
6
src/factories/controllers/post-admin-settings-validate-controller-factory.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import { PostAdminSettingsValidateController } from '../../controllers/admin/post-settings-validate-controller' | ||
| import { IController } from '../../@types/controllers' | ||
|
|
||
| export const createPostAdminSettingsValidateController = (): IController => { | ||
| return new PostAdminSettingsValidateController() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { z } from 'zod' | ||
|
|
||
| export const adminSettingsPatchBodySchema = z | ||
| .object({ | ||
| path: z.string().min(1), | ||
| value: z.custom<unknown>((input) => input !== undefined, { message: 'value is required' }), | ||
| }) | ||
| .strict() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| const SENSITIVE_SETTING_KEYS = new Set(['passwordHash', 'secret']) | ||
|
|
||
| const isPlainObject = (value: unknown): value is Record<string, unknown> => { | ||
| return typeof value === 'object' && value !== null && !Array.isArray(value) | ||
| } | ||
|
|
||
| export const isSensitiveSettingsPath = (path: string): boolean => { | ||
| const segments = path.split('.') | ||
| const lastSegment = segments[segments.length - 1] ?? '' | ||
| const key = lastSegment.replace(/\[\d+\]$/, '') | ||
|
|
||
| return SENSITIVE_SETTING_KEYS.has(key) | ||
| } | ||
|
|
||
| export const isWriteProtectedSettingsPath = (path: string): boolean => { | ||
| return path === 'admin.passwordHash' || path.endsWith('.passwordHash') | ||
| } | ||
|
|
||
| export const redactSettingsValue = (path: string, value: unknown): unknown => { | ||
| if (isSensitiveSettingsPath(path) && typeof value === 'string' && value.length > 0) { | ||
| return '***' | ||
| } | ||
|
|
||
| return value | ||
| } | ||
|
|
||
| export const redactSettingsSecrets = <T>(settings: T): T => { | ||
| const redactWalk = (value: unknown): unknown => { | ||
| if (Array.isArray(value)) { | ||
| return value.map(redactWalk) | ||
| } | ||
|
|
||
| if (!isPlainObject(value)) { | ||
| return value | ||
| } | ||
|
|
||
| const result: Record<string, unknown> = {} | ||
|
|
||
| for (const [key, entry] of Object.entries(value)) { | ||
| if (SENSITIVE_SETTING_KEYS.has(key) && typeof entry === 'string' && entry.length > 0) { | ||
| result[key] = '***' | ||
| continue | ||
| } | ||
|
|
||
| result[key] = redactWalk(entry) | ||
| } | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| return redactWalk(settings) as T | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.