CWE Rule 259
R2026bDescription
Use of Hard-coded Password
Polyspace Implementation
Polyspace checks for these issues:
Password for Service Hardcoded
Authentication Using Comparison with Hardcoded Password
Examples
The issue occurs when your code uses a hardcoded password for a service, for example
passwd = "guest";.
Hardcoded credentials can be discovered in source code, binaries, or version control and lead to unauthorized access. Authentication failures where the password is hardcoded is difficult to diagnose and mitigate
Do not embed passwords in code. Retrieve secrets at runtime from a secure source, or a configuration file with restricted permissions, or a dedicated secrets manager.
In this example, a hard-coded password is used for a service. Polyspace® reports a violation.
#include <stddef.h>
typedef struct _MYSQL MYSQL;
MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char *user,
const char *passwd, const char *db, unsigned int port,
const char *unix_socket, unsigned long client_flag);
extern MYSQL *sql;
void *connect_to_database(const char *db) {
return mysql_real_connect(sql, "localhost", "guest", "guest", db, 0, 0, 0); // Noncompliant
}
Avoid hard coded password. In this code, the password is retrieved by calling a function. Polyspace does not report a violation.
#include <stdlib.h>
extern const char* getPass();
typedef struct _MYSQL MYSQL;
MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char *user,
const char *passwd, const char *db, unsigned int port,
const char *unix_socket, unsigned long client_flag);
extern MYSQL *sql;
void *connect_to_database(const char *db) {
return mysql_real_connect(sql, "localhost", "guest", getPass(), db, 0, 0, 0);
}
The issue occurs when an authentication check compares input directly to a hard-coded literal password.
Using a literal in authentication allows anyone who discovers the string to bypass or perform the authentication. Hard-coded comparison logic makes changing or rotating the credential difficult and error-prone.
Do not compare credentials to literals in code. Verify credentials against a secure, external source. For example, use a runtime-provided secret and compare hashed values. Prefer authentication services or libraries that handle secure hashing and salting rather than raw string comparisons against literals.
In this example, the function compares the supplied password directly to a hard-coded literal.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int VerifyAdmin(char *password) {
if (strcmp(password, "Mew!")) { //Noncompliant
printf("Incorrect Password!\n");
return(0);
}
printf("Entering Diagnostic Mode...\n");
return(1);
}Compare the supplied credential against a value obtained at runtime. This code retrieves
the expected password from the environment via getenv and compares it,
avoiding a hard-coded literal.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int VerifyAdmin(const char *password) {
const char *expected = getenv("ADMIN_PASS");
if (!expected) {
fprintf(stderr, "No admin password configured in ADMIN_PASS\n");
return 0;
}
if (strcmp(password, expected) != 0) {
printf("Incorrect Password!\n");
return 0;
}
printf("Entering Diagnostic Mode...\n");
return 1;
}
int main(void) {
VerifyAdmin("attempt");
return 0;
}
Check Information
| Category: Others |
PQL Name: std.cwe_native.R259 |
Version History
Introduced in R2026b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)