geronimo-captcha 2.0.0

A JavaScript-free CAPTCHA for Rust. A speed bump for bulk automation, layered over server-side rate limiting. Not a wall.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use crate::SpriteFormat;
use crate::error::{CaptchaError, Result};

use image::{ExtendedColorType, RgbImage, Rgba, RgbaImage, imageops};
use imageproc::geometric_transformations::{Border, Interpolation, Projection, warp_into};
use rand::RngExt;
use webp::{Encoder as WebPEncoder, WebPConfig};

const MAX_NOISE_COUNT: u32 = 1_000_000;
const MAX_NOISE_SIZE: u32 = 64;
const MAX_NOISE_WORK: u64 = 4_000_000;
const MAX_BLUR_SIGMA: f32 = 32.0;

// libwebp effort level, valid range 0..=6.
const WEBP_METHOD: i32 = 1;

// JPEG at the qualities this crate encodes lands near 0.2 bytes per pixel.
const JPEG_BYTES_PER_PIXEL_SHIFT: usize = 4;

#[derive(Clone, Copy, Debug, Default)]
pub enum NoisePattern {
    Dots,
    Lines,
    #[default]
    Grid,
}

/// Parameters for the noise watermark drawn over
/// the composed sprite.
#[derive(Clone, Copy, Debug)]
pub struct NoiseOptions {
    /// Number of noise marks. Capped at 1_000_000.
    /// `count` multiplied by the pixels one mark
    /// covers (1, `size`, or `size` squared, by
    /// pattern) must not exceed 4_000_000.
    pub count: u32,

    /// Edge length of one mark, in pixels,
    /// for [`NoisePattern::Lines`]
    /// and [`NoisePattern::Grid`].
    /// Capped at 64.
    pub size: u32,

    /// Gaussian blur applied after the noise.
    /// Zero disables it. Capped at 32.0.
    pub blur_sigma: f32,

    /// Opacity of each mark, 0 (invisible) to 255 (opaque).
    /// Marks are alpha-blended onto the sprite.
    pub alpha: u8,

    /// Inclusive channel range each mark's colour
    /// is drawn from. `.0` must not exceed `.1`.
    pub color_range: (u8, u8),
    pub shape: NoisePattern,
    pub red: bool,
    pub green: bool,
    pub blue: bool,
}

impl Default for NoiseOptions {
    fn default() -> Self {
        NoiseOptions {
            count: 300 * 9,
            size: 2,
            alpha: 100,
            color_range: (0, 255),
            shape: NoisePattern::default(),
            red: true,
            green: true,
            blue: true,
            blur_sigma: 0.7,
        }
    }
}

impl NoiseOptions {
    /// # Errors
    ///
    /// Returns [`CaptchaError::InvalidInput`] if
    /// any field is outside its documented range.
    pub fn validate(&self) -> Result<()> {
        if self.color_range.0 > self.color_range.1 {
            return Err(CaptchaError::InvalidInput(format!(
                "noise color_range is inverted: {} > {}",
                self.color_range.0, self.color_range.1
            )));
        }

        if self.count > MAX_NOISE_COUNT {
            return Err(CaptchaError::InvalidInput(format!(
                "noise count {} exceeds maximum {MAX_NOISE_COUNT}",
                self.count
            )));
        }

        if self.size == 0 || self.size > MAX_NOISE_SIZE {
            return Err(CaptchaError::InvalidInput(format!(
                "noise size {} outside 1..={MAX_NOISE_SIZE}",
                self.size
            )));
        }

        if !self.blur_sigma.is_finite() || self.blur_sigma < 0.0 || self.blur_sigma > MAX_BLUR_SIGMA
        {
            return Err(CaptchaError::InvalidInput(format!(
                "noise blur_sigma {} outside 0.0..={MAX_BLUR_SIGMA}",
                self.blur_sigma
            )));
        }

        let work = u64::from(self.count) * self.pixels_per_mark();
        if work > MAX_NOISE_WORK {
            return Err(CaptchaError::InvalidInput(format!(
                "noise count {} at size {} blends {work} pixels, maximum is {MAX_NOISE_WORK}",
                self.count, self.size
            )));
        }

        Ok(())
    }

    fn pixels_per_mark(&self) -> u64 {
        match self.shape {
            NoisePattern::Dots => 1,
            NoisePattern::Lines => u64::from(self.size),
            NoisePattern::Grid => u64::from(self.size) * u64::from(self.size),
        }
    }
}

/// Caller must compose the result onto `bg`.
pub fn warp_tile(base: &RgbaImage, angle_deg: f32, flip: bool, bg: Rgba<u8>, out: &mut RgbaImage) {
    let (in_w, in_h) = base.dimensions();
    let (out_w, out_h) = out.dimensions();

    let scale = out_w as f32 / in_w as f32;
    let scale_x = match flip {
        true => -scale,
        false => scale,
    };

    let projection = Projection::translate(out_w as f32 / 2.0, out_h as f32 / 2.0)
        * Projection::scale(scale_x, scale)
        * Projection::rotate(angle_deg.to_radians())
        * Projection::translate(-(in_w as f32) / 2.0, -(in_h as f32) / 2.0);

    warp_into(
        base,
        projection,
        Interpolation::Bilinear,
        Border::Constant(bg),
        out,
    );

    mask_outside_circle(out, bg);
}

pub fn watermark_with_noise(img: &mut RgbaImage, opts: NoiseOptions) {
    let mut rng = rand::rng();
    let (width, height) = img.dimensions();

    for _ in 0..opts.count {
        let x = rng.random_range(0..width);
        let y = rng.random_range(0..height);

        let channel = |enabled: bool, rng: &mut _| match enabled {
            true => RngExt::random_range(rng, opts.color_range.0..=opts.color_range.1),
            false => 0,
        };

        let color = Rgba([
            channel(opts.red, &mut rng),
            channel(opts.green, &mut rng),
            channel(opts.blue, &mut rng),
            opts.alpha,
        ]);

        match opts.shape {
            NoisePattern::Dots => blend_pixel(img, x, y, color),
            NoisePattern::Lines => {
                for i in 0..opts.size.min(width - x) {
                    blend_pixel(img, x + i, y, color);
                }
            }
            NoisePattern::Grid => {
                for dx in 0..opts.size.min(width - x) {
                    for dy in 0..opts.size.min(height - y) {
                        blend_pixel(img, x + dx, y + dy, color);
                    }
                }
            }
        }
    }
}

pub fn blur_rgb(img: RgbImage, sigma: f32) -> RgbImage {
    match sigma > 0.0 {
        true => imageops::fast_blur(&img, sigma),
        false => img,
    }
}

pub fn encode_image(img: &RgbImage, fmt: &SpriteFormat) -> Result<(Vec<u8>, &'static str)> {
    match *fmt {
        SpriteFormat::Jpeg { quality } => {
            let mut buf = Vec::with_capacity(img.as_raw().len() >> JPEG_BYTES_PER_PIXEL_SHIFT);
            let mut enc = image::codecs::jpeg::JpegEncoder::new_with_quality(&mut buf, quality);

            enc.encode(
                img.as_raw(),
                img.width(),
                img.height(),
                ExtendedColorType::Rgb8,
            )
            .map_err(CaptchaError::Encode)?;

            Ok((buf, "image/jpeg"))
        }
        SpriteFormat::Webp { quality, lossless } => {
            let enc = WebPEncoder::from_rgb(img.as_raw(), img.width(), img.height());

            let webp = match lossless {
                true => enc.encode_lossless(),
                false => {
                    let mut config = WebPConfig::new()
                        .map_err(|()| CaptchaError::Internal("build webp config".into()))?;
                    config.quality = quality as f32;
                    config.method = WEBP_METHOD;

                    enc.encode_advanced(&config)
                        .map_err(|e| CaptchaError::Internal(format!("webp encode: {e:?}")))?
                }
            };

            Ok((webp.to_vec(), "image/webp"))
        }
    }
}

fn mask_outside_circle(img: &mut RgbaImage, fill: Rgba<u8>) {
    let (width, height) = img.dimensions();

    let radius = width.min(height) as f32 / 2.0 - 1.0;
    if radius <= 0.0 {
        return;
    }

    let (cx, cy) = (width as f32 / 2.0, height as f32 / 2.0);
    let r2 = radius * radius;

    for y in 0..height {
        let dy = y as f32 + 0.5 - cy;
        let span = r2 - dy * dy;

        let (lo, hi) = match span > 0.0 {
            true => {
                let dx = span.sqrt();
                (
                    (cx - dx).ceil().clamp(0.0, width as f32) as u32,
                    (cx + dx).floor().clamp(0.0, width as f32) as u32,
                )
            }
            false => (width, width),
        };

        for x in (0..lo).chain(hi..width) {
            img.put_pixel(x, y, fill);
        }
    }
}

fn blend_pixel(img: &mut RgbaImage, x: u32, y: u32, src: Rgba<u8>) {
    let dst = img.get_pixel_mut(x, y);
    let a = u32::from(src.0[3]);
    let inv = 255 - a;

    for c in 0..3 {
        dst.0[c] = ((u32::from(src.0[c]) * a + u32::from(dst.0[c]) * inv) / 255) as u8;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn noise_validation_rejects_out_of_range() {
        let cases = [
            (
                "inverted color_range",
                NoiseOptions {
                    color_range: (200, 100),
                    ..NoiseOptions::default()
                },
            ),
            (
                "count over cap",
                NoiseOptions {
                    count: MAX_NOISE_COUNT + 1,
                    shape: NoisePattern::Dots,
                    ..NoiseOptions::default()
                },
            ),
            (
                "size zero",
                NoiseOptions {
                    size: 0,
                    ..NoiseOptions::default()
                },
            ),
            (
                "size over cap",
                NoiseOptions {
                    size: MAX_NOISE_SIZE + 1,
                    ..NoiseOptions::default()
                },
            ),
            (
                "negative blur",
                NoiseOptions {
                    blur_sigma: -0.1,
                    ..NoiseOptions::default()
                },
            ),
            (
                "blur over cap",
                NoiseOptions {
                    blur_sigma: MAX_BLUR_SIGMA + 0.1,
                    ..NoiseOptions::default()
                },
            ),
            (
                "blur nan",
                NoiseOptions {
                    blur_sigma: f32::NAN,
                    ..NoiseOptions::default()
                },
            ),
            (
                "blur infinite",
                NoiseOptions {
                    blur_sigma: f32::INFINITY,
                    ..NoiseOptions::default()
                },
            ),
            (
                "grid work over cap",
                NoiseOptions {
                    count: MAX_NOISE_COUNT,
                    size: MAX_NOISE_SIZE,
                    shape: NoisePattern::Grid,
                    ..NoiseOptions::default()
                },
            ),
            (
                "lines work over cap",
                NoiseOptions {
                    count: MAX_NOISE_COUNT,
                    size: MAX_NOISE_SIZE,
                    shape: NoisePattern::Lines,
                    ..NoiseOptions::default()
                },
            ),
        ];

        for (name, noise) in cases {
            assert!(
                matches!(noise.validate(), Err(CaptchaError::InvalidInput(_))),
                "{name} should be rejected"
            );
        }
    }

    #[test]
    fn noise_validation_accepts_bounds() {
        let accepted = [
            NoiseOptions::default(),
            NoiseOptions {
                count: 0,
                ..NoiseOptions::default()
            },
            NoiseOptions {
                count: MAX_NOISE_COUNT,
                shape: NoisePattern::Dots,
                ..NoiseOptions::default()
            },
            NoiseOptions {
                size: MAX_NOISE_SIZE,
                count: 1,
                ..NoiseOptions::default()
            },
            NoiseOptions {
                blur_sigma: 0.0,
                ..NoiseOptions::default()
            },
            NoiseOptions {
                blur_sigma: MAX_BLUR_SIGMA,
                ..NoiseOptions::default()
            },
            NoiseOptions {
                color_range: (7, 7),
                ..NoiseOptions::default()
            },
        ];

        for noise in accepted {
            assert!(noise.validate().is_ok(), "{noise:?} should be accepted");
        }
    }
}