Skip to content
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

Integrating custom bot detection into frictionless captcha #1282

Merged
merged 10 commits into from
Jun 18, 2024
1,854 changes: 1,112 additions & 742 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/detector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 0",
"lint:fix": "echo \"Not linting @prosopo/obf-bot\" && exit 0",
"lint": "echo \"Not linting @prosopo/obf-bot\" && exit 0"
"lint": "echo \"Not linting @prosopo/obf-bot\" && exit 0",
"build": "tsc"
},
"dependencies": {},
"devDependencies": {
"typescript": "5.3.2"
},
Expand Down
58 changes: 58 additions & 0 deletions packages/detector/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
export function isBot(): Promise<{
fingerprint: {
resistance:
| {
privacy: undefined
security: undefined
mode: undefined
extension: undefined
engine: any

Check warning on line 9 in packages/detector/src/index.d.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
}
| undefined
headlessFeaturesFingerprint:
| {
likeHeadlessRating: number
headlessRating: number
stealthRating: number
systemFonts: string
platformEstimate: any[]

Check warning on line 18 in packages/detector/src/index.d.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
chromium: boolean
likeHeadless: {
noChrome: boolean
hasPermissionsBug: boolean
noPlugins: boolean
noMimeTypes: boolean
notificationIsDenied: boolean
hasKnownBgColor: boolean
prefersLightColor: any

Check warning on line 27 in packages/detector/src/index.d.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
uaDataIsBlank: boolean
pdfIsDisabled: boolean
noTaskbar: boolean
hasVvpScreenRes: boolean
hasSwiftShader: any

Check warning on line 32 in packages/detector/src/index.d.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
noWebShare: any

Check warning on line 33 in packages/detector/src/index.d.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
noContentIndex: boolean
noContactsManager: boolean
noDownlinkMax: boolean
}
headless: {
webDriverIsOn: any

Check warning on line 39 in packages/detector/src/index.d.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
hasHeadlessUA: any

Check warning on line 40 in packages/detector/src/index.d.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
hasHeadlessWorkerUA: any

Check warning on line 41 in packages/detector/src/index.d.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
}
stealth: {
hasIframeProxy: boolean
hasHighChromeIndex: any

Check warning on line 45 in packages/detector/src/index.d.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
hasBadChromeRuntime: boolean
hasToStringProxy: boolean
hasBadWebGL: any

Check warning on line 48 in packages/detector/src/index.d.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
}
}
| undefined
}
isBotBotD: any
botScore: any
isBot: boolean
botType: any
}>
//# sourceMappingURL=index.d.ts.map
3,243 changes: 3,242 additions & 1 deletion packages/detector/src/index.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions packages/detector/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.esm.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist"
},
"include": ["src"]
}
2 changes: 1 addition & 1 deletion packages/procaptcha-frictionless/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"> 0.5%, last 2 versions, not dead"
],
"dependencies": {
"@fingerprintjs/botd": "^1.9.0",
"@prosopo/detector": "1.0.2",
"@prosopo/procaptcha-pow": "1.0.2",
"@prosopo/procaptcha-react": "1.0.2",
"@prosopo/types": "1.0.2",
Expand Down
27 changes: 18 additions & 9 deletions packages/procaptcha-frictionless/src/ProcaptchaFrictionless.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,36 @@
import { Procaptcha } from '@prosopo/procaptcha-react'
import { ProcaptchaPlaceholder } from '@prosopo/web-components'
import { ProcaptchaPow } from '@prosopo/procaptcha-pow'
import { ProcaptchaProps } from '@prosopo/types'
import { load } from '@fingerprintjs/botd'
import { useEffect, useState } from 'react'
import { BotDetectionFunction, ProcaptchaFrictionlessProps } from '@prosopo/types'

Check failure on line 18 in packages/procaptcha-frictionless/src/ProcaptchaFrictionless.tsx

View workflow job for this annotation

GitHub Actions / check

Imports should be sorted alphabetically
import { isBot } from '@prosopo/detector'

export const ProcaptchaFrictionless = ({ config, callbacks }: ProcaptchaProps) => {
// Use state to manage which component to render
const customDetectBot: BotDetectionFunction = async () => {
return await isBot().then((result) => {
const bot = result.isBot
return { bot }
})
}

export const ProcaptchaFrictionless = ({
config,
callbacks,
detectBot = customDetectBot,
}: ProcaptchaFrictionlessProps) => {
const [componentToRender, setComponentToRender] = useState(<ProcaptchaPlaceholder darkMode={config.theme} />)

useEffect(() => {
const detectBot = async () => {
const botd = await load()
const result = botd.detect()
const detectAndSetComponent = async () => {
const result = await detectBot()
if (result.bot) {
setComponentToRender(<Procaptcha config={config} callbacks={callbacks} />)
} else {
setComponentToRender(<ProcaptchaPow config={config} callbacks={callbacks} />)
}
}

detectBot()
}, [config, callbacks])
detectAndSetComponent()
}, [config, callbacks, detectBot])

return componentToRender
}
6 changes: 5 additions & 1 deletion packages/procaptcha-frictionless/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
"lib": ["es6", "dom"]
"lib": ["es6", "dom"],
"allowJs": true
},
"include": ["src", "src/**/*.json", "src/index.html"],
"references": [
{
"path": "../../dev/config"
},
{
"path": "../detector"
},
{
"path": "../procaptcha-react"
},
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ export * from './datasets/index.js'
export * from './provider/index.js'
export * from './procaptcha/index.js'
export * from './procaptcha-bundle/index.js'
export * from './procaptcha-frictionless/index.js'
export { default as networks } from './networks/index.js'
export type { Hash, AccountId } from '@prosopo/captcha-contract/types-arguments'
14 changes: 14 additions & 0 deletions packages/types/src/procaptcha-frictionless/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2021-2024 Prosopo (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
export * from './props.js'
23 changes: 23 additions & 0 deletions packages/types/src/procaptcha-frictionless/props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2021-2024 Prosopo (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { ProcaptchaProps } from '../procaptcha/props.js'

export type BotDetectionFunction = () => Promise<{ bot: boolean }>

/**
* The props for the Procaptcha Frictionless component.
*/
export interface ProcaptchaFrictionlessProps extends ProcaptchaProps {
detectBot?: BotDetectionFunction
}
Loading