Package: cloudflare-challenge
CloudFlare-ChallengeEnsure that we can do a cloudflare challenge in flaskRationaleIf you Flask server is behind a CloudFlare wall then any upload of data may provoke
a "challenge" of the "I'm not a robot" kind.Instead of returning the response to your browser query, CloudFlare sends
back an html page with a 403 HTTP status which will interogate your browser internals and leave a cookiecf_clearance--ifyou "pass" the challenge!.This is of course adisasterif you have used Ajax to send the request.The idea here is to get that sweet, sweet CloudFlare cookiecf_clearanceas soon as possible or at least before
you do any ajax requests.Basically if there is nocf_clearancecookie for a request this Blueprint will redirect to
a "managed" page where it will automatically upload an image to provoke the CloudFlare challenge --
then check for success.Once your browser has thecf_clearancecookie thenCloudFlare-Challengewill leave your app alone.This "solution" is not ideal but it maybe better than weird failures of your ajax requests that will
ultimately confuse/anger your users.The big assumption here is that an upload of an image will provoke the CloudFlare challenge. If
it doesn't then don't use this package!ConfigurationYou will need to set 1-5 configuration variables# path to a static image (required) e.g:CF_IMAGE_FILENAME="img/Three-pink-daisies.jpeg"# endpoint to redirect to after challengeCF_REDIRECT_TO=None# template to inherit from. Defaults to one provided by cloudflare_challenge.CF_MAIN_TEMPLATE=None# list of endpoint prefixes that will be white/black listed# can be just a stringCF_WHITE_LIST=()CF_BLACK_LIST=()IfCF_IMAGE_FILENAMEis missing or None then the blueprint willsilentlynot be registered even
ifinit_appis called.init_appis indempotent.The image filename will be used byurl_for('static', filename=CF_IMAGE_FILENAME)to
generate a url. The image should be large enough to provoke a challenge. Choose an image
that will already be cached in your brower such as a banner image in your flask landing page.If you specify a template (CF_MAIN_TEMPLATE) it should have acontentblock
(for html, this is where the iframe is blatted) and ajsblock (for javascript).IfCF_REDIRECT_TOis missing or None then steps will be taken to redirect back to
the original page that prompted the redirection to the challenge page otherwise it will
redirect back to/.Remember:CF_REDIRECT_TOexpects a flaskendpointnot a URL.White listed endpoints won't trigger a check for CloudFlare cookies, headers etc.
Use this for "static" images, css etc (thestaticendpoint is already white listed).You can blacklist flask endpoints -- possibly endpoints that generate html with forms in them
and thus might trigger the challenge.The black list is checked first then the white list.Either way, Ajax requests (with aX-Requested-Withheader) will not trigger the challenge page (no point really since
this doesn't help -- too late!).It is maybe the best to black list endpoints that generate html forms for the user to fill out, or
any page that might send an ajax request due to user interaction. You will want to trigger the
challengebeforeany Ajax/form upload is undertaken.UsageBasic usagefromflaskimportFlaskfromcloudflare_challengeimportinit_appapp=Flask(__name__)app.config.from_pyfile("config.py")# sayinit_app(app,url_prefix='/someprefix')Client SideIf you are using jQuery on a page to enable Ajax then you can ensure Challenges
are detected by adding to your page:{%from"cloudflare-macros.html"importcf_challenge%}{{cf_challenge()}}Then Ajax challenges will be detected and logged.If, in addition you setMAIL_SERVERandCF_MAIL_RECIPIENT, then cloudflare-challenge will attempt to
send an email too.If you only want this part then setCF_WHITE_LIST = '*'