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

DataDome edge-middleware - fix searchParams in payload #896

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
f2b825b
Update DataDome module
MickaelDatadome Jan 23, 2024
d0ff4eb
Update DataDome module
MickaelDatadome Jan 23, 2024
0227d35
Fix type issue
MickaelDatadome Jan 24, 2024
55365f7
Fix NextResponse type for response
MickaelDatadome Jan 24, 2024
06435d7
Fix variable name
MickaelDatadome Jan 24, 2024
f167daf
remove strict in tsconfig.json
MickaelDatadome Feb 6, 2024
bacb5ac
fix: renamed variables, reorder payload fields
MickaelDatadome Feb 7, 2024
b90dae5
try to clone the DD response
MickaelDatadome Feb 7, 2024
cdf9a7a
test with try
MickaelDatadome Feb 7, 2024
69d3c13
test with Response constructor
MickaelDatadome Feb 7, 2024
ebf54d0
Clean up
MickaelDatadome Feb 7, 2024
bbb0a26
fix readme
MickaelDatadome Feb 7, 2024
8a2c15f
Add `DATADOME_ENDPOINT` as an environment variable
MickaelDatadome Feb 7, 2024
391b862
Merge branch 'main' into main
MickaelDatadome Feb 7, 2024
2f4d0af
Fix variable name
MickaelDatadome Feb 8, 2024
5e14206
Merge branch 'main' of https://github.com/MickaelDatadome/vercel_exam…
MickaelDatadome Feb 8, 2024
5764695
Update version to stable `1.0.0`
MickaelDatadome Feb 8, 2024
82a772c
Add truncation, Fix cookieLength calculation
MickaelDatadome Feb 9, 2024
6302e9d
Merge branch 'main' into main
MickaelDatadome Feb 9, 2024
fd8841f
fix - force https by default on endpoint
MickaelDatadome Feb 9, 2024
cfa2a60
add support for `TrueClientIP`
MickaelDatadome Feb 9, 2024
ed9fb30
fix cookie length
MickaelDatadome Feb 9, 2024
00e4cf2
review module version
MickaelDatadome Feb 9, 2024
fd43187
Fix client side visibility for variable
MickaelDatadome Feb 15, 2024
1ee0f3f
Update .env.example to require client side key
MickaelDatadome Feb 15, 2024
9e1b414
Fix - Clean environment variables example file.
MickaelDatadome Feb 23, 2024
03525f0
Merge branch 'vercel:main' into main
MickaelDatadome Apr 5, 2024
2df66e9
Fix inconsistent searchParams in payload
MickaelDatadome Apr 5, 2024
beeb35a
Merge branch 'main' into main
MickaelDatadome Apr 8, 2024
0d70206
fix sessionbyheader cookie value
MickaelDatadome Apr 9, 2024
e6ced35
configure sessionByHeader
MickaelDatadome Apr 9, 2024
cf4489f
Add ajaxListenerPath
MickaelDatadome Apr 9, 2024
ed2527d
Merge branch 'vercel:main' into main
MickaelDatadome Apr 9, 2024
d15a242
Merge branch 'main' into main
MickaelDatadome Apr 12, 2024
6689e8f
Merge branch 'main' into main
MickaelDatadome May 10, 2024
ed9ebd3
Fix typescript strict mode
MickaelDatadome May 10, 2024
2af0841
Merge branch 'main' into main
MickaelDatadome May 28, 2024
ae4ddfe
Merge branch 'main' into main
MickaelDatadome Jun 6, 2024
e81bbd6
Merge branch 'main' into main
MickaelDatadome Jun 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 8 additions & 13 deletions edge-middleware/bot-protection-datadome/lib/datadome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default async function datadome(req: NextRequest) {
? req.headers.get('x-forwarded-for')!.split(',')[0]
: '127.0.0.1',
RequestModuleName: 'Next.js',
ModuleVersion: '0.3.0',
ModuleVersion: '0.3.1',
AuthorizationLen: getAuthorizationLength(req),
Accept: req.headers.get('accept'),
AcceptEncoding: req.headers.get('accept-encoding'),
Expand All @@ -43,11 +43,11 @@ export default async function datadome(req: NextRequest) {
PostParamLen: req.headers.get('content-length'),
Protocol: req.headers.get('x-forwarded-proto'),
Referer: req.headers.get('referer'),
Request: pathname + encode(Object.fromEntries(req.nextUrl.searchParams)),
Request: pathname + req.nextUrl.search,
MickaelDatadome marked this conversation as resolved.
Show resolved Hide resolved
ServerHostname: req.headers.get('host'),
ServerName: 'vercel',
ServerRegion: 'sfo1',
TimeRequest: new Date().getTime() * 1000,
TimeRequest: Date.now() * 1000,
TrueClientIP: req.headers.get('true-client-ip'),
UserAgent: req.headers.get('user-agent'),
Via: req.headers.get('via'),
Expand All @@ -69,16 +69,19 @@ export default async function datadome(req: NextRequest) {

const options = {
method: 'POST',
body: stringify(truncateRequestData(requestData)),
body: '',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'DataDome'
},
}
if (req.headers.get('x-datadome-clientid')?.length) {
//@ts-ignore - custom DataDome error
options.headers['X-DataDome-X-Set-Cookie'] = 'true'
requestData.ClientID = req.headers.get('x-datadome-clientid') as string
}
options.body = stringify(truncateRequestData(requestData));

const dataDomeReq = fetch(DATADOME_ENDPOINT + '/validate-request/', options)

const timeoutPromise = new Promise((resolve, reject) => {
Expand Down Expand Up @@ -153,15 +156,6 @@ export default async function datadome(req: NextRequest) {
}
}

function encode(query: Record<string, string>) {
let e = ''
for (const k in query) {
const v = query[k]
e += `${encodeURIComponent(k)}=${encodeURIComponent(v)}`
}
return e
}

function toHeaders(
reqHeaders: Headers,
dataDomeResHeaders: Headers,
Expand Down Expand Up @@ -261,6 +255,7 @@ function truncateRequestData(requestData: Record<string, string | number | null

for (let key in requestData) {
const value = requestData[key];
//@ts-ignore
const limit = limits[key.toLowerCase()];
if (limit && value && typeof value == 'string' && value.length > Math.abs(limit)) {
if (limit > 0) {
Expand Down
4 changes: 3 additions & 1 deletion edge-middleware/bot-protection-datadome/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ function MyApp({ Component, pageProps }: AppProps) {
<Script strategy="lazyOnload" id="load-datadome">{`
window.ddjskey = '${process.env.NEXT_PUBLIC_DATADOME_CLIENT_SIDE_KEY}'
window.ddoptions = {
endpoint: '${DATADOME_JS}'
endpoint: '${DATADOME_JS}',
sessionByHeader: true,
ajaxListenerPath: true
}
`}</Script>
<Script src={DATADOME_TAGS} strategy="lazyOnload" />
Expand Down
2 changes: 1 addition & 1 deletion edge-middleware/bot-protection-datadome/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
Expand Down