diff --git a/ethers-etherscan/src/errors.rs b/ethers-etherscan/src/errors.rs index f840cc57..f10920ba 100644 --- a/ethers-etherscan/src/errors.rs +++ b/ethers-etherscan/src/errors.rs @@ -39,6 +39,8 @@ pub enum EtherscanError { InvalidApiKey, #[error("Sorry, you have been blocked by Cloudflare, See also https://community.cloudflare.com/t/sorry-you-have-been-blocked/110790")] BlockedByCloudflare, + #[error("The Requested prompted a cloudflare captcha security challenge to review the security of your connection before proceeding.")] + CloudFlareSecurityChallenge, } /// etherscan/polyscan is protected by cloudflare, which can lead to html responses like `Sorry, you have been blocked` See also @@ -48,10 +50,23 @@ pub(crate) fn is_blocked_by_cloudflare_response(txt: &str) -> bool { txt.to_lowercase().contains("sorry, you have been blocked") } +/// etherscan/polyscan is protected by cloudflare, which can require captchas to "review the +/// security of your connection before proceeding" +pub(crate) fn is_cloudflare_security_challenge(txt: &str) -> bool { + txt.contains("https://www.cloudflare.com?utm_source=challenge") || + txt.to_lowercase().contains("checking if the site connection is secure") +} + #[cfg(test)] mod tests { use super::*; + #[test] + fn test_is_cloudflare_security_challenge() { + let res = " Just a moment...

\"Icon api-goerli.etherscan.io

Checking if the site connection is secure

api-goerli.etherscan.io needs to review the security of your connection before proceeding.
Ray ID: 794294b0ff122cc8
Performance & security by Cloudflare
"; + assert!(is_cloudflare_security_challenge(res)); + } + #[test] fn test_cloudflare_response() { let resp = "\n\n\n\n \n\nAttention Required! | Cloudflare\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\n
Please enable cookies.
\n
\n
\n

Sorry, you have been blocked

\n

You are unable to access polygonscan.com

\n
\n\n
\n
\n
\n \n \n \n
\n
\n
\n\n
\n
\n
\n

Why have I been blocked?

\n\n

This website is using a security service to protect itself from online attacks. The action you just performed triggered the security solution. There are several actions that could trigger this block including submitting a certain word or phrase, a SQL command or malformed data.

\n
\n\n
\n

What can I do to resolve this?

\n\n

You can email the site owner to let them know you were blocked. Please include what you were doing when this page came up and the Cloudflare Ray ID found at the bottom of this page.

\n
\n
\n
\n\n \n\n\n
\n
\n\n \n\n\n\n"; diff --git a/ethers-etherscan/src/lib.rs b/ethers-etherscan/src/lib.rs index a77128f3..b3fa1c6e 100644 --- a/ethers-etherscan/src/lib.rs +++ b/ethers-etherscan/src/lib.rs @@ -1,6 +1,6 @@ //! Bindings for [etherscan.io web api](https://docs.etherscan.io/) -use crate::errors::is_blocked_by_cloudflare_response; +use crate::errors::{is_blocked_by_cloudflare_response, is_cloudflare_security_challenge}; use contract::ContractMetadata; use errors::EtherscanError; use ethers_core::{ @@ -209,6 +209,8 @@ impl Client { error!(target: "etherscan", ?res, "Failed to deserialize response: {}", err); if is_blocked_by_cloudflare_response(res) { EtherscanError::BlockedByCloudflare + } else if is_cloudflare_security_challenge(res) { + EtherscanError::CloudFlareSecurityChallenge } else { EtherscanError::Serde(err) }