1

I am trying to integrate AWS WAF CAPTCHA inside a React Native WebView. The AWS WAF response provides an HTML document that includes scripts (CDNs) for challenge and CAPTCHA rendering. However, the CAPTCHA does not render properly inside WebView. Even though the html document is rendering in web, it is only showing white screen in WebView.

  const htmlContent = `
  <!DOCTYPE html>
  <html lang="en">
  <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Human Verification</title>
      <script>
          window.awsWafCookieDomainList = [];
          window.gokuProps = {
              "key": "AQIDAHjcYu/GjX+QlghicBgQ/...",
              "iv": "A6wedAGoHAAAPbE0",
              "context": "NFyHZVaB4qXxqYnXxoFtvbWK..."
          };
          
          window.addEventListener("load", function() {
              const container = document.querySelector("#captcha-container");
              if (window.CaptchaScript && window.ChallengeScript) {
                  CaptchaScript.renderCaptcha(container, async (voucher) => {
                      await ChallengeScript.submitCaptcha(voucher);
                      window.location.reload(true);
                  });
              } else {
                  console.error("CaptchaScript or ChallengeScript is not defined");
              }
          });
      </script>
  </head>
  <body>
      <div id="captcha-container"></div>
      <noscript>
          <h1>JavaScript is disabled</h1>
          <p>In order to continue, you need to verify that you're not a robot by solving a CAPTCHA puzzle.</p>
          <p>The CAPTCHA puzzle requires JavaScript. Enable JavaScript and then reload the page.</p>
      </noscript>
  </body>
  </html>
  `;

  return (
    <View style={styles.container}>
      <WebView
        originWhitelist={['*']}
        source={{ html: htmlContent }}
        javaScriptEnabled={true}
        domStorageEnabled={true}
      />
    </View>
  );
};```
1
  • As a note reCAPTCHA is a Google product and is different than AWS WAF CAPTCHA. Commented Apr 8 at 17:02

1 Answer 1

0

Try using this, it work

import React, { useEffect, useState } from 'react';
import { View } from 'react-native';
import { WebView } from 'react-native-webview';

export default function CaptchaScreen() {
  const [captchaHtml, setCaptchaHtml] = useState<string | null>(null);

  useEffect(() => {
    fetchCaptcha();
  }, []);

  const fetchCaptcha = async () => {
    try {
      const response = await fetch('https://your-protected-url.com');
      const text = await response.text();

      if (text.includes('captcha') || text.includes('challenge')) {
        setCaptchaHtml(text);
      } else {
        // Handle normal response
      }
    } catch (err) {
      console.error('Error fetching CAPTCHA:', err);
    }
  };

  if (!captchaHtml) return null;

  return (
    <View style={{ flex: 1 }}>
      <WebView
        originWhitelist={['*']}
        source={{ html: captchaHtml }}
        javaScriptEnabled
        domStorageEnabled
      />
    </View>
  );
}

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented 11 hours ago

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.