fix(backend): validate Stellar publicKey format in user validator - #1148
fix(backend): validate Stellar publicKey format in user validator#1148Fury03 wants to merge 2 commits into
Conversation
Note on the failing CI checksThe red checks here are pre-existing on The new tests pass. Comparing this PR's Same 6 failing files and same 14 failing tests, with exactly +1 passing file and +13 passing tests — that delta is The pre-existing failures.
None of those files are touched here — this PR changes Happy to open a separate PR fixing the |
Closes #1098
Problem
registerUserSchemacheckedpublicKeywithz.string().min(50, ...).regex(/^G[A-Z2-7]{55}$/, ...). The regex was right, but the schema had two problems:min(50)produced a second, less specific issue ('Invalid Stellar public key') for every short input, so a reporter got two overlapping messages for one mistake.publicKeyfell back to Zod's genericinvalid_typetext.The format rule was also duplicated as an inline literal in three places in
user.controller.ts, with no single source of truth.Changes
All in
backend/src/validators/user.validator.ts:STELLAR_PUBLIC_KEY_REGEXandSTELLAR_PUBLIC_KEY_ERRORas the canonical format rule and message.stellarPublicKeySchemaand use it forregisterUserSchema.publicKey. Droppedmin(50), which the regex already subsumes.Invalid Stellar public key format: expected 56 base32 characters starting with "G". A missing or non-stringpublicKeygetspublicKey is required and must be a string.A malformed key now fails in
registerUserSchema.parseinsideregisterUser, which forwards theZodErrortoerrorHandler, returning 400 with{ error: 'Validation Error', details: [{ path: 'publicKey', message: ... }] }— before any Prisma lookup or write.Tests
New
backend/tests/user.validator.test.ts— 13 cases:'not-a-stellar-key', too short, too long, wrong version prefix (S...), lowercase, a character outside the base32 alphabet (1), empty string, and whitespace-padded.undefined, a number, andnullwith the type message..parsethrows aZodErrorcarrying the descriptive message — the input the error middleware turns into the 400.ZodError→ 400Validation Erroris already covered bytests/error.middleware.test.ts, so the test deliberately avoids importing the middleware and stays a dependency-free unit test.Verified locally:
(The full suite needs
prisma generate, which couldn't reachbinaries.prisma.shfrom my environment — the pre-existingtscerrors insse.controller.ts,soroban-indexer.service.ts, and others are untouched by this PR.)Notes
stellarPublicKeySchema's doc comment says so explicitly, so nobody mistakes a match for proof the key is real.StrKey.isValidEd25519PublicKeywould have pulled in CRC16 validation as a side effect; the issue allows a regex, so I used one.stellarPublicKeySchemato the otherpublicKeycall sites (the inline checks inuser.controller.ts, and other controllers) is left to the separately tracked issue.