-
-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add rate limiting and form validation for bookmark submissions
- Loading branch information
1 parent
25f8af4
commit 4979d9d
Showing
16 changed files
with
258 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,7 @@ | ||
'use server' | ||
|
||
import { cookies } from 'next/headers' | ||
|
||
import { BOOKMARK_SUBMISSION_COUNT_COOKIE_NAME, MAX_BOOKMARK_SUBMISSIONS_PER_DAY } from '@/lib/constants' | ||
import { getBookmarkItems } from '@/lib/raindrop' | ||
|
||
export async function submitBookmark(formData) { | ||
const cookieStore = await cookies() | ||
|
||
// Fake promise to simulate submitting the form | ||
await new Promise((resolve) => setTimeout(resolve, 2000)) | ||
|
||
const formSubmissionCountCookie = cookieStore.get(BOOKMARK_SUBMISSION_COUNT_COOKIE_NAME) | ||
if (formSubmissionCountCookie?.value >= MAX_BOOKMARK_SUBMISSIONS_PER_DAY) { | ||
throw new Error('You have reached the maximum number of submissions for today.') | ||
} | ||
|
||
try { | ||
const response = await fetch( | ||
`https://api.airtable.com/v0/${process.env.AIRTABLE_BASE_ID}/${process.env.AIRTABLE_BOOKMARKS_TABLE_ID}`, | ||
{ | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${process.env.AIRTABLE_PERSONAL_ACCESS_TOKEN}` | ||
}, | ||
body: JSON.stringify({ | ||
fields: { | ||
URL: formData.url, | ||
Email: formData.email, | ||
Date: new Date().toISOString(), | ||
Type: formData.type || 'Other' | ||
} | ||
}), | ||
signal: AbortSignal.timeout(5000) | ||
} | ||
) | ||
|
||
cookieStore.set( | ||
formSubmissionCountCookie?.name ?? BOOKMARK_SUBMISSION_COUNT_COOKIE_NAME, // Name | ||
Number(formSubmissionCountCookie?.value ?? 0) + 1, // Value | ||
{ | ||
maxAge: 60 * 60 * 24 // 24 hours | ||
} | ||
) | ||
|
||
const data = await response.json() | ||
return data | ||
} catch (error) { | ||
console.info(error) | ||
throw new Error('Failed to submit bookmark') | ||
} | ||
} | ||
|
||
export async function getBookmarkItemsByPageIndex(id, pageIndex) { | ||
return await getBookmarkItems(id, pageIndex) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import ip from '@arcjet/ip' | ||
import { isbot } from 'isbot' | ||
import { NextResponse } from 'next/server' | ||
|
||
import { formSchema } from '@/components/submit-bookmark/utils' | ||
import rateLimit from '@/lib/rate-limit' | ||
|
||
const limiter = rateLimit({ | ||
interval: 600 * 1000, // 10 minutes (600 seconds * 1000 ms) | ||
uniqueTokenPerInterval: 500 // Max 500 IPs | ||
}) | ||
|
||
export async function POST(req) { | ||
const json = await req.json() | ||
const data = await formSchema.safeParse(json) | ||
if (!data.success) { | ||
const { error } = data | ||
return NextResponse.json({ error }, { status: 400 }) | ||
} | ||
|
||
if (isbot(req.headers.get('User-Agent'))) { | ||
return NextResponse.json({ error: 'Bots are not allowed.' }, { status: 403 }) | ||
} | ||
|
||
// Use the @arcjet/ip package to get the client's IP address. This looks at | ||
// the headers set by different hosting platforms to try and get the real IP | ||
// address before falling back to the request's remote address. This is | ||
// necessary because the IP headers could be spoofed. In non-production | ||
// environments we allow private/internal IPs. | ||
const clientIp = ip(req, req.headers) | ||
|
||
try { | ||
await limiter.check(5, clientIp) // Limit to 5 requests | ||
} catch { | ||
return NextResponse.json({ error: 'Rate limit exceeded. Try again later.' }, { status: 429 }) | ||
} | ||
|
||
try { | ||
const { url, email, type } = data.data | ||
|
||
const response = await fetch( | ||
`https://api.airtable.com/v0/${process.env.AIRTABLE_BASE_ID}/${process.env.AIRTABLE_BOOKMARKS_TABLE_ID}`, | ||
{ | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${process.env.AIRTABLE_PERSONAL_ACCESS_TOKEN}` | ||
}, | ||
body: JSON.stringify({ | ||
fields: { | ||
URL: url, | ||
Email: email, | ||
Date: new Date().toISOString(), | ||
Type: type || 'Other' | ||
} | ||
}) | ||
} | ||
) | ||
|
||
const res = await response.json() | ||
return NextResponse.json({ res }) | ||
} catch (error) { | ||
console.info(error) | ||
return NextResponse.json({ error: 'Error submitting bookmark.' }, { status: 500 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { z } from 'zod' | ||
|
||
export const formSchema = z.object({ | ||
url: z.string().url({ | ||
message: 'Invalid URL.' | ||
}), | ||
email: z.string().email({ | ||
message: 'Invalid email address.' | ||
}), | ||
type: z.string().optional().or(z.literal('')) | ||
}) |
Oops, something went wrong.