CWE-481 Variante Brouillon Low likelihood

Assigning instead of Comparing

This flaw occurs when a developer accidentally uses the assignment operator (=) instead of the comparison operator (== or ===). The code assigns a value when it was meant to check for equality,…

Définition

What is CWE-481?

This flaw occurs when a developer accidentally uses the assignment operator (=) instead of the comparison operator (== or ===). The code assigns a value when it was meant to check for equality, which fundamentally changes the program's logic.
This error is a common typo because assignment and comparison operators look nearly identical in many programming languages (like using '=' instead of '=='). In a conditional statement like an 'if' check, the code will evaluate the assigned value itself, often leading to unexpected behavior where the condition always passes as 'true' or always fails. The result is typically a clear bug in program execution flow, such as skipping security checks or executing incorrect branches of code. While modern linters and compilers often warn about this pattern, it remains a frequent mistake during rapid development or code reviews, emphasizing the need for defensive coding practices and proper tooling.
Impact réel

Real-world CVEs caused by CWE-481

Aucune référence CVE publique n'est liée à ce CWE dans le catalogue MITRE pour le moment.

Comment les attaquants l'exploitent

Parcours de l'attaquant étape par étape

  1. 1

    The following C/C++ and C# examples attempt to validate an int input parameter against the integer value 100.

  2. 2

    However, the expression to be evaluated in the if statement uses the assignment operator "=" rather than the comparison operator "==". The result of using the assignment operator instead of the comparison operator causes the int variable to be reassigned locally and the expression in the if statement will always evaluate to the value on the right hand side of the expression. This will result in the input value not being properly validated, which can cause unexpected results.

  3. 3

    In this example, we show how assigning instead of comparing can impact code when values are being passed by reference instead of by value. Consider a scenario in which a string is being processed from user input. Assume the string has already been formatted such that different user inputs are concatenated with the colon character. When the processString function is called, the test for the colon character will result in an insertion of the colon character instead, adding new input separators. Since the string was passed by reference, the data sentinels will be inserted in the original string (CWE-464), and further processing of the inputs will be altered, possibly malformed..

  4. 4

    The following Java example attempts to perform some processing based on the boolean value of the input parameter. However, the expression to be evaluated in the if statement uses the assignment operator "=" rather than the comparison operator "==". As with the previous examples, the variable will be reassigned locally and the expression in the if statement will evaluate to true and unintended processing may occur.

  5. 5

    While most Java compilers will catch the use of an assignment operator when a comparison operator is required, for boolean variables in Java the use of the assignment operator within an expression is allowed. If possible, try to avoid using comparison operators on boolean variables in java. Instead, let the values of the variables stand for themselves, as in the following code.

Exemple de code vulnérable

Vulnerable C

The following C/C++ and C# examples attempt to validate an int input parameter against the integer value 100.

Vulnérable C
int isValid(int value) {
  	if (value=100) {
  		printf("Value is valid\n");
  		return(1);
  	}
  	printf("Value is not valid\n");
  	return(0);
  }
Exemple de code sécurisé

Secure Java

While most Java compilers will catch the use of an assignment operator when a comparison operator is required, for boolean variables in Java the use of the assignment operator within an expression is allowed. If possible, try to avoid using comparison operators on boolean variables in java. Instead, let the values of the variables stand for themselves, as in the following code.

Sécurisé Java
public void checkValid(boolean isValid) {
  	if (isValid) {
  		System.out.println("Performing processing");
  		doSomethingImportant();
  	}
  	else {
  		System.out.println("Not Valid, do not perform processing");
  		return;
  	}
  }
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-481

  • Testing Many IDEs and static analysis products will detect this problem.
  • Implementation Place constants on the left. If one attempts to assign a constant with a variable, the compiler will produce an error.
Signaux de détection

How to detect CWE-481

Automated Static Analysis High

Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)

Correction automatique Plexicus

Plexicus détecte automatiquement CWE-481 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-481 ?

This flaw occurs when a developer accidentally uses the assignment operator (=) instead of the comparison operator (== or ===). The code assigns a value when it was meant to check for equality, which fundamentally changes the program's logic.

Quelle est la gravité de CWE-481 ?

MITRE évalue la probabilité d'exploitation comme Faible — l'exploitation est rare, mais la faiblesse doit tout de même être corrigée lorsqu'elle est découverte.

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

MITRE lists the following affected platforms: C, C++, Java, C#.

Comment puis-je prévenir CWE-481 ?

Many IDEs and static analysis products will detect this problem. Place constants on the left. If one attempts to assign a constant with a variable, the compiler will produce an error.

Comment Plexicus détecte et corrige CWE-481 ?

Le moteur SAST de Plexicus reconnaît la signature de flux de données de CWE-481 à 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-481 ?

MITRE publie la définition canonique à https://cwe.mitre.org/data/definitions/481.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.