-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update IPAddressExample.cs #25
base: main
Are you sure you want to change the base?
Conversation
@@ -4,7 +4,7 @@ | |||
public class IPAddressExample | |||
{ | |||
private IPAddress hardcodedIpAddress; | |||
public const string MyIPAddress = "123.168.96.59"; | |||
public const string MyIPAddress = "123.168.96.58"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Semgrep Assistant suggests the following fix: Use configuration files or environment variables to store IP addresses instead of hardcoding them in the code.
View step-by-step instructions
- Remove the hardcoded IP address from the code by replacing the line
public const string MyIPAddress = "123.168.96.58";
with a configuration-based approach. - Use a configuration file or environment variable to store the IP address. For example, you can use an appsettings.json file or environment variables.
- If using appsettings.json, add an entry like
"MyIPAddress": "123.168.96.58"
under a suitable section. - In your code, read the IP address from the configuration source. If using appsettings.json, you can use the
ConfigurationManager
class to retrieve the value:string myIPAddress = ConfigurationManager.AppSettings["MyIPAddress"];
. - Ensure that your application is configured to load the configuration file or environment variables at runtime.
This code change should be a good starting point:
public const string MyIPAddress = "123.168.96.58"; | |
using System; | |
using System.Net; | |
using System.Configuration; // Add this for ConfigurationManager | |
public class IPAddressExample | |
{ | |
private IPAddress hardcodedIpAddress; | |
public string MyIPAddress; | |
public IPAddressExample() | |
{ | |
// Retrieve the IP address from configuration | |
MyIPAddress = ConfigurationManager.AppSettings["MyIPAddress"]; | |
string myIP = "123.168.96.54"; | |
//ok: avoid_ip_address_in_the_code | |
hardcodedIpAddress = IPAddress.Parse("192.168.0.1"); | |
print("Sebas"); | |
print("---"); | |
//ruleid: avoid_ip_address_in_the_code | |
hardcodedIpAddress = IPAddress.Parse(X, "123.168.96.54"); | |
//ruleid: avoid_ip_address_in_the_code | |
IP_ADDRESS = "123.168.96.54"; | |
//ruleid: avoid_ip_address_in_the_code | |
IP_ADDRESS = MY_IP = "123.168.96.54"; | |
//ok: avoid_ip_address_in_the_code | |
hardcodedIpAddress = IPAddress.Parse("a"); | |
//ok: avoid_ip_address_in_the_code | |
hardcodedIpAddress = IPAddress.Parse("ab"); | |
//ok: avoid_ip_address_in_the_code | |
hardcodedIpAddress = IPAddress.Parse("192"); | |
//ok: avoid_ip_address_in_the_code | |
} | |
} |
Leave feedback with a 👍 / 👎. Save a memory with /semgrep remember <your custom instructions>
.
No description provided.