Skip to content
mentoor

Topic 5 of 6 · 4 min read

Unsafe defaults: CORS, debug mode and crypto

Goal: Identify an insecure default in generated code or config and replace it with a safe setting that still meets the requirement.

When something fails in development, an assistant often "fixes" it by turning a protection off. Those settings tend to ship.

Check yourself · 1 of 3

A browser blocks a call from https://app.example.com to your API. Which CORS setting is the right fix?

Choose one answer

Why it matters

A browser blocks a request and the model adds Access-Control-Allow-Origin: *. A certificate error appears and it sets verify=False. A password hash is needed and it picks MD5 because it is short. Each change makes the error vanish, and each change removes a safety. The code worked in the demo, so nobody looks again.

The settings to look for

  1. CORS. A wildcard origin, or reflecting any Origin header back with credentials allowed. Use an explicit list of your own front-end origins.
  2. Debug mode. DEBUG=True, Flask debug=True, verbose stack traces and framework debug pages. These show code and configuration to visitors.
  3. TLS. verify=False, rejectUnauthorized: false, or NODE_TLS_REJECT_UNAUTHORIZED=0. They allow anyone on the path to impersonate the server.
  4. Password hashing. MD5, SHA-1 or plain SHA-256 for passwords, or no salt. Use a slow password hash such as Argon2id, scrypt or bcrypt through a maintained library.
  5. Randomness. Math.random() or random.random() for tokens and reset codes. Use the operating system's secure source.
  6. Permissions. Containers running as root, chmod 777, cloud roles with * actions, databases open to the internet.

How to fix without breaking the feature

Ask what the setting was trying to solve, then solve that. A CORS error means the front end and API are on different origins: list that origin. A certificate error means the trust chain is wrong: install the right certificate or CA, not disable checking.

Before and after

# Before: works everywhere, protects nothing
resp = requests.get(url, verify=False)
hashed = hashlib.md5(password.encode()).hexdigest()

# After
resp = requests.get(url, timeout=10)   # verification is on by default
hashed = argon2.PasswordHasher().hash(password)

What goes wrong

  • A TODO to harden later that never gets done.
  • Config differences between environments. Debug on in production because a single file is shared.
  • Homemade crypto. The agent writes its own token or encryption scheme instead of using a vetted library.
  • Allowing credentials with a wildcard origin, which browsers reject, so the agent then reflects the origin instead.

Treat every flag that loosens a check as a finding until someone can explain why it is needed.

Examples

The CORS fix that opens every origin

app.use(cors({ origin: true, credentials: true }));

origin: true reflects whatever site sent the request and allows cookies, so any website can call the API as the logged-in user. Replace it with origin: ["https://app.example.com"].

The reset token from Math.random

const token = Math.random().toString(36).slice(2);

Math.random is predictable and is not meant for secrets. Use crypto.randomBytes(32).toString("hex"), store only its hash, and give it an expiry.

Key points

  • Assistants often silence an error by disabling a protection: wildcard CORS, debug mode or TLS verification.
  • Fix the cause of the error, such as listing the right origin or installing the right certificate, not the check.
  • Passwords need a slow dedicated hash such as Argon2id, scrypt or bcrypt, never MD5 or plain SHA.
  • Tokens and reset codes need a cryptographically secure random source, not Math.random.
  • Search a diff for verify=False, rejectUnauthorized, DEBUG, chmod 777 and a * origin or permission.

Common mistakes

  • Leaving verify=False or rejectUnauthorized: false in place because "it is just for testing".
  • Using a fast general-purpose hash for passwords because the code looks short and clean.
  • Allowing all origins with credentials to get past a CORS error.
  • Shipping the development config, including debug pages, to production.

Check yourself · 2 of 3

An agent adds verify=False to remove a certificate error. What does that do?

Choose one answer

Check yourself · 3 of 3

Which is the best choice for storing user passwords?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic