Skip to content

security: harden slider_captcha against brute-force, replay, and bot attacks#2

Draft
BrianTran24 with Copilot wants to merge 2 commits into
mainfrom
copilot/improve-slider-captcha-security
Draft

security: harden slider_captcha against brute-force, replay, and bot attacks#2
BrianTran24 with Copilot wants to merge 2 commits into
mainfrom
copilot/improve-slider-captcha-security

Conversation

Copilot AI commented Apr 23, 2026

Copy link
Copy Markdown

The original implementation verified captcha answers entirely client-side with a predictable Random() PRNG, no attempt limits, no session binding, no movement analysis, and plain HTTP in the example service.

Changes

D — Cryptographically secure position generation

_RenderTestSliderCaptChar.create() now uses Random.secure() instead of Random(), making puzzle piece positions unpredictable.

C — Brute-force protection via maxAttempts

SliderCaptcha gains a maxAttempts parameter (default 5). On consecutive failures the widget auto-resets with a fresh random position and fires onConfirm(false) once.

SliderCaptcha(
  maxAttempts: 3,
  onConfirm: (passed) async { ... },
  ...
)

E — Drag-trail collection and bot heuristics

New SliderDragPoint / SliderMovementData classes (exported from the package) capture timestamped x-positions during every drag-update. Both SliderCaptcha and SliderCaptchaClient expose an onBehaviorData callback; callers forward the trail to their server for bot-detection.

SliderCaptcha(
  onBehaviorData: (data) {
    if (!data.isLikelyHuman()) rejectEarly();
    sendToServer(captchaId, data.trailToJson()); // server-side analysis
  },
  ...
)

isLikelyHuman() applies three client-side checks: minimum point count (≥5), minimum duration (≥300 ms), and non-zero velocity variance (threshold > 1e-10 to avoid float precision false negatives).

B — Session token (replay-attack prevention)

SliderCaptchaClientProvider accepts an optional captchaId field, populated from the server's requestID. Include it in the verification request body so the server can invalidate the token after a single use.

G — HTTPS enforcement in the example service

SliderCaptchaService now requires an explicit baseUrl (no default, to force conscious configuration) and asserts https:// at construction time:

// Throws ArgumentError immediately if baseUrl is plain HTTP
SliderCaptchaService(baseUrl: 'https://api.example.com');

The pre-existing broken Uri.https(fullUrl) call in postAnswer is also fixed.

Copilot AI and others added 2 commits April 23, 2026 17:36
…ptcha

- D: Use Random.secure() for cryptographically secure puzzle position generation
- C: Add maxAttempts parameter to SliderCaptcha; auto-reset captcha after N consecutive failures (default 5)
- E: Track drag trail (SliderDragPoint list + timestamps) during drag gesture; expose via optional onBehaviorData callback on both SliderCaptcha and SliderCaptchaClient; add SliderMovementData.isLikelyHuman() heuristic
- B: Add captchaId field to SliderCaptchaClientProvider so callers can bind each session to a server-side token and pass it in verification requests
- G: SliderCaptchaService now enforces HTTPS base URLs via _assertHttps(); old broken Uri.https() call fixed
- New tests for SliderMovementData heuristics and SliderCaptcha widget params"

Agent-Logs-Url: https://github.com/BrianTran24/slider_captcha/sessions/966034aa-e704-4a20-898e-efe63973d8e8

Co-authored-by: BrianTran24 <179514740+BrianTran24@users.noreply.github.com>
- Use variance > 1e-10 epsilon to avoid floating-point false negatives in bot heuristic
- Convert _SliderCaptchaComponent onBehaviorData to named optional parameter (consistent API)
- Remove default baseUrl from SliderCaptchaService constructor to force explicit configuration"

Agent-Logs-Url: https://github.com/BrianTran24/slider_captcha/sessions/966034aa-e704-4a20-898e-efe63973d8e8

Co-authored-by: BrianTran24 <179514740+BrianTran24@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants