yii2
yii2 copied to clipboard
HTTP Auth credentials parsing fails with malformed base64, causing JSON encoding errors.
The getAuthCredentials() method in yii\web\Request does not validate base64-decoded data from the Authorization header, causing applications throw InvalidArgumentException: Malformed UTF-8 characters when the response is JSON encoded.
Steps to Reproduce
-
Send a request with malformed
Authorizationheader:GET /api/endpoint HTTP/1.1 Authorization: Basic foo:bar -
In your controller action:
public function actionAuth(): array { Yii::$app->response->format = Response::FORMAT_JSON; return [ 'username' => Yii::$app->request->getAuthUser(), 'password' => Yii::$app->request->getAuthPassword(), ]; } -
Expected: Returns
{"username": null, "password": null}with200status. -
Actual: Throws
InvalidArgumentException: Malformed UTF-8 characters, possibly incorrectly encodedwith500status.
need to add mb_check_encoding($decoded, 'UTF-8') checker