IT

How a fake website forced a complete rewrite of a PHP CMS core

A non-existent domain in a P2P network pushed a developer to conduct a deep audit and rewrite a PHP flat-file CMS core.

·2 min read
How a fake website forced a complete rewrite of a PHP CMS core

A developer recently released the first version of their pet project — a decentralized flat-file CMS written in pure PHP without databases, where all data is stored in JSON files. The concept treats each site as a planet in a shared network, communicating via P2P mail and a galactic radio. However, a clever field test by a reader who added a non-existent domain exposed critical architectural flaws.

Qisqacha - A fake P2P station exposed critical architectural vulnerabilities. - Race condition issues in JSON file operations were successfully fixed. - Heavy bcrypt hashing causing PHP-FPM crashes was replaced with SHA-256. - cURL connections now enforce strict SSL certificate verification.

Architectural flaws in JSON handling In the initial version, reading and writing JSON files relied on standard functions with the LOCK_EX flag, which only locks the file during the actual write operation. This led to race conditions where concurrent requests would overwrite each other's changes. to fix this, developers implemented a wrapper function pig_json_update() that maintains an exclusive lock throughout the entire read-modify-write cycle, completely preventing data loss during simultaneous user requests or incoming P2P messages.

Security and performance optimization Passwords were initially stored in plain text inside config.php, and attempting to use slow bcrypt hashing on low-end servers caused PHP-FPM workers to crash with 502 Bad Gateway errors. Dynamically rewriting PHP configuration files also corrupted the OPcache. Hashes were moved to an isolated data/users.json file, shifting to a fast cryptographically salted SHA-256 algorithm with timing-attack mitigation. Additionally, strict SSL verification was enabled for cURL requests with a safe fallback mechanism for older servers.

Privacy and brute-force protection To ensure privacy, the system stopped logging real guest IP addresses in daily metric logs, replacing them with an irreversible cryptographic hash generated using a unique site salt. This allows accurate unique visitor tracking without leaking personal data. Furthermore, the rigid IP ban after three failed login attempts was replaced with a dynamic mathematical captcha. This stops automated bots without accidentally blocking innocent users sharing the same corporate NAT network.

What this means for developers This case demonstrates that even small pet projects require rigorous attention to architectural security and stress testing. Real-world field tests conducted by external users often provide more value than automated static analysis by revealing overlooked edge cases.

Source: Все публикации подряд на Хабре

Related articles