Skip to content

Commit

Permalink
Prevent plugin enumeration by throwing 404 for known endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Saboteur777 committed Oct 15, 2020
1 parent 0fb93a8 commit 1cc6bb8
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions src/controllers/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
use webmenedzser\reporter\Reporter;

use Craft;
use craft\behaviors\EnvAttributeParserBehavior;
use craft\web\Controller;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
use yii\web\UnauthorizedHttpException;

/**
Expand All @@ -28,30 +28,24 @@ class BaseController extends Controller
/**
* Checks if the request should be fulfilled or not.
*
* @throws BadRequestHttpException
* @throws UnauthorizedHttpException
* @throws NotFoundHttpException
*/
protected function checkIfAuthenticated()
protected function checkIfAuthenticated() : void
{
if (!Craft::$app->request->isPost) {
$message = 'Only POST requests are supported.';

throw new BadRequestHttpException($message);
}

$path = Craft::$app->request->getFullPath();
$key = Craft::$app->request->getParam('key');
$apiKey = Craft::parseEnv(Reporter::$plugin->getSettings()->apiKey);

if (!$key) {
$message = 'Missing parameter: `key`.';
if (!Craft::$app->request->isPost) {
throw new NotFoundHttpException('Template not found: ' . $path);
}

throw new BadRequestHttpException($message);
if (!$key) {
throw new NotFoundHttpException('Template not found: ' . $path);
}

if ($key !== $apiKey) {
$message = 'Unauthenticated access is not allowed.';

throw new UnauthorizedHttpException($message);
throw new NotFoundHttpException('Template not found: ' . $path);
}
}
}

0 comments on commit 1cc6bb8

Please sign in to comment.