Bird
0
0

What is wrong with this API key validation snippet?

medium📝 Analysis Q7 of 15
Microservices - Authentication and Authorization
What is wrong with this API key validation snippet?
function validateKey(request) {
  if (request.headers['Authorization'] === null) {
    return 'No key'
  }
  return 'Valid key'
}
ANo error, code works as expected
BIt uses strict equality which is incorrect here
CIt should check for empty string instead of null
DIt only checks for null, missing or undefined keys are ignored
Step-by-Step Solution
Solution:
  1. Step 1: Analyze key existence check

    Checking only for null misses undefined or missing keys.
  2. Step 2: Understand impact on validation

    Missing keys will not trigger 'No key' message, causing incorrect validation.
  3. Final Answer:

    It only checks for null, missing or undefined keys are ignored -> Option D
  4. Quick Check:

    Check for all missing cases, not just null [OK]
Quick Trick: Check for undefined and null when validating keys [OK]
Common Mistakes:
MISTAKES
  • Checking only null but ignoring undefined
  • Assuming strict equality covers all missing cases

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Microservices Quizzes