feat: migrate chat delete flow to dedicated api#1612
feat: migrate chat delete flow to dedicated api#1612arpitgupta1214 wants to merge 1 commit intotestfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
📝 WalkthroughWalkthroughThe changes migrate room deletion from a local Next.js API route to an external API endpoint. The deprecated local handler is removed, and the client component is updated to call the new authenticated external service with proper bearer token authorization. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ❌ 1❌ Failed checks (1 warning)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@components/Sidebar/Modals/DeleteConfirmationModal.tsx`:
- Around line 27-28: The code uses apiOverride (from useApiOverride) directly as
baseUrl (falling back to NEW_API_BASE_URL) and may send the bearer token to
attacker-controlled origins; update DeleteConfirmationModal to validate and
sanitize apiOverride before using it for authenticated requests: allow only
same-origin or a small whitelist of trusted origins (or require a relative
path), reject or ignore overrides that parse to an external origin, and ensure
when an override is rejected you fall back to NEW_API_BASE_URL; apply the same
validation wherever baseUrl is computed from useApiOverride (e.g., the other
baseUrl usages referenced).
🪄 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: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: cee8c176-93d6-4dfe-bb52-b61884a27536
📒 Files selected for processing (2)
app/api/room/delete/route.tscomponents/Sidebar/Modals/DeleteConfirmationModal.tsx
💤 Files with no reviewable changes (1)
- app/api/room/delete/route.ts
| const apiOverride = useApiOverride(); | ||
| const baseUrl = apiOverride || NEW_API_BASE_URL; |
There was a problem hiding this comment.
Block untrusted API override origins before sending bearer token.
apiOverride is sourced from URL/session storage and directly used as baseUrl for authenticated requests. A crafted ?api= value can exfiltrate bearer tokens to attacker-controlled origins.
🔐 Suggested hardening
+const TRUSTED_API_ORIGINS = new Set([
+ new URL(NEW_API_BASE_URL).origin,
+ // Optional: allow explicit extra origin(s) via env, comma-separated
+ ...(process.env.NEXT_PUBLIC_TRUSTED_API_ORIGINS ?? "")
+ .split(",")
+ .map((v) => v.trim())
+ .filter(Boolean),
+]);
+
const DeleteConfirmationModal = ({ isOpen, onClose, chatRoom, chatRooms, onDelete }: DeleteConfirmationModalProps) => {
@@
const handleDelete = async () => {
@@
+ let requestBaseUrl = baseUrl;
+ try {
+ const origin = new URL(requestBaseUrl).origin;
+ if (!TRUSTED_API_ORIGINS.has(origin)) {
+ setError("Invalid API endpoint configuration.");
+ return;
+ }
+ } catch {
+ setError("Invalid API endpoint configuration.");
+ return;
+ }
+
@@
- const response = await fetch(`${baseUrl}/api/chats`, {
+ const response = await fetch(`${requestBaseUrl}/api/chats`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({ id: roomId }),
});As per coding guidelines, "Implement built-in security practices for authentication and data handling."
Also applies to: 76-81
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@components/Sidebar/Modals/DeleteConfirmationModal.tsx` around lines 27 - 28,
The code uses apiOverride (from useApiOverride) directly as baseUrl (falling
back to NEW_API_BASE_URL) and may send the bearer token to attacker-controlled
origins; update DeleteConfirmationModal to validate and sanitize apiOverride
before using it for authenticated requests: allow only same-origin or a small
whitelist of trusted origins (or require a relative path), reject or ignore
overrides that parse to an external origin, and ensure when an override is
rejected you fall back to NEW_API_BASE_URL; apply the same validation wherever
baseUrl is computed from useApiOverride (e.g., the other baseUrl usages
referenced).
Summary
POST /api/room/deleteto dedicatedDELETE /api/chatsuseAccessTokenuseApiOverrideapp/api/room/delete/route.tsValidation
pnpm exec eslint components/Sidebar/Modals/DeleteConfirmationModal.tsxSummary by CodeRabbit