AG
2025-03-10•6 min read

Security by default in the OSTA Job Portal — without a framework's safety net

A government recruitment platform in plain PHP 8 + MySQL. No framework means CSRF, rate limiting, RBAC, and audit logging are yours to build — and a one-line dev convenience nearly shipped a live OTP disclosure.

PHPSecurityMySQL

The OSTA Job Portal is a recruitment management system for the Oromia Science & Technology Authority — applicant, employer, and admin portals on one PHP 8 + MySQL platform. It's built in plain PHP: no Laravel, no Symfony, no framework. That choice is the security story, because everything a framework would normally hand me — CSRF middleware, rate limiting, RBAC, audit logging — had to be built, tested, and reasoned about explicitly. Here's what that looked like, including the bug that taught me the most.

Passwords: Argon2id, and only the standard path

Password hashing is PHP's native Argon2id — the memory-hard winner of the password hashing competition:

$hash = password_hash($password, PASSWORD_ARGON2ID);

No custom salting, no pre-hashing, no "improvements." The trade-off here is a strange one to explain: the secure choice is to write less code. Every line of custom cryptography is a line that can be subtly wrong in a way tests won't catch. Verification goes through password_verify, rehashing on login is handled by PHP itself when defaults improve. The discipline is refusing to touch it.

CSRF: short code, timed comparison

Cross-site request protection is its own middleware — POST requests must carry a token matching the session:

class CsrfMiddleware
{
    public function handle(): bool
    {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $token = $_POST['csrf_token'] ?? $_SERVER['HTTP_X_CSRF_TOKEN'] ?? '';

            if (!isset($_SESSION['csrf_token']) || !hash_equals($_SESSION['csrf_token'], $token)) {
                http_response_code(403);
                echo json_encode(['error' => 'Invalid CSRF token']);
                return false;
            }
        }

        return true;
    }
}

Two details carry the weight. hash_equals is the timing-safe comparison — string equality in PHP short-circuits, and a hostile actor can measure that. And the middleware returns false rather than throwing, so the pipeline stays a straight line you can read top to bottom. The trade-off versus a framework's rotating per-request tokens: a session-scoped token lives longer, which slightly widens the window for a stolen token. Against that, the implementation is 15 lines I fully understand, on a platform where "I understand every piece of the security path" is the actual requirement.

Rate limiting: honest about what it can't do

Auth endpoints are rate limited by a hand-rolled sliding window: per-IP counters in the session store, 429 with a Retry-After header when the window overflows.

The interesting part is being honest about the model's limits. Session/IP-based counting assumes one server and truthful IPs — behind a load balancer or a proxy you'd need shared storage (Redis) and the real client IP from trusted headers. On a single-server government deployment, the simple model is correct. If the platform ever scales out, the middleware interface stays; the storage swaps. Security code that pretends it scales when it doesn't is worse than code that documents its ceiling.

RBAC and uploads: guard the directory, not just the route

Three roles — applicant, employer, admin — enforced through dedicated middleware on every protected route, checked server-side on each request. Document uploads get their own hardening: an upload directory outside the web root where possible, MIME and extension validation, and resume downloads routed through an authorized endpoint that streams the file — never a public URL someone can guess or share. The rule of thumb: anything a user uploaded should be unreachable except through code that first asks "who is asking?"

The bug that taught me the most: a dev convenience in production

Email verification uses one-time codes. During development I made the verify page display the OTP in the browser so I didn't have to keep checking my inbox. The line that guarded it was:

<?php if (isset($_SESSION['_dev_otp'])): ?>

Read it carefully — it checks whether the OTP exists, not where the page is running. In production, that dev code path was live: the page that asks a user to verify their email could print the verification code to anyone whose session contained it. An account-takeover primitive, shipped by me, in the name of convenience.

The fix is one environment gate:

<?php if (isset($_SESSION['_dev_otp']) && env('APP_ENV', 'production') !== 'production'): ?>

The lesson outlived the bug: development conveniences must be gated on the environment by default, and "by default" means the code fails closed when the variable is missing — notice env('APP_ENV', 'production') assumes production when unset. Any path that exists "only in dev" is a production path you haven't audited yet. This is now the first thing I look for in code review, mine or anyone else's.

Verified, not claimed

A secure system you can't verify is just a claim, so the repo ships with the checks that keep the claims honest: a lint sweep (scripts/lint.php), unit tests for the security-critical helpers (sanitization, token handling), smoke tests that exercise the real request paths, Playwright end-to-end suites covering auth, applications, and admin flows — all wired into CI. The README states plainly what doesn't exist (no live demo, no shared demo accounts) because a security posture that overstates itself is itself a vulnerability.

What "secure by default" means here

Not a feature list — a direction of failure. Hashing that refuses customization, comparisons that refuse to leak timing, rate limits that document their ceiling, uploads that fail closed, and dev tools that can't follow the code into production. The framework-less stack made every one of those an explicit decision. That's the trade I'd make again: less leverage, more understanding — on the system where understanding is the security.