CWE-187 Variante Incomplet

Partial String Comparison

This weakness occurs when software checks only part of a string or token to determine a match, instead of comparing the entire value. This incomplete validation can lead to incorrect security…

Définition

What is CWE-187?

This weakness occurs when software checks only part of a string or token to determine a match, instead of comparing the entire value. This incomplete validation can lead to incorrect security decisions.
Partial string comparison happens when a system, such as an authentication module, validates only the first few characters of an input against a stored value. For example, if a password check only verifies the initial 5 characters, an attacker could gain access by providing any password that starts with those same 5 characters, regardless of the full correct password. This flaw fundamentally undermines security logic by treating partially matching inputs as fully authorized. Developers should always enforce complete, exact-string comparisons for security-critical operations like authentication, authorization tokens, or integrity checks to prevent attackers from exploiting these predictable shortcuts.
Impact réel

Real-world CVEs caused by CWE-187

  • Product does not prevent access to restricted directories due to partial string comparison with a public directory

  • Argument parser of an IMAP server treats a partial command "body[p" as if it is "body.peek", leading to index error and out-of-bounds corruption.

  • Web browser only checks the hostname portion of a certificate when the hostname portion of the URI is not a fully qualified domain name (FQDN), which allows remote attackers to spoof trusted certificates.

  • One-character password by attacker checks only against first character of real password.

  • One-character password by attacker checks only against first character of real password.

Comment les attaquants l'exploitent

Parcours de l'attaquant étape par étape

  1. 1

    This example defines a fixed username and password. The AuthenticateUser() function is intended to accept a username and a password from an untrusted user, and check to ensure that it matches the username and password. If the username and password match, AuthenticateUser() is intended to indicate that authentication succeeded.

  2. 2

    In AuthenticateUser(), the strncmp() call uses the string length of an attacker-provided inPass parameter in order to determine how many characters to check in the password. So, if the attacker only provides a password of length 1, the check will only examine the first byte of the application's password before determining success.

  3. 3

    As a result, this partial comparison leads to improper authentication (CWE-287).

  4. 4

    Any of these passwords would still cause authentication to succeed for the "admin" user:

  5. 5

    This significantly reduces the search space for an attacker, making brute force attacks more feasible.

Exemple de code vulnérable

Vulnerable C

This example defines a fixed username and password. The AuthenticateUser() function is intended to accept a username and a password from an untrusted user, and check to ensure that it matches the username and password. If the username and password match, AuthenticateUser() is intended to indicate that authentication succeeded.

Vulnérable C
```
/* Ignore CWE-259 (hard-coded password) and CWE-309 (use of password system for authentication) for this example. */* 
  
  char *username = "admin";
  char *pass = "password";
  
  int AuthenticateUser(char *inUser, char *inPass) {
  ```
  	if (strncmp(username, inUser, strlen(inUser))) {
  		logEvent("Auth failure of username using strlen of inUser");
  		return(AUTH_FAIL);
  	}
  	if (! strncmp(pass, inPass, strlen(inPass))) {
  		logEvent("Auth success of password using strlen of inUser");
  		return(AUTH_SUCCESS);
  	}
  	else {
  		logEvent("Auth fail of password using sizeof");
  		return(AUTH_FAIL);
  	}
  }
  int main (int argc, char **argv) {
  		int authResult;
  		if (argc < 3) {
  			ExitError("Usage: Provide a username and password");
  		}
  		authResult = AuthenticateUser(argv[1], argv[2]);
  		if (authResult == AUTH_SUCCESS) {
  			DoAuthenticatedTask(argv[1]);
  		}
  		else {
  			ExitError("Authentication failed");
  		}
  }
Charge utile de l'attaquant

Any of these passwords would still cause authentication to succeed for the "admin" user:

Charge utile de l'attaquant
p
  pa
  pas
  pass
Exemple de code sécurisé

Secure pseudo

Sécurisé pseudo
// Validate, sanitize, or use a safe API before reaching the sink.
function handleRequest(input) {
  const safe = validateAndEscape(input);
  return executeWithGuards(safe);
}
What changed: the unsafe sink is replaced (or the input is validated/escaped) so the same payload no longer triggers the weakness.
Liste de contrôle de prévention

How to prevent CWE-187

  • Testing Thoroughly test the comparison scheme before deploying code into production. Perform positive testing as well as negative testing.
Signaux de détection

How to detect CWE-187

SAST High

Exécuter une analyse statique (SAST) sur le code source à la recherche du motif non sécurisé dans le flux de données.

DAST Moderate

Exécuter des tests de sécurité applicative dynamique (DAST) contre le point de terminaison en ligne.

Runtime Moderate

Surveiller les journaux runtime pour détecter des traces d'exception inhabituelles, des entrées malformées ou des tentatives de contournement d'autorisation.

Code review Moderate

Revue de code : signaler tout nouveau code qui traite les entrées de cette surface sans utiliser les helpers du framework validés.

Correction automatique Plexicus

Plexicus détecte automatiquement CWE-187 et ouvre une PR de correction en moins de 60 secondes.

Codex Remedium analyse chaque commit, identifie cette faiblesse précise et livre une pull request prête à être relue avec le correctif. Pas de tickets. Pas de transferts.

Questions fréquentes

Frequently asked questions

Qu'est-ce que CWE-187 ?

This weakness occurs when software checks only part of a string or token to determine a match, instead of comparing the entire value. This incomplete validation can lead to incorrect security decisions.

Quelle est la gravité de CWE-187 ?

MITRE n'a pas publié de note de probabilité d'exploitation pour cette faiblesse. Traitez-la comme un impact moyen jusqu'à ce que votre modèle de menace prouve le contraire.

Quels langages ou plateformes sont affectés par CWE-187 ?

MITRE n'a pas spécifié les plateformes affectées pour ce CWE — il peut s'appliquer à la plupart des stacks applicatives.

Comment puis-je prévenir CWE-187 ?

Thoroughly test the comparison scheme before deploying code into production. Perform positive testing as well as negative testing.

Comment Plexicus détecte et corrige CWE-187 ?

Le moteur SAST de Plexicus reconnaît la signature de flux de données de CWE-187 à chaque commit. Lorsqu'une correspondance est trouvée, notre agent Codex Remedium ouvre une PR de correction avec le code corrigé, les tests et un résumé d'une ligne pour le relecteur.

Où puis-je en savoir plus sur CWE-187 ?

MITRE publie la définition canonique à https://cwe.mitre.org/data/definitions/187.html. Vous pouvez également consulter la documentation OWASP et NIST pour des conseils adjacents.

Prêt quand vous l'êtes

Arrêtez de payer par développeur.
Commencez à fermer la boucle.

Plexicus est l'ASPM natif IA qui scanne, filtre, corrige, penteste et explique — de façon autonome. Développeurs illimités, dépôts illimités, actions IA à usage équitable. Vrai niveau gratuit, €269/mo annuel quand vous êtes prêt.