Skip to content
Notifications
Clear all

Guide: Documenting a reproducible 'secure by design' prompt failure.

2 Posts
2 Users
0 Reactions
3 Views
(@stack_guardian)
Eminent Member
Joined: 4 months ago
Posts: 12
Topic starter   [#2548]

Hey everyone. We talk a lot about AI's potential, but for real-world adoption, we need to know where it cracks under pressure. This subforum is for those concrete, reproducible failures. I want to kick us off with a category I see causing real problems: **"secure by design" failures**, where the assistant suggests a solution that *looks* robust but introduces a subtle, critical vulnerability.

Here's a clean example from a code review scenario:

**The Prompt:**
"Write a Python function that validates a user's session token from a cookie. The token is a JWT. Check if it's valid and not expired."

**Typical Assistant Output:**
```python
import jwt

def validate_session_token(cookie_header, secret_key):
try:
# Extract token from 'Cookie: session=' header
token = cookie_header.split('session=')[1].split(';')[0]
payload = jwt.decode(token, secret_key, algorithms=['HS256'])
return payload
except (IndexError, jwt.InvalidTokenError):
return None
```

**Why This Fails (The Correct Answer):**
The function does decode the JWT, but it misses a crucial security step: **verifying the token's signature algorithm explicitly.** The `jwt.decode` call uses `algorithms=['HS256']`, but if the token header specifies `"alg": "none"` (a "none" algorithm), many older versions of the PyJWT library would *accept it* if the algorithm list wasn't strictly enforced. The correct, secure-by-design practice is to **always specify `algorithms=`** and never rely on a library's default. Even better, the extraction from the cookie header is fragile and doesn't handle malformed headers robustly.

**The Correct, Reproducible Fix:**
```python
import jwt

def validate_session_token(cookie_header, secret_key):
if not cookie_header:
return None
try:
# More robust extraction
cookies = [c.strip() for c in cookie_header.split(';')]
token = None
for c in cookies:
if c.startswith('session='):
token = c[8:] # len('session=')
break
if token is None:
return None

# EXPLICIT algorithm list is CRITICAL
payload = jwt.decode(token, secret_key, algorithms=['HS256'])
return payload
except jwt.InvalidTokenError:
return None
```
The key failure was the assistant providing a "working" but insecure default pattern. This is the kind of thing that slips into code reviews because it *looks* correct.

I'm looking for more examples like this—where the assistant's output is plausible, functional, but demonstrably insecure or flawed in a specific, reproducible way. Share your cases.

--mod


Be excellent to each other


   
Quote
(@moderator_mel)
Trusted Member
Joined: 4 months ago
Posts: 29
 

Exactly the kind of post we need. That JWT example is perfect because the failure is so subtle. The code *looks* correct, even to a developer who knows they should validate the signature.

One related nuance I've seen: even if you specify the algorithm, some assistants will default to the `jwt.decode` option `verify=True` without mentioning it. A user might then copy the pattern but later set `verify=False` for 'testing', and that dangerous line gets committed. The secure pattern should always make the verification explicit and hard to remove accidentally.

Thanks for starting with a concrete, copy-paste example. It sets the right standard.


No receipts, no trust.


   
ReplyQuote