3333"""
3434)
3535
36+ HEADER_DONT_CACHE = {"Cache-Control" : "no-store" }
37+
3638
3739def make_signature (value : str ) -> str :
3840 if CLOUDFLARE_SECRET_KEY is None :
@@ -82,8 +84,10 @@ async def verify_captcha_middleware(request: Request, call_next):
8284
8385 host = request .headers .get ("referer" )
8486 if host and host .startswith ("http:" ):
85- url = request .url .replace (scheme = "https" )
86- return RedirectResponse (url = str (url ))
87+ error_html = ERROR_PAGE_TEMPLATE .substitute (
88+ error_title = "HTTPS is required for accessing this site" ,
89+ )
90+ return Response (content = error_html , status_code = 400 , media_type = "text/html" )
8791
8892 # Check if the user has completed the CAPTCHA verification
8993 captcha_verified = request .cookies .get ("captcha_verified" )
@@ -109,17 +113,13 @@ async def captcha_page():
109113 <div class="cf-turnstile" data-sitekey="{ os .getenv ('CLOUDFLARE_SITE_KEY' )} " data-callback="onSubmit"></div>
110114 </form>
111115 <script>
112- // Function called when CAPTCHA is completed
116+ let formSubmitted = false;
113117 function onSubmit(token) {{
114- document.getElementById('captcha-form').submit(); // Auto-submit form once CAPTCHA is validated
118+ if (!formSubmitted) {{
119+ formSubmitted = true;
120+ document.getElementById('captcha-form').submit();
121+ }}
115122 }}
116-
117- // Optional: Automatically trigger Turnstile verification when the page loads
118- window.onload = function() {{
119- setTimeout(function() {{
120- turnstile.execute();
121- }}, 1000); // Trigger after 1 second (adjust as needed)
122- }};
123123 </script>
124124 </body>
125125 </html>
@@ -135,7 +135,12 @@ async def verify_captcha(request: Request):
135135 error_html = ERROR_PAGE_TEMPLATE .substitute (
136136 error_title = "CAPTCHA response is invalid" ,
137137 )
138- return Response (content = error_html , media_type = "text/html" , status_code = 400 )
138+ return Response (
139+ content = error_html ,
140+ status_code = 400 ,
141+ headers = HEADER_DONT_CACHE ,
142+ media_type = "text/html" ,
143+ )
139144
140145 client_ip : str
141146 if request .client :
@@ -146,7 +151,12 @@ async def verify_captcha(request: Request):
146151 error_html = ERROR_PAGE_TEMPLATE .substitute (
147152 error_title = "Could not determine client host" ,
148153 )
149- return Response (content = error_html , media_type = "text/html" , status_code = 400 )
154+ return Response (
155+ content = error_html ,
156+ status_code = 400 ,
157+ headers = HEADER_DONT_CACHE ,
158+ media_type = "text/html" ,
159+ )
150160
151161 # Verify the CAPTCHA with Cloudflare
152162 url = "https://challenges.cloudflare.com/turnstile/v0/siteverify"
@@ -165,11 +175,18 @@ async def verify_captcha(request: Request):
165175 error_html = ERROR_PAGE_TEMPLATE .substitute (
166176 error_title = "CAPTCHA verification failed" ,
167177 )
168- return Response (content = error_html , media_type = "text/html" , status_code = 400 )
178+ return Response (
179+ content = error_html ,
180+ status_code = 400 ,
181+ headers = HEADER_DONT_CACHE ,
182+ media_type = "text/html" ,
183+ )
169184
170185 # Set a signed cookie to mark CAPTCHA as verified
171186 cookie_value = create_secure_cookie (cf_turnstile_response )
172- redirect_response = RedirectResponse (url = f"{ CHAINLIT_URI } /" , status_code = 302 )
187+ redirect_response = RedirectResponse (
188+ url = f"{ CHAINLIT_URI } /" , status_code = 302 , headers = HEADER_DONT_CACHE
189+ )
173190 redirect_response .set_cookie (
174191 key = "captcha_verified" ,
175192 value = cookie_value ,
0 commit comments