backend setup - #13
Conversation
📝 WalkthroughWalkthroughThis PR adds five Express router modules, mounts them in the API entrypoint, updates the CORS origin default to ChangesAPI routing and response/config updates
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 4
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@apps/api/src/config/env.ts`:
- Around line 5-6: Restore URL validation in the env schema so malformed
connection strings fail at boot instead of later; update the Zod definitions in
env.ts for DATABASE_URL and REDIS_URL to use the URL-specific validator again,
keeping the REDIS_URL default while still enforcing valid URL format. Locate the
change in the env schema constants where DATABASE_URL and REDIS_URL are
declared.
- Line 9: The `CORS_ORIGIN` env default in `env.ts` is too permissive because it
allows any origin by default. Update the `env` schema to require an explicit
allow-listed origin value (or a validated list) instead of `"*"`, and make
`apps/api/src/index.ts`’s `cors({ origin: env.CORS_ORIGIN })` use that
restricted setting so only approved app origins are accepted.
In `@apps/api/src/index.ts`:
- Around line 17-18: The CORS setup in app.use(cors({ origin: env.CORS_ORIGIN
})) is too permissive because env.CORS_ORIGIN can default to "*", so replace it
with explicit allow-list validation for app origins only. Update the CORS
configuration in the app bootstrap to parse a comma-separated allow-list and use
a cors() callback that checks the request origin against that list, rather than
passing the env string directly. Use the existing cors middleware setup in
index.ts and the CORS_ORIGIN config from env.ts as the main points to adjust.
In `@apps/api/src/middleware/errorHandler.ts`:
- Around line 10-16: Unconditional raw error logging in errorHandler can leak
user-submitted data, especially for ZodError validation failures. Update the
errorHandler middleware to avoid calling console.error(err) for expected 4xx
validation cases; instead, only log unexpected server errors or use a
structured/redacting logger that does not dump the full error object. Keep the
existing ZodError branch behavior intact while ensuring the logging path is
gated by the error type/status.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 0f42be48-de0d-4b4a-8a70-9c15aa3ff1d2
📒 Files selected for processing (8)
apps/api/src/config/env.tsapps/api/src/index.tsapps/api/src/middleware/errorHandler.tsapps/api/src/routes/likes.tsapps/api/src/routes/posts.tsapps/api/src/routes/uploads.tsapps/api/src/routes/users.tsapps/api/src/routes/webhooks.ts
There was a problem hiding this comment.
🧹 Nitpick comments (1)
apps/api/src/config/env.ts (1)
9-17: 🔒 Security & Privacy | 🔵 Trivial | ⚡ Quick winConsider validating each origin as a well-formed URL.
The transform splits/trims values but doesn't validate that each resulting entry is a valid URL, so a malformed
CORS_ORIGINenv value (e.g., missing scheme) would pass parsing and only fail silently at request time rather than failing fast at boot, consistent with the "fail-fast env validation on boot" guideline applied elsewhere in this file (e.g.DATABASE_URL/REDIS_URLnow use.url()).🔧 Proposed fix
CORS_ORIGIN: z .string() .transform((val) => val .split(",") .map((origin) => origin.trim()) - .filter(Boolean), + .filter(Boolean) + .map((origin) => { + z.string().url().parse(origin); + return origin; + }), ) .default("http://localhost:3000"),🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@apps/api/src/config/env.ts` around lines 9 - 17, The CORS_ORIGIN env parsing in env.ts only splits and trims values, so malformed origins can slip through and fail later at runtime. Update the CORS_ORIGIN schema to validate each parsed entry as a proper URL before returning it, similar to the fail-fast handling used for DATABASE_URL and REDIS_URL. Keep the transform logic in the CORS_ORIGIN field, but add per-item URL validation so boot-time config parsing rejects invalid origins immediately.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@apps/api/src/config/env.ts`:
- Around line 9-17: The CORS_ORIGIN env parsing in env.ts only splits and trims
values, so malformed origins can slip through and fail later at runtime. Update
the CORS_ORIGIN schema to validate each parsed entry as a proper URL before
returning it, similar to the fail-fast handling used for DATABASE_URL and
REDIS_URL. Keep the transform logic in the CORS_ORIGIN field, but add per-item
URL validation so boot-time config parsing rejects invalid origins immediately.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: c559f2dd-ba02-46ef-bc91-a71a847bd898
📒 Files selected for processing (1)
apps/api/src/config/env.ts
Summary by CodeRabbit
http://localhost:3000.