enh
This commit is contained in:
parent
7e418a0b6b
commit
9ab47b32c4
3 changed files with 48 additions and 71 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import { router, protectedProcedure } from '@/src/trpc/trpc-index'
|
||||
import { router, staffProcedure } from '@/src/trpc/trpc-index'
|
||||
import { z } from 'zod';
|
||||
import { TRPCError } from '@trpc/server'
|
||||
import { computeConstants } from '@/src/lib/const-store'
|
||||
import { refreshGenKv, refreshGenKvForRegion } from '@/src/lib/gen-store'
|
||||
import { CONST_SCOPE } from '@/src/lib/const-keys'
|
||||
|
|
@ -7,10 +8,6 @@ import { scheduleStoreInitialization } from '@/src/stores/store-initializer'
|
|||
import {
|
||||
getAllConstants as getAllConstantsFromDb,
|
||||
upsertConstants as upsertConstantsInDb,
|
||||
getAllGlobalConsts,
|
||||
upsertGlobalConsts,
|
||||
getLegacyConsts,
|
||||
upsertLegacyConsts,
|
||||
} from '@/src/dbService'
|
||||
import type { Constant, ConstantUpdateResult } from '@packages/shared'
|
||||
|
||||
|
|
@ -22,17 +19,14 @@ const constantsInput = z.object({
|
|||
constants: z.array(z.object({ key: z.string(), value: z.any() })),
|
||||
})
|
||||
|
||||
const toRows = (constants: Constant[]) =>
|
||||
constants.map((c) => ({ key: c.key, value: c.value == null ? null : String(c.value) }))
|
||||
|
||||
export const constRouter = router({
|
||||
// Backward-compat: merged global + legacy view. Writes route by key scope.
|
||||
getConstants: protectedProcedure
|
||||
getConstants: staffProcedure
|
||||
.query(async (): Promise<Constant[]> => {
|
||||
return getAllConstantsFromDb();
|
||||
}),
|
||||
|
||||
updateConstants: protectedProcedure
|
||||
updateConstants: staffProcedure
|
||||
.input(constantsInput)
|
||||
.mutation(async ({ input, ctx }): Promise<ConstantUpdateResult> => {
|
||||
const { constants } = input;
|
||||
|
|
@ -45,10 +39,24 @@ export const constRouter = router({
|
|||
throw new Error(`Invalid constant keys: ${invalidKeys.join(', ')}`);
|
||||
}
|
||||
|
||||
const regionId = ctx.staffUser?.regionId ?? null
|
||||
await upsertConstantsInDb(constants, regionId ?? undefined);
|
||||
// Global constants (versionNum, playStoreUrl, …) are superadmin-only and are
|
||||
// managed via superadmin.const.*. Never let a region staff token write them
|
||||
// through the merged route.
|
||||
const globalKeys = constants
|
||||
.filter((c) => CONST_SCOPE[c.key] === 'global')
|
||||
.map((c) => c.key)
|
||||
|
||||
await computeConstants();
|
||||
if (globalKeys.length > 0) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: `Global constants are superadmin-only: ${globalKeys.join(', ')}`,
|
||||
})
|
||||
}
|
||||
|
||||
const regionId = ctx.staffUser?.regionId ?? null
|
||||
await upsertConstantsInDb(constants, regionId ?? undefined)
|
||||
|
||||
await computeConstants()
|
||||
|
||||
const touchedRegion = constants.some((c) => CONST_SCOPE[c.key] === 'region')
|
||||
const touchedOther = constants.some((c) => CONST_SCOPE[c.key] !== 'region')
|
||||
|
|
@ -65,46 +73,4 @@ export const constRouter = router({
|
|||
keys: constants.map(c => c.key),
|
||||
};
|
||||
}),
|
||||
|
||||
// Global consts (global_consts table).
|
||||
getGlobalConstants: protectedProcedure
|
||||
.query(async (): Promise<Constant[]> => {
|
||||
const values = await getAllGlobalConsts()
|
||||
return Object.entries(values).map(([key, value]) => ({ key, value }))
|
||||
}),
|
||||
|
||||
updateGlobalConstants: protectedProcedure
|
||||
.input(constantsInput)
|
||||
.mutation(async ({ input }): Promise<ConstantUpdateResult> => {
|
||||
await upsertGlobalConsts(toRows(input.constants))
|
||||
await computeConstants()
|
||||
await refreshGenKv()
|
||||
return {
|
||||
success: true,
|
||||
updatedCount: input.constants.length,
|
||||
keys: input.constants.map(c => c.key),
|
||||
}
|
||||
}),
|
||||
|
||||
// Legacy leftovers (key_val_store table).
|
||||
getLegacyConstants: protectedProcedure
|
||||
.query(async (): Promise<Constant[]> => {
|
||||
const values = await getLegacyConsts()
|
||||
return Object.entries(values).map(([key, value]) => ({ key, value }))
|
||||
}),
|
||||
|
||||
updateLegacyConstants: protectedProcedure
|
||||
.input(constantsInput)
|
||||
.mutation(async ({ input }): Promise<ConstantUpdateResult> => {
|
||||
await upsertLegacyConsts(toRows(input.constants))
|
||||
await computeConstants()
|
||||
if (input.constants.some((c) => CACHE_AFFECTING_KEYS.includes(c.key))) {
|
||||
await scheduleStoreInitialization()
|
||||
}
|
||||
return {
|
||||
success: true,
|
||||
updatedCount: input.constants.length,
|
||||
keys: input.constants.map(c => c.key),
|
||||
}
|
||||
}),
|
||||
});
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@ Confirmed reachable with a user token:
|
|||
|
||||
- `admin.order.cancelOrder` — cancel any order
|
||||
- `admin.user.updateUserSuspension` — suspend any user
|
||||
- `admin.const.updateConstants` — change app configuration
|
||||
- `admin.user.sendNotification` — broadcast notifications
|
||||
- `admin.product.toggleOutOfStock` (and the other `admin.product.*` writes)
|
||||
- `admin.staffUser.createStaffUser` — **create a usable staff account (privilege escalation)**; the new account can log in
|
||||
|
|
@ -31,6 +30,14 @@ write.
|
|||
|
||||
**Pinned by:** `tests/api/authorization-matrix.test.ts`.
|
||||
|
||||
> **Partially fixed (FC-01/SEC-01):** `admin.const.*` is no longer reachable with
|
||||
> a user token. The router now uses `staffProcedure`; `updateConstants` also
|
||||
> rejects global-scope keys (globals are superadmin-only), and the redundant
|
||||
> `getGlobalConstants`/`updateGlobalConstants`/`getLegacyConstants`/
|
||||
> `updateLegacyConstants` procedures were removed (they had no production UI
|
||||
> caller — the admin apps use `admin.region.*`, the superadmin UI uses
|
||||
> `superadmin.const.*`). The remaining entries above are still open.
|
||||
|
||||
### 2. Public `admin.vendorSnippets` endpoints
|
||||
|
||||
`src/trpc/apis/admin-apis/apis/vendor-snippets.ts`
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@
|
|||
*/
|
||||
|
||||
import { apiClient, captureError, uniqueSuffix } from '../helpers/api'
|
||||
import { asNumber, getConstants, setConst } from '../helpers/consts'
|
||||
import {
|
||||
createFutureSlot,
|
||||
createStoreWithSku,
|
||||
|
|
@ -83,20 +82,6 @@ describe('authorization matrix', () => {
|
|||
}
|
||||
})
|
||||
|
||||
it('pins: a user token CAN change config via admin.const.updateConstants', async () => {
|
||||
const before = await getConstants()
|
||||
const original = before.minOrderAmountForCoupon
|
||||
|
||||
try {
|
||||
await apiClient(userToken).admin.const.updateConstants.mutate({
|
||||
constants: [{ key: 'minOrderAmountForCoupon', value: 4321 }],
|
||||
})
|
||||
expect(asNumber((await getConstants()).minOrderAmountForCoupon)).toBe(4321)
|
||||
} finally {
|
||||
if (original !== undefined) await setConst('minOrderAmountForCoupon', original)
|
||||
}
|
||||
})
|
||||
|
||||
it('pins: a user token CAN send a notification via admin.user.sendNotification', async () => {
|
||||
const target = await registerThrowawayUser()
|
||||
|
||||
|
|
@ -167,6 +152,25 @@ describe('authorization matrix', () => {
|
|||
)
|
||||
expect(error.message).toMatch(/UNAUTHORIZED|unauthorized/i)
|
||||
})
|
||||
|
||||
it('rejects a user token on admin.const.updateConstants', async () => {
|
||||
const error = await captureError(() =>
|
||||
apiClient(userToken).admin.const.updateConstants.mutate({
|
||||
constants: [{ key: 'minOrderAmountForCoupon', value: 4321 }],
|
||||
})
|
||||
)
|
||||
expect(error.message).toMatch(/UNAUTHORIZED|unauthorized/i)
|
||||
})
|
||||
|
||||
it('rejects a staff token writing a global constant via admin.const.updateConstants', async () => {
|
||||
const staff = await staffSession()
|
||||
const error = await captureError(() =>
|
||||
apiClient(staff.token).admin.const.updateConstants.mutate({
|
||||
constants: [{ key: 'versionNum', value: '9.9.9' }],
|
||||
})
|
||||
)
|
||||
expect(error.message).toMatch(/superadmin-only/i)
|
||||
})
|
||||
})
|
||||
|
||||
describe('public admin.vendorSnippets procedures', () => {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue