Skip to main content

7. What Is Secure Coding

Secure coding means writing code that remains reliable and safe even when faced with invalid, malicious, or unexpected input. It prevents errors from becoming exploits.

Insecure Example

data = input("Enter JSON: ")
obj = eval(data) # Executes arbitrary code!
print("Parsed:", obj)

Secure Example

import json

data = input("Enter JSON: ")
try:
obj = json.loads(data)
print("Parsed safely:", obj)
except json.JSONDecodeError:
print("Invalid JSON format.")

Lesson: Avoid unsafe parsing and evaluation. Use safe modules that restrict code execution.