parity(api,apps): close SEC-02 - orders take the customer from the token

The user declined to defer this to Phase 2, correctly: a public POST /orders
with customerId in the body let anyone place an order on any customer's
account, and an order lands on that customer's gold debt and ledger.

POST /orders is now behind CustomerAuth. OrdersService.createOrder takes the
customer id as a separate argument that the controller fills from the token;
the body's customerId is ignored and a mismatch is logged. The field stays
optional in the DTO and in shared-types only so both apps keep validating
while they still send it -- they drop it in Phase 2. The admin builder is
unaffected: it legitimately places orders for a chosen customer and passes
that id explicitly, behind the admin guard.

GET /orders/:id is scoped to the owner. A foreign or nonexistent id both
return 404, never 403, so ids stay unenumerable. Operators read through the
new GET /orders/admin/:id behind rid order.list, declared before the :id
route because Nest matches in declaration order.

Both apps' CartView now gates on session.isLoggedIn rather than a remembered
customerId, which is no longer a credential.

OTP audit (requested): no master code, no bypass, no hardcoded code anywhere;
one code path issues a token and only after bcrypt.compare. No SMS provider is
wired up -- in development the code is logged once, guarded by
NODE_ENV !== 'production'.

Fixed while auditing (SEC-11): deliver() persisted the real message text, code
included, into SmsMessage.body, which the admin SMS grid lists and full-text
searches. Any operator with the sms rid could read live codes, defeating the
hashing of codeHash. The stored row is now a redacted audit line.

Also logged MON-08: wagePercent/discountPercent/flatRialCharge are still taken
from the client on order creation. The reference is worse -- it takes
rash_weight from the client too -- so this is already a partial improvement;
logged rather than changed, per D4.

Tests: auth-smoke.sh now 53 passed, 0 failed, including a spoofed body
customerId being ignored in favour of the token. Typecheck clean on api,
user-app, user-app-v2 and shared-types.

[ref: henza_server ApiMobileController.php:533,536-545,615-632]

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
s.esmaeili
2026-09-22 07:09:33 +03:30
co-authored by Claude Opus 5
parent 4efe543d62
commit 024e3dd045
12 changed files with 394 additions and 43 deletions
+59
View File
@@ -176,3 +176,62 @@ surface entirely.
`pnpm test` works for the first time: **7 passed** covering `faToEn`.
`faToEn` moved to its own `digits.ts` so it can be tested without booting Nest.
- Typecheck clean on api, user-app and user-app-v2; both apps build.
---
## 2026-09-22 — Security hotfix, stage 3 — SEC-02 (orders) + OTP audit
Branch `parity/security-hotfix`. The user declined to defer SEC-02 to Phase 2:
a public `POST /orders` taking `customerId` from the body let anyone place an
order on any customer's account, and an order lands on that customer's **gold
debt and ledger**. That is a money hole, so it is closed here.
### 1. `POST /orders` is customer-authenticated
- **What:** the route is behind `@CustomerAuth()`. `OrdersService.createOrder`
now takes `customerId` as a **separate argument**, supplied by the controller
from the token. The body's `customerId` is ignored entirely; a mismatch is
logged (`OrdersService` warn) and the token always wins.
- `CreateOrderDto.customerId` and the shared `CreateOrderDto` are **optional**,
so both apps keep validating while they still send it. Removed in Phase 2.
- **Reference:** `ApiMobileController::setOrder` read `auth()->id()`
(henza_server:533) behind the `api-customer` guard — this restores that.
- **Admin path unaffected:** `AdminOrdersService` places orders *for* a selected
customer and passes that id explicitly; its route is admin-guarded.
### 2. `GET /orders/:id` is scoped to the owner
- `getOwnedById(orderId, customerId)` matches on both, and a miss is **404, not
403** — a 403 would confirm the id exists, the same enumeration leak the old
`/customers/:id` routes had. A nonexistent id and someone else's id are
indistinguishable.
- Operators use the new `GET /orders/admin/:id` behind rid `order.list`.
Declared **before** `:id` because Nest matches in declaration order.
### 3. Both apps
- `CartView.vue` in **both** apps now gates on `session.isLoggedIn` (the token)
rather than on a remembered `customerId`, which is no longer a credential.
### 4. OTP audit (user request 3)
- **No master code, no bypass, no hardcoded code** anywhere in the API or either
app — verified by grep for `master.?code|bypass|backdoor|1111|0000|1234|skipOtp|DEV_OTP|TEST_OTP`.
Exactly one code path issues a token, and only after `bcrypt.compare`.
- **SMS provider: none.** No gateway is wired up (STUB-02). In development the
code is logged once via `Logger`, guarded by `NODE_ENV !== 'production'`.
- **Fixed while auditing:** `deliver()` persisted the real message text — with
the code in it — into `SmsMessage.body`, which the admin SMS grid both lists
and full-text searches (`admin-misc.service.ts:135-136`). Any operator with
the `sms` rid could read live OTP codes, defeating the point of hashing
`codeHash`. The persisted row is now a redacted audit line; the real text is
built for the future gateway and not stored. The 9 test rows created during
this session were deleted.
### Tests
`apps/api/scripts/auth-smoke.sh` extended again — **53 passed, 0 failed**:
order routes 401 without a token, a spoofed body `customerId` ignored in favour
of the token, own order readable, unknown/foreign order id 404, and the admin
order route rejecting a customer token while admitting an admin one.
Typecheck clean on api, user-app, user-app-v2 and shared-types.