Spam bots and honey pots
Introduction
Section titled โIntroductionโOur application is now protected against Cross-Site Request Forgery (CSRF) attacks. ๐๐
In this step, we will add an extra layer of security to protect our application from spam bots.
How can we protect against spam bots online?
Section titled โHow can we protect against spam bots online?โTo protect against spam bots, we can hide a secret โhoneypotโ field in our forms.
A โhoneypotโ is a hidden field in a form that is invisible to users but visible to spam bots. When a spam bot fills out the form, it will fill in the โhoneypotโ field as well. We can then check if the โhoneypotโ field is filled in and reject the form submission if it is.
Letโs add this now.
Adding a honeypot field
Section titled โAdding a honeypot fieldโFirstly, add the following import to the top of the file:
import { type LinksFunction } from '@remix-run/node'import Document from '~/components/shared-layout/Document'import { useNonce } from '~/utils/nonce-provider.ts'import rootLinkElements from '~/utils/providers/rootLinkElements'import HeaderWithSearch from './components/organisms/HeaderWithSearch'import FooterMenuRight from './components/organisms/Footer/FooterMenuRight'import ThemeSwitch from './components/shared-layout/ThemeSwitch.tsx'import { loader } from './__root.server.tsx'import { Outlet, useLoaderData } from '@remix-run/react'import useTheme from './hooks/useTheme.tsx'import { AuthenticityTokenProvider } from 'remix-utils/csrf/react'import { HoneypotProvider } from 'remix-utils/honeypot/react'
Once in place, we can wrap our Document
and all its children with the HoneypotProvider
component:
export default function App() { const data = useLoaderData<typeof loader>() const theme = useTheme() const nonce = useNonce()
return ( <AuthenticityTokenProvider token={data.csrfToken}> <HoneypotProvider {...data.honeyProps}> <Document nonce={nonce} theme={theme}> <div className="flex h-screen flex-col justify-between"> <HeaderWithSearch />
<div className="flex-1"> <Outlet /> </div>
<div className="container flex justify-between pb-5"> <ThemeSwitch userPreference={data.requestInfo.userPrefs.theme} /> </div> <FooterMenuRight /> </div> </Document> </HoneypotProvider> </AuthenticityTokenProvider> )}
Checking the honeypot field
Section titled โChecking the honeypot fieldโWe can see the honeypot in action if we visit the http://localhost:3000/signup
page and inspect the form in our browserโs developer tools.
Can you see the hidden fields? ๐ต๏ธโโ๏ธ
Well, spam bots canโt! If they fill in this field, our application will now reject their submission.
And thatโs it! Our application is protected against spam bots. ๐ซ๐ค
Summary
Section titled โSummaryโAs with CSRF protection, adding a honeypot to our application is straightforward in Remix.
Letโs update your assignment to document this new security feature. ๐
Whatโs next?
Section titled โWhatโs next?โIn the next step, we will create a new user account on our local site using the signup form weโve just secured. ๐