IC Improve Code Recipes

Chapter 7

Security Essentials

Security code in business applications is usually written once, early, by whoever happened to be on the project that week, and then copied for fifteen years. I have opened CryptoHelper classes that "encrypt" with a hard-coded key and a zero IV, PasswordUtility classes that store an unsalted MD5, and token generators built on new Random(). None of them were written carelessly. They were written to the standard of the day and never revisited.

The archive bears that out. Across 30 engagements there are 7,368 hand-written classes about security, cryptography or passwords, and 28 engagements have Active Directory or authentication classes, 3,000 in all. About 205 files still use the MD5 or SHA1 classes, in 15 engagements. And about 8,353 files build SQL text by string concatenation, in 30 engagements, which is the most common injection risk I find on any review.

This chapter replaces those helpers with short, current recipes: password hashing with PBKDF2 or the ASP.NET Core Identity hasher, authenticated encryption with AES-GCM, SHA-256 and HMAC for integrity, tokens from a cryptographic random generator, Active Directory lookups that dispose what they open, and the two injection defenses that matter most in line-of-business code: SQL parameters with allow-listed identifiers, and safe file paths.

One rule before the recipes: don't invent cryptography. Every recipe here is a thin, careful wrapper around primitives in the .NET base class library. The value is in choosing the right primitive and the right parameters, and in a storage format you can upgrade later.

Recipes

  1. 7.1Password Hashing with PBKDF2Passwords are stored in plain text, encrypted with a reversible key, or hashed with a fast algorithm such as MD5 or SHA-1, often without a salt. When…
  2. 7.2Encrypting Data with AES-GCMSome data must be readable by the application but not by someone holding a copy of the database: bank account numbers, national IDs, API credentials…
  3. 7.3Hashing and HMAC with SHA-256You need to check that a file wasn't corrupted in transfer, or that a webhook request really came from your payment provider and wasn't altered. The…
  4. 7.4Secure Random TokensPassword-reset links, e-mail confirmation codes, API keys and one-time download links need values nobody can guess. Legacy code uses new Random(),…
  5. 7.5Active Directory Lookups with System.DirectoryServices.AccountManagementIntranet applications need to look up a user's display name and e-mail, check group membership for authorization, and occasionally validate a user…
  6. 7.6Protecting Against SQL Injection and Path TraversalTwo attacks show up on almost every review I do. SQL injection, from search screens that splice user input into SQL, especially for sort columns and…

Chapter checklist

  • Search for MD5, SHA1, RijndaelManaged, new Random( and Guid.NewGuid in security code, and list every place they protect a password, token or secret.
  • Move password storage to PBKDF2 with a versioned format (or the Identity PasswordHasher), and wrap old MD5 or SHA-1 hashes immediately rather than waiting for logins.
  • Replace homegrown encryption with AES-GCM, random nonces and associated data, and keep the key in a secret store.
  • Verify webhooks and signed requests with HMAC-SHA-256, a timestamp and FixedTimeEquals.
  • Generate tokens with RandomNumberGenerator, store only their SHA-256 hashes, and give each an expiry and single use.
  • Dispose every PrincipalContext and Principal, use GetAuthorizationGroups for nested groups, and cache directory lookups.
  • Review every SQL string built with + or interpolation for user input, allow-list sort columns, and pass every untrusted file path through a SafePath-style check.