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.)
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,…
What is CWE-481?
Real-world CVEs caused by CWE-481
Ainda não há referências CVE públicas associadas a este CWE no catálogo da MITRE.
Trajeto do atacante passo a passo
- 1
The following C/C++ and C# examples attempt to validate an int input parameter against the integer value 100.
- 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
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
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
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.
Vulnerable C
The following C/C++ and C# examples attempt to validate an int input parameter against the integer value 100.
int isValid(int value) {
if (value=100) {
printf("Value is valid\n");
return(1);
}
printf("Value is not valid\n");
return(0);
} 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.
public void checkValid(boolean isValid) {
if (isValid) {
System.out.println("Performing processing");
doSomethingImportant();
}
else {
System.out.println("Not Valid, do not perform processing");
return;
}
} 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.
How to detect CWE-481
O Plexicus deteta automaticamente o CWE-481 e abre um PR de correção em menos de 60 segundos.
O Codex Remedium analisa cada commit, identifica esta fraqueza exata e entrega um pull request pronto para revisão com o patch. Sem tickets. Sem transferências.
Frequently asked questions
O que é o 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.
Qual a gravidade do CWE-481?
A MITRE classifica a probabilidade de exploração como Baixa — a exploração é pouco comum, mas a fraqueza deve mesmo assim ser corrigida quando descoberta.
Que linguagens ou plataformas são afetadas pelo CWE-481?
MITRE lists the following affected platforms: C, C++, Java, C#.
Como posso prevenir o 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.
Como é que o Plexicus deteta e corrige o CWE-481?
O motor SAST do Plexicus correlaciona a assinatura de fluxo de dados do CWE-481 em cada commit. Quando é encontrada uma correspondência, o nosso agente Codex Remedium abre um PR de correção com o código corrigido, testes e um resumo de uma linha para o revisor.
Onde posso saber mais sobre o CWE-481?
A MITRE publica a definição canónica em https://cwe.mitre.org/data/definitions/481.html. Pode também consultar a documentação da OWASP e do NIST para orientações adjacentes.
Weaknesses related to CWE-481
Use of Incorrect Operator
This vulnerability occurs when a developer mistakenly uses the wrong operator in their code, leading to unintended and potentially…
Comparing instead of Assigning
This vulnerability occurs when a developer accidentally uses a comparison operator (like '==') where an assignment operator (like '=') was…
Use of Wrong Operator in String Comparison
This vulnerability occurs when a developer incorrectly compares string values, typically by using reference equality operators (like == or…
Incorrect Comparison
This weakness occurs when a security-critical decision relies on a flawed comparison between two pieces of data. The incorrect logic can…
Pare de pagar por desenvolvedor.
Comece a fechar o ciclo.
O Plexicus é o ASPM nativo de IA que verifica, filtra, corrige, pentesta e explica — de forma autónoma. Programadores ilimitados, repos ilimitados, ações de IA de utilização justa. Nível gratuito real, €269/mo anual quando estiver pronto.