-
Notifications
You must be signed in to change notification settings - Fork 64
feat: add honeypots and (mostly) invisible PoW captchas to highly spam prone forms #3488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
da876d7
feat: allow blocking users
tefkah 174fffa
feat: improve blocking users
tefkah 5c9d2bd
fix: lint, etc
tefkah 26dff7a
feat: allow extra keywords to be added by env var
tefkah aec4f86
Merge branch 'master' into tfk/spam-superadminuser
tefkah 1abb43d
fix: also stop showing user comment
tefkah 396f6bd
chore: merge
tefkah ab023fc
fix: lint
tefkah 7843395
fix: lint, fr
tefkah d75a949
feat: captchas and honeypots
tefkah 36f22c8
feat: improved captchas and honeypots
tefkah 3715229
fix: lint
tefkah 31b9f95
fix: send slack message on ban/unban
tefkah f7eab42
fix: skip captchas in test
tefkah e7f7c42
fix
tefkah 0428725
Merge branch 'main' into tfk/captcha-honeypot
tefkah 5cf9d94
fix: typecheck
tefkah 09ed4bf
Merge branch 'main' into tfk/captcha-honeypot
tefkah d23b55d
fix: allow superadmins to instaban from comments
tefkah 6127ddb
fix: improve messages
tefkah c0bc69e
Merge branch 'main' into tfk/captcha-honeypot
tefkah 3368dd8
fix: allow sorting and filtering spam users
tefkah 70b572e
fix: fix create pub button flow
tefkah 6e36379
fix: remove stupid test (bad claude)
tefkah 55cfd53
refactor: make honeypot helper more sensible
tefkah e4a9fa4
refactor: modify altcha loading in replies a bit
tefkah 408d810
chore: merge
tefkah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,199 @@ | ||
| import React, { forwardRef, useEffect, useImperativeHandle, useRef, useState } from 'react'; | ||
|
|
||
| import { usePageContext } from 'utils/hooks'; | ||
|
|
||
| export type AltchaRef = { | ||
| value: string | null; | ||
| verify: () => Promise<string>; | ||
| }; | ||
|
|
||
| type AltchaProps = { | ||
| challengeurl?: string; | ||
| auto?: 'off' | 'onfocus' | 'onload' | 'onsubmit'; | ||
| onStateChange?: (ev: Event | CustomEvent<{ payload?: string; state: string }>) => void; | ||
| style?: React.CSSProperties & Record<string, string>; | ||
| }; | ||
|
|
||
| const DEFAULT_CHALLENGE_URL = '/api/captcha/challenge'; | ||
|
|
||
| type WidgetElement = HTMLElement & AltchaWidgetMethods; | ||
|
|
||
| const Altcha = forwardRef<AltchaRef, AltchaProps>((props, ref) => { | ||
| const { challengeurl = DEFAULT_CHALLENGE_URL, auto, onStateChange, style } = props; | ||
| const { locationData } = usePageContext(); | ||
| const devMode = !locationData.isProd; | ||
| const widgetRef = useRef<WidgetElement | null>(null); | ||
| const [value, setValue] = useState<string | null>(null); | ||
| const [loaded, setLoaded] = useState(false); | ||
| const [simulateFailure, setSimulateFailure] = useState(false); | ||
| const [widgetKey, setWidgetKey] = useState(0); | ||
| const valueRef = useRef<string | null>(null); | ||
| valueRef.current = value; | ||
|
|
||
| useEffect(() => { | ||
| import('altcha').then(() => setLoaded(true)); | ||
| }, []); | ||
|
|
||
| const [altchaVisible, setAltchaVisible] = useState<boolean>(false); | ||
| // biome-ignore lint/correctness/useExhaustiveDependencies: widgetKey triggers re-attach after remount | ||
| useEffect(() => { | ||
| if (!loaded) return; | ||
| const w = widgetRef.current; | ||
| if (!w) return; | ||
| const handleStateChange = (ev: Event) => { | ||
| const e = ev as CustomEvent<{ payload?: string; state: string }>; | ||
| console.log('state changed', e.detail); | ||
|
|
||
| switch (e.detail.state) { | ||
| case 'error': | ||
| case 'code': | ||
| case 'unverified': | ||
| setAltchaVisible(true); | ||
| break; | ||
| case 'verifying': | ||
| if (devMode) { | ||
| setAltchaVisible(true); | ||
| } | ||
| break; | ||
| case 'verified': | ||
| if (e.detail.payload) { | ||
| setValue(e.detail.payload); | ||
| setAltchaVisible(false); | ||
| } | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
|
|
||
| onStateChange?.(e); | ||
| }; | ||
| w.addEventListener('statechange', handleStateChange); | ||
| return () => w.removeEventListener('statechange', handleStateChange); | ||
| }, [loaded, onStateChange, widgetKey]); | ||
|
|
||
| // biome-ignore lint/correctness/useExhaustiveDependencies: widgetKey triggers re-bind after remount | ||
| useImperativeHandle( | ||
| ref, | ||
| () => ({ | ||
| get value() { | ||
| return valueRef.current; | ||
| }, | ||
| verify(): Promise<string> { | ||
| const w = widgetRef.current; | ||
| if (!w) return Promise.reject(new Error('Altcha widget not mounted')); | ||
| const current = valueRef.current; | ||
| if (current) return Promise.resolve(current); | ||
| return new Promise((resolve, reject) => { | ||
| const handler = (ev: Event) => { | ||
| const e = ev as CustomEvent<{ payload?: string; state: string }>; | ||
| const state = e.detail?.state; | ||
| if (state === 'verified' && e.detail?.payload) { | ||
| w.removeEventListener('statechange', handler); | ||
| resolve(e.detail.payload); | ||
| return; | ||
| } | ||
| if (state === 'error' || state === 'expired') { | ||
| w.removeEventListener('statechange', handler); | ||
| reject(new Error('Captcha verification failed')); | ||
| } | ||
| }; | ||
| w.addEventListener('statechange', handler); | ||
| w.verify(); | ||
| }); | ||
| }, | ||
| }), | ||
| [widgetKey], | ||
| ); | ||
|
|
||
| const handleToggleFailure = () => { | ||
| setSimulateFailure((prev) => !prev); | ||
| setValue(null); | ||
| setWidgetKey((k) => k + 1); | ||
| }; | ||
|
|
||
| const handleReset = () => { | ||
| setValue(null); | ||
| widgetRef.current?.reset(); | ||
| }; | ||
|
|
||
| if (!loaded) return null; | ||
|
|
||
| const devAttrs = devMode ? { debug: true, floatingpersist: 'focus' as const } : {}; | ||
|
|
||
| const widget = ( | ||
| <React.Fragment key={widgetKey}> | ||
| <altcha-widget | ||
| delay={500} | ||
| ref={widgetRef as any} | ||
| challengeurl={challengeurl} | ||
| {...(auto ? { auto } : {})} | ||
| floating="auto" | ||
| {...devAttrs} | ||
| {...(simulateFailure ? { mockerror: true } : {})} | ||
| style={{ | ||
| display: altchaVisible ? 'block' : 'none', | ||
| zIndex: 1000, | ||
| ...(style ? ({ style } as any) : {}), | ||
| }} | ||
| // disable very annoying wait alert | ||
| strings="{"waitAlert":""}" | ||
| /> | ||
| </React.Fragment> | ||
| ); | ||
|
|
||
| if (!devMode) return widget; | ||
|
|
||
| return ( | ||
| <div | ||
| style={{ | ||
| display: 'flex', | ||
| alignItems: 'center', | ||
| gap: 6, | ||
| fontSize: 11, | ||
| color: '#5c7080', | ||
| padding: '2px 6px', | ||
| border: '1px dashed #5c7080', | ||
| borderRadius: 3, | ||
| }} | ||
| > | ||
| {widget} | ||
| <span style={{ fontWeight: 600 }}>Captcha</span> | ||
| <label | ||
| style={{ | ||
| cursor: 'pointer', | ||
| display: 'inline-flex', | ||
| alignItems: 'center', | ||
| gap: 3, | ||
| color: simulateFailure ? '#db3737' : undefined, | ||
| }} | ||
| > | ||
| <input | ||
| type="checkbox" | ||
| checked={simulateFailure} | ||
| onChange={handleToggleFailure} | ||
| style={{ margin: 0 }} | ||
| /> | ||
| fail | ||
| </label> | ||
| <button | ||
| type="button" | ||
| onClick={handleReset} | ||
| style={{ | ||
| fontSize: 11, | ||
| padding: '1px 6px', | ||
| cursor: 'pointer', | ||
| border: '1px solid #ced9e0', | ||
| borderRadius: 3, | ||
| background: 'white', | ||
| lineHeight: '16px', | ||
| }} | ||
| > | ||
| reset | ||
| </button> | ||
| </div> | ||
| ); | ||
| }); | ||
|
|
||
| Altcha.displayName = 'Altcha'; | ||
|
|
||
| export default Altcha; | ||
This file contains hidden or 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 @@ | ||
| export { type AltchaRef, default } from './Altcha'; |
This file contains hidden or 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 hidden or 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,54 @@ | ||
| import React from 'react'; | ||
|
|
||
| import { usePageContext } from 'utils/hooks'; | ||
|
|
||
| import './honeypot.scss'; | ||
|
|
||
| type HoneypotProps = { | ||
| name: string; | ||
| }; | ||
|
|
||
| const Honeypot = (props: HoneypotProps) => { | ||
| const { name } = props; | ||
| const { locationData } = usePageContext(); | ||
| const devMode = !locationData.isProd; | ||
|
|
||
| if (devMode) { | ||
| return ( | ||
| <label | ||
| style={{ | ||
| display: 'inline-flex', | ||
| alignItems: 'center', | ||
| gap: 4, | ||
| fontSize: 11, | ||
| color: 'orange', | ||
| fontWeight: 600, | ||
| padding: '2px 6px', | ||
| border: '1px dashed orange', | ||
| borderRadius: 3, | ||
| }} | ||
| > | ||
| Honeypot | ||
| <input | ||
| type="text" | ||
| name={name} | ||
| autoComplete="off" | ||
| style={{ width: 80, fontSize: 11, padding: '1px 4px' }} | ||
| /> | ||
| </label> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <input | ||
| type="text" | ||
| className="honeypot-input" | ||
| name={name} | ||
| tabIndex={-1} | ||
| autoComplete="off" | ||
| aria-hidden="true" | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| export default Honeypot; |
This file contains hidden or 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,8 @@ | ||
| .honeypot-input { | ||
| position: absolute; | ||
| left: -9999px; | ||
| width: 1px; | ||
| height: 1px; | ||
| opacity: 0; | ||
| pointer-events: none; | ||
| } |
This file contains hidden or 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 @@ | ||
| export { default } from './Honeypot'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.