Problem/Motivation

altcha_captcha_validation() and SelfHostedSolutionVerification::verify() don't check the type of the decoded payload before using it. A request with an invalid altcha value crashes form validation with an uncaught TypeError. The visitor gets an HTTP 500 instead of a normal "CAPTCHA failed" message, and every such request writes an error with a stack trace to the log.

  • altcha.module: Json::decode() returns NULL for invalid JSON and a scalar for e.g. "1", and the result is passed to verify(array $data):
    $payload = Json::decode($json);
    ...
    return \Drupal::service('altcha.self_hosted_solution_verification')->verify($payload);
  • src/SelfHostedSolutionVerification.php: nested values are passed to typed library constructors unchecked:
    parameters: ChallengeParameters::fromArray($data['challenge']['parameters'] ?? []),  // array required
    signature: $data['challenge']['signature'] ?? NULL,                                  // ?string required
  • src/SentinelSolutionVerification.php: verifyServerSignature() requires a string secret; a NULL config value throws (covered by the separate empty-secret issue).

All of these fail closed, so this does not bypass the CAPTCHA. But anyone can trigger error responses and fill the log with errors at will.

Errors observed with altcha-org/altcha 2.3.0 on PHP 8.3:

Decoded altcha value Result
not-json TypeError: ...::verify(): Argument #1 ($data) must be of type array, null given
1 TypeError: ...::verify(): Argument #1 ($data) must be of type array, int given
{"challenge":{"parameters":"x"}} TypeError: ChallengeParameters::fromArray(): Argument #1 ($arr) must be of type array, string given
{"challenge":{"parameters":{},"signature":[1]}} TypeError: Challenge::__construct(): Argument #2 ($signature) must be of type ?string, array given

Steps to reproduce

  1. Install ALTCHA 2.0.x (self-hosted) and enable the ALTCHA CAPTCHA on the user login form.
  2. Open /user/login as an anonymous user, fill in the form, and in the browser dev tools set the value of the hidden altcha input to bm90LWpzb24= (base64 of not-json).
  3. Submit the form.
  4. Expected: the form shows a CAPTCHA validation error. Actual: "The website encountered an unexpected error." (HTTP 500), and a TypeError is logged.

Proposed resolution

Check the types at the input boundary and return FALSE for anything that isn't a well-formed payload.

altcha.module:

@@ function altcha_captcha_validation($solution, $response, $element, $form_state): bool {
-  $hash = \Drupal::request()->request->get('altcha');
-  if (!is_string($hash)) {
+  // Read via all() so an array value (altcha[]=x) cannot throw.
+  $hash = \Drupal::request()->request->all()['altcha'] ?? NULL;
+  if (!is_string($hash) || $hash === '') {
     return FALSE;
   }

-  $json = base64_decode($hash);
-  if (!$json) {
+  $json = base64_decode($hash, TRUE);
+  if ($json === FALSE || $json === '') {
     return FALSE;
   }

   $payload = Json::decode($json);
+  if (!is_array($payload)) {
+    return FALSE;
+  }
+
   $config = \Drupal::configFactory()->get('altcha.settings');

src/SelfHostedSolutionVerification.php:

@@ public function verify(array $data): bool {
-    $altcha = new Altcha($this->secretManager->getSecretKey());
-
-    $challenge = new Challenge(
-      parameters: ChallengeParameters::fromArray($data['challenge']['parameters'] ?? []),
-      signature: $data['challenge']['signature'] ?? NULL,
-    );
-
-    $solution = new Solution(
-      counter: (int) ($data['solution']['counter'] ?? 0),
-      derivedKey: (string) ($data['solution']['derivedKey'] ?? ''),
-    );
+    $parameters = $data['challenge']['parameters'] ?? NULL;
+    $signature = $data['challenge']['signature'] ?? NULL;
+    $counter = $data['solution']['counter'] ?? NULL;
+    $derived_key = $data['solution']['derivedKey'] ?? NULL;
+    if (!is_array($parameters)
+      || !is_string($signature) || $signature === ''
+      || !is_int($counter) || $counter < 0
+      || !is_string($derived_key)) {
+      return FALSE;
+    }
+
+    $altcha = new Altcha($this->secretManager->getSecretKey());
+
+    $challenge = new Challenge(
+      parameters: ChallengeParameters::fromArray($parameters),
+      signature: $signature,
+    );
+
+    $solution = new Solution(
+      counter: $counter,
+      derivedKey: $derived_key,
+    );

     $payload = new Payload($challenge, $solution);
@@
-    // The challenge signature identifies this verified solution.
-    $solution_id = $data['challenge']['signature'] ?? NULL;
-    if (!is_string($solution_id) || $solution_id === '') {
-      return FALSE;
-    }
-
-    return $this->consumeSolution($solution_id, $data['challenge']['parameters']['expiresAt'] ?? NULL);
+    // The challenge signature identifies this verified solution.
+    return $this->consumeSolution($signature, $parameters['expiresAt'] ?? NULL);

Add unit test cases to tests/src/Unit/SelfHostedSolutionVerificationTest.php (data provider) asserting that verify() returns FALSE without throwing for:

  • []
  • ['challenge' =&gt; 'x']
  • ['challenge' =&gt; ['parameters' =&gt; 'x']]
  • ['challenge' =&gt; ['parameters' =&gt; [], 'signature' =&gt; [1]]]
  • ['challenge' =&gt; ['parameters' =&gt; [], 'signature' =&gt; 's'], 'solution' =&gt; ['counter' =&gt; '1', 'derivedKey' =&gt; []]]

Add a functional test that submits altcha=bm90LWpzb24= and expects a validation error rather than a 500.

Remaining tasks

  • Review the patch.
  • Add the tests.
  • Check overlap with the existing "missing null guard" issue (#3631713), if it covers the same code.

User interface changes

None. Invalid submissions show the normal CAPTCHA validation error instead of an error page.

API changes

None.

Data model changes

None.


Found and created with the help of AI.

Issue fork altcha-3636501

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

anybody created an issue. See original summary.

anybody’s picture

Status: Active » Needs review

anybody’s picture

Test-only fails as expected!