Skip to content

fix: hostname validation accepts flexible IntoIterator - #12

Merged
JuliDi merged 1 commit into
mainfrom
julian/fix-hostname-validation
Aug 7, 2026
Merged

fix: hostname validation accepts flexible IntoIterator#12
JuliDi merged 1 commit into
mainfrom
julian/fix-hostname-validation

Conversation

@JuliDi

@JuliDi JuliDi commented Aug 7, 2026

Copy link
Copy Markdown
Member

Right now when you only know the URL of your page but need to extract the hostname, usage becomes utterly terrible:

let host = Url::parse(&app_state.app_config.url)
    .ok()
    .and_then(|u| u.host_str().map(|h| h.to_string()));
let hostnames = host.as_deref().map(|h| [h])
let validation_response = app_state
    .turnstile_client
    .siteverify(
        SiteVerifyRequest {
            response: turnstile_response.clone(),
            ..Default::default()
        },
        hostnames.as_ref().map(|h| h.as_slice()),
    )
    .await;

This PR makes the valid_hostnames parameter more flexible (though at the cost of requiring None::<&str> to skip instead of just None, which might be good because it makes it more difficult to skip).

With this PR it becomes

let hostnames = Url::parse(&app_state.app_config.url)
    .ok()
    .and_then(|u| u.host_str().map(|h| h.to_string()))
let validation_response = app_state
    .turnstile_client
    .siteverify(
        SiteVerifyRequest {
            response: turnstile_response.clone(),
            ..Default::default()
        },
        hostnames,
    )
    .await;

@JuliDi JuliDi self-assigned this Aug 7, 2026
@JuliDi
JuliDi requested a review from joelsa August 7, 2026 16:14
Comment thread src/lib.rs
/// will error if the hostname returned by the Turnstile API does not match any of the
/// provided hostnames.
/// When it is None, the hostname is not verified.
/// To skip hostname verification, set it to `None::<&str>`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@JuliDi
JuliDi merged commit 06a520c into main Aug 7, 2026
3 checks passed
@JuliDi
JuliDi deleted the julian/fix-hostname-validation branch August 7, 2026 19:30
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