Skip to content

Spam bots and honey pots

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.

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.

Spam bot honeypot attack

Letโ€™s add this now.

Firstly, add the following import to the top of the file:

app/root.tsx
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:

app/root.tsx
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>
)
}

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.

Honeypot field

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. ๐Ÿšซ๐Ÿค–

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. ๐Ÿš€

In the next step, we will create a new user account on our local site using the signup form weโ€™ve just secured. ๐ŸŽ‰