Hardcoded Credentials Risk
Which of the following practices is the best way to resolve hardcoded database credentials found by a static code analysis tool?
- Store credentials in environment variables
- Comment out the credentials in code
- Change the credentials to 'admin:admin'
- Store credentials in a public GitHub repo
- Hardcode the credentials with a warning comment
SQL Injection Detection
Static analysis found this code vulnerable to SQL injection: String query = 'SELECT * FROM users WHERE username = ' + userInput; What is the most secure way to fix it?
- Use parameterized queries
- Escape all single quotes in userInput manually
- Log the query before running it
- Use userInput.toUpperCase()
- Remove the input check entirely
Cross-Site Scripting (XSS)
Given the code snippet: output.write(userComment);, which remediation prevents cross-site scripting attacks?
- Sanitize user input before output
- Minify the output HTML
- Write the output inside a u003Cscriptu003E tag
- Only log the comment to the console
- Remove all whitespace from output
Improper Input Validation
Why is it insecure to use this logic? if (!input.isEmpty()) { process(input); }
- It does not validate the input's type or content
- It takes too much CPU time
- It never calls process(input)
- It validates input length only
- It throws a NullPointException
Untrusted Data in File Paths
A static analyzer flags this: File f = new File('/uploads/' + filename); What is the risk if filename comes from user input?
- Path traversal vulnerability
- File is always a .txt file
- Automatic file encryption
- Improved upload speed
- Lower memory usage
Insecure Cryptography Usage
The code uses MD5 for storing passwords: String hash = MD5(password); Why is this flagged as insecure?
- MD5 is fast and vulnerable to brute-force attacks
- MD5 uses too much memory
- MD5 requires a license fee
- MD5 changes the password format
- MD5 is only for image files
Command Injection
A scanner reports this as dangerous: Runtime.getRuntime().exec('ping ' + userInput); What is the best way to eliminate the command injection risk?
- Validate and whitelist userInput
- Use userInput.trim()
- Replace 'ping' with 'pong'
- Catch and ignore exceptions
- Log the command before execution
Sensitive Information in Logs
Review the logging statement: logger.info('User password: ' + password); What is the security vulnerability?
- Sensitive data exposure in logs
- Password is too long
- Logger might crash
- Logs are only accessible to admins
- Password is not encoded
Improper Use of Randomness
Why is this not secure for generating tokens? int token = (int)(Math.random() * 100000);
- Math.random() is not cryptographically secure
- token is too long
- Math.random() requires an internet connection
- The result is always negative
- The code is not written in Python
Use of Deprecated APIs
Static analysis warns: 'Use of deprecated API SecureRandom.getSeed(int)'. What should you do?
- Use a more current and secure API for random number generation
- Ignore the warning
- Increase the seed size
- Switch to Random.nextInt()
- Reduce the number of function calls