さすがにServiceからHttpExceptionは雑すぎたか
This commit is contained in:
parent
16b5fb3533
commit
0a9920b11c
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers\Api;
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use App\MetadataResolver\DeniedHostException;
|
||||||
use App\Services\MetadataResolveService;
|
use App\Services\MetadataResolveService;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
@ -13,7 +14,11 @@ class CardController
|
|||||||
'url:required|url'
|
'url:required|url'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
try {
|
||||||
$metadata = $service->execute($request->input('url'));
|
$metadata = $service->execute($request->input('url'));
|
||||||
|
} catch (DeniedHostException $e) {
|
||||||
|
abort(403, $e->getMessage());
|
||||||
|
}
|
||||||
$metadata->load('tags');
|
$metadata->load('tags');
|
||||||
|
|
||||||
$response = response($metadata);
|
$response = response($metadata);
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace App\Listeners;
|
namespace App\Listeners;
|
||||||
|
|
||||||
use App\Events\LinkDiscovered;
|
use App\Events\LinkDiscovered;
|
||||||
|
use App\MetadataResolver\DeniedHostException;
|
||||||
use App\Services\MetadataResolveService;
|
use App\Services\MetadataResolveService;
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
use Illuminate\Queue\InteractsWithQueue;
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
@ -32,6 +33,8 @@ class LinkCollector
|
|||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$this->metadataResolveService->execute($event->url);
|
$this->metadataResolveService->execute($event->url);
|
||||||
|
} catch (DeniedHostException $e) {
|
||||||
|
// ignored
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
// 今のところこのイベントは同期実行されるので、上流をクラッシュさせないために雑catchする
|
// 今のところこのイベントは同期実行されるので、上流をクラッシュさせないために雑catchする
|
||||||
report($e);
|
report($e);
|
||||||
|
30
app/MetadataResolver/DeniedHostException.php
Normal file
30
app/MetadataResolver/DeniedHostException.php
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\MetadataResolver;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* メタデータの解決を禁止しているホストに対して取得を試み、ブロックされたことを表します。
|
||||||
|
*/
|
||||||
|
class DeniedHostException extends Exception
|
||||||
|
{
|
||||||
|
private $url;
|
||||||
|
|
||||||
|
public function __construct(string $url, Throwable $previous = null)
|
||||||
|
{
|
||||||
|
parent::__construct("Access denied by system policy: $url", 0, $previous);
|
||||||
|
$this->url = $url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getUrl(): string
|
||||||
|
{
|
||||||
|
return $this->url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getHost(): string
|
||||||
|
{
|
||||||
|
return parse_url($this->url, PHP_URL_HOST);
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@
|
|||||||
namespace App\Services;
|
namespace App\Services;
|
||||||
|
|
||||||
use App\Metadata;
|
use App\Metadata;
|
||||||
|
use App\MetadataResolver\DeniedHostException;
|
||||||
use App\MetadataResolver\MetadataResolver;
|
use App\MetadataResolver\MetadataResolver;
|
||||||
use App\Tag;
|
use App\Tag;
|
||||||
use App\Utilities\Formatter;
|
use App\Utilities\Formatter;
|
||||||
@ -29,7 +30,7 @@ class MetadataResolveService
|
|||||||
|
|
||||||
// 自分自身は解決しない
|
// 自分自身は解決しない
|
||||||
if (parse_url($url, PHP_URL_HOST) === parse_url(config('app.url'), PHP_URL_HOST)) {
|
if (parse_url($url, PHP_URL_HOST) === parse_url(config('app.url'), PHP_URL_HOST)) {
|
||||||
abort(403);
|
throw new DeniedHostException($url);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 無かったら取得
|
// 無かったら取得
|
||||||
|
Loading…
Reference in New Issue
Block a user