@nuasite/components 0.47.2 → 0.47.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@nuasite/components",
3
3
  "description": "Nua Site astro components.",
4
4
  "license": "Apache-2.0",
5
- "version": "0.47.2",
5
+ "version": "0.47.4",
6
6
  "files": [
7
7
  "dist/**",
8
8
  "src/**",
@@ -127,6 +127,7 @@ const tokenId = `${tokenFieldName}_${formId}`
127
127
  private startTime = Date.now();
128
128
  private honeypotFieldNames: string[] = []
129
129
  private abortController = new AbortController()
130
+ private tokenRefreshInterval: ReturnType<typeof setInterval> | undefined
130
131
 
131
132
  connectedCallback() {
132
133
  const form = this.querySelector('form')
@@ -219,9 +220,7 @@ const tokenId = `${tokenFieldName}_${formId}`
219
220
  let lastHumanityFailure: string | null = null
220
221
  let bypassHumanityCheck = false
221
222
 
222
- const refreshSubmissionGuards = () => {
223
- activationTime = performance.now()
224
-
223
+ const refreshToken = () => {
225
224
  if (tokenInput) {
226
225
  const { value } = createTokenValue()
227
226
  tokenInput.value = value
@@ -229,8 +228,20 @@ const tokenId = `${tokenFieldName}_${formId}`
229
228
  }
230
229
  }
231
230
 
231
+ const refreshSubmissionGuards = () => {
232
+ activationTime = performance.now()
233
+ refreshToken()
234
+ }
235
+
232
236
  refreshSubmissionGuards()
233
237
 
238
+ // The server rejects tokens older than its expiry window — keep the token fresh
239
+ // for long-open tabs and pages restored from the back/forward cache
240
+ this.tokenRefreshInterval = setInterval(refreshToken, 4 * 60 * 1000)
241
+ window.addEventListener('pageshow', (e) => {
242
+ if (e.persisted) refreshToken()
243
+ }, { signal: this.abortController.signal })
244
+
234
245
  const trackForm = (action: string, detail?: Record<string, unknown>) => {
235
246
  if (typeof window !== 'undefined' && (window as any).analytics?.form) {
236
247
  (window as any).analytics.form(formId, action, detail)
@@ -240,7 +251,6 @@ const tokenId = `${tokenFieldName}_${formId}`
240
251
  submitAnywayButton?.addEventListener('click', () => {
241
252
  bypassHumanityCheck = true
242
253
  submitAnywayButton.classList.add('hidden')
243
- activationTime = performance.now()
244
254
  form.requestSubmit()
245
255
  }, { signal: this.abortController.signal })
246
256
 
@@ -255,13 +265,17 @@ const tokenId = `${tokenFieldName}_${formId}`
255
265
 
256
266
  trackForm('attempt', { formData: formDataForTracking })
257
267
 
258
- if (bypassHumanityCheck) {
268
+ const bypassed = bypassHumanityCheck
269
+ if (bypassed) {
259
270
  trackForm('humanity_override', {
260
271
  reason: `accepted_after_humanity_warning:${lastHumanityFailure ?? 'unknown'}`,
261
272
  formData: formDataForTracking,
262
273
  })
263
274
  bypassHumanityCheck = false
264
275
  lastHumanityFailure = null
276
+ // The user explicitly confirmed — drop autofilled honeypot values so the
277
+ // server accepts the submission as fully valid
278
+ this.clearHoneypotFields(form)
265
279
  } else {
266
280
  const humanityFailure = this.checkHumanity(form)
267
281
  if (humanityFailure) {
@@ -273,7 +287,7 @@ const tokenId = `${tokenFieldName}_${formId}`
273
287
  }
274
288
 
275
289
  const elapsed = performance.now() - activationTime
276
- if (minSubmitDelay > 0 && elapsed < minSubmitDelay) {
290
+ if (!bypassed && minSubmitDelay > 0 && elapsed < minSubmitDelay) {
277
291
  trackForm('reject', { reason: 'submitted_too_fast', formData: formDataForTracking })
278
292
  showError(fastSubmitMessage || tryAgainMessage)
279
293
  return
@@ -289,6 +303,14 @@ const tokenId = `${tokenFieldName}_${formId}`
289
303
 
290
304
  showLoading()
291
305
 
306
+ // The server rejects tokens younger than its minimum submit delay — if the
307
+ // token was refreshed moments ago, wait out the remainder before sending
308
+ const tokenIssuedAt = tokenInput ? Number(tokenInput.value.split(':')[1]) : NaN
309
+ const tokenAge = Date.now() - tokenIssuedAt
310
+ if (Number.isFinite(tokenAge) && tokenAge >= 0 && tokenAge < 700) {
311
+ await new Promise(resolve => setTimeout(resolve, 700 - tokenAge))
312
+ }
313
+
292
314
  try {
293
315
  const response = await fetch(form.action, {
294
316
  method: form.method || 'POST',
@@ -353,6 +375,9 @@ const tokenId = `${tokenFieldName}_${formId}`
353
375
 
354
376
  disconnectedCallback() {
355
377
  this.abortController.abort()
378
+ if (this.tokenRefreshInterval !== undefined) {
379
+ clearInterval(this.tokenRefreshInterval)
380
+ }
356
381
  }
357
382
 
358
383
  private getInteractionScore(): number {
@@ -371,8 +396,6 @@ const tokenId = `${tokenFieldName}_${formId}`
371
396
 
372
397
  // WebDriver API presence (set to true in automated browsers)
373
398
  if (navigator.webdriver === true) return 'webdriver'
374
- // Empty plugins list is only suspicious on desktop — mobile browsers normally have none
375
- if (!isMobile && !window.navigator.plugins.length) return 'no_plugins'
376
399
  if (!window.navigator.languages.length) return 'no_languages'
377
400
 
378
401
  if (ua.includes('HeadlessChrome')) return 'headless_chrome'
@@ -387,6 +410,15 @@ const tokenId = `${tokenFieldName}_${formId}`
387
410
  return null
388
411
  }
389
412
 
413
+ private clearHoneypotFields(form: HTMLFormElement) {
414
+ for (const fieldName of this.honeypotFieldNames) {
415
+ const field = form.querySelector(`[name="${fieldName}"]`) as HTMLInputElement | null
416
+ if (field) field.value = ''
417
+ }
418
+ const cssHoneypot = form.querySelector('input[style*="position: absolute; left: -9999px"]') as HTMLInputElement | null
419
+ if (cssHoneypot) cssHoneypot.value = ''
420
+ }
421
+
390
422
  private checkHumanity(form: HTMLFormElement): string | null {
391
423
  const timeSpent = Date.now() - this.startTime;
392
424