🛡️ CSRF Protection & Scraping Prevention Issue

Hey everyone, I’m facing a security challenge in my Next.js app:

  1. Current Setup:

    • Using @edge-csrf/nextjs for CSRF protection
    • CSRF token generated in middleware
    • Token passed to client via headers and cookies
  2. The Problem:

    • Python scraper still gets 200 responses or copies requests from the network tab and tests it
    • Need to prevent successful scraping attempts
  3. What I’ve Tried:

    • Implementing dynamic CSRF token generation
    • Adding custom headers to detect scraping
  4. What I Need:

    • Robust solution to block scraping attempts
    • Ensure CSRF protection works against automated tools
    • Next.js-specific best practices for this scenario

Any ideas on how to strengthen our defenses? Thanks in advance! :pray:

middleware.ts

const csrfProtect = createCsrfProtect({
  cookie: {
    secure: process.env.NODE_ENV === "production",
    sameSite: "strict",
  },
});

// Next.js middleware function
export const middleware = async (request: NextRequest) => {
  const response = NextResponse.next();

  try {
    await csrfProtect(request, response);
  } catch (err) {
    if (err instanceof CsrfError)
      return new NextResponse("invalid csrf token", { status: 403 });
    throw err;
  }

  return response;
};

search/page.tsx

export default function SearcPage() {
  const csrfToken = headers().get("X-CSRF-Token") || "missing";
  return <SearchClientPage csrfToken={csrfToken} />;
}

SearchClientPage .tsx

const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
    event.preventDefault();
    let formData = new FormData(event.currentTarget);
    formData.set("csrf_token", csrfToken);
    formData.set("from", "search");
    formAction(formData);
  };

Response from copy request from network tab and test it with PowerShell or py

1:{"success":true,"message":"­","data":{.....data}}}

Hi, @m0mahmoud! Welcome to the Vercel Community :smile:

Appreciate all the detail here! I actually ended up asking v0.dev for some guidance and this is what it got back to me with:

https://v0.dev/chat/BK9i35CI6aC

Let me know if that helps!

2 Likes

Thanks for your message

1 Like

Let us know how you get on :smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.