# redirect() without return: the 302 that stopped nothing

> An access check in a legacy Yii2 admin panel sent a redirect but never returned it, so the action kept running and saved a guest's POST. Ten more places had the same bug.

Source: https://ebrahimsameh.com/blog/redirect-without-return/
Updated: 2026-11-25

In many web frameworks, calling `redirect()` doesn't end the request. It sets a `Location` header and a 302 status, and then your code keeps running. If an access check redirects without returning or throwing, the browser gets bounced, but everything after the check still happens. I found this in a legacy Yii2 admin panel, where it let a guest write to a record.

## What happened

A controller had a helper that checked whether the current user could touch a record. When the answer was no, it called `$this->redirect()`. Stripped down, it looked like this:

```php
private function checkAccess($model)
{
    if (!$this->canUserAccess($model)) {
        $this->redirect(['site/index']); // sets a header, nothing more
    }
}

public function actionUpdate($id)
{
    $model = $this->findModel($id); // calls checkAccess()
    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $id]);
    }
    return $this->render('update', ['model' => $model]);
}
```

In Yii2, `Controller::redirect()` only puts a `Location` header on the shared response object. The access check returned normally, `findModel()` returned the record, and `actionUpdate()` went on to load the POST data and call `save()`. The 302 was cosmetic.

On its own this might have been caught somewhere else. Here nothing else stopped it:

- The `update` action wasn't in the access filter's `only` list, so the login rule never ran for it.
- The base controller's `beforeAction()` handled two-factor checks for users who were already logged in and never blocked a guest.
- CSRF validation was turned off on a related controller, with the comment `//testing`.

The permission check correctly returned `false` for a guest. That was the whole defence, and it never halted anything.

## How it was proven

A functional test sent a guest request to the update action. Run against the old code, the request reached `save()`, and the log showed the model attempting an update and failing validation. The only thing that stopped the write was a validation error. Every new test in the fix was also run against the old code to make sure it failed there, so none of them pass by accident.

## Ten more of the same

The same audit found ten more access checks with the same shape, and each had code after the check that kept running: cancelling another tenant's ticket, updating and deleting records, sending notifications. In one place a subclass had replaced its parent's check, which threw an exception, with a version that only redirected. That quietly turned a hard stop into a suggestion.

All of them now throw, the same way the base CRUD controller's own check already did. An `HttpException` ends the action, and the framework turns it into an error response:

```php
private function checkAccess($model)
{
    if (!$this->canUserAccess($model)) {
        throw new MethodNotAllowedHttpException('Access to the requested page is forbidden.');
    }
}
```

The order of the commits mattered. Some legitimate users, root admins on certain screens, were being bounced by these checks because their permission rule was missing. Converting the checks to throw first would have turned that bounce into an error page for them. So the missing rules landed first and the halt conversion after. One payment screen got its own commit, with a log check required before merging, because it was the one place where a bounce might have been normal use.

A later fix closed a sibling of the `only` list problem: actions inherited from a base CRUD controller weren't in a subclass's `only` list, so they ran with no login at all. They were added to the list, and every inherited action got a test that a guest is sent to login first.

## What to check in your own code

- Grep for `redirect(` inside guard or check functions. If the call isn't returned or followed by an exit, the caller keeps going.
- Make refusals throw. An exception stops the action; a header doesn't.
- If your access filter takes an allow-list of action names, check that every write action, including inherited ones, is on it.
- When you fix one instance, search the codebase for the same shape and fix those too.
- Test refusals by asserting that the data didn't change. A 302 in the response proves nothing.
