Repository navigation
Expand file tree
/
Copy pathAuthenticateSshSignature.php
More file actions
95 lines (74 loc) · 3.08 KB
/
Copy pathAuthenticateSshSignature.php
File metadata and controls
95 lines (74 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
declare(strict_types=1);
namespace App\Http\Middleware;
use App\Actions\CreateCommitImages;
use App\Services\NullBytes;
use App\Services\SignedCommits;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;
use InvalidArgumentException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Response;
/**
* Authenticates a commit signed with the author's SSH key, as sent by the `devnull commit` script.
*/
final readonly class AuthenticateSshSignature
{
public function __construct(private SignedCommits $signedCommits, private NullBytes $nullBytes)
{
//
}
/**
* @param Closure(Request): (Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
$withImages = $request->files->has('images');
try {
$commit = $this->signedCommits->verify(
$withImages ? $this->payloadFile($request) : $request->getContent(),
$request->headers->get('X-SSH-Signature', '') ?? '',
$withImages ? fn (): array => $this->imageHashes($request) : null,
);
} catch (InvalidArgumentException $invalidArgumentException) {
abort(401, $invalidArgumentException->getMessage());
}
abort_if($commit['user']->isBanned(), 403, __('Your account is banned from /dev/null.'));
Auth::setUser($commit['user']);
$request->merge([
'body' => $commit['body'],
'reply_to' => $commit['reply_to'],
]);
// The body comes out of the signed payload only now, after the global middleware took NUL bytes out of the inputs.
$this->nullBytes->strip($request);
return $next($request);
}
private function payloadFile(Request $request): string
{
$payload = $request->files->get('payload');
$contents = $payload instanceof UploadedFile && $payload->isValid() ? file_get_contents($payload->getPathname()) : false;
return is_string($contents) ? $contents : '';
}
/**
* @return list<string>
*/
private function imageHashes(Request $request): array
{
$images = $request->files->all()['images'] ?? [];
$images = is_array($images) ? $images : [$images];
throw_if(count($images) > CreateCommitImages::MAX_IMAGES, InvalidArgumentException::class, 'The attached images do not match the signed payload.');
$hashes = [];
foreach ($images as $image) {
if ($image instanceof UploadedFile && ! $image->isValid()) {
throw ValidationException::withMessages(['images' => __('validation.uploaded', ['attribute' => 'image'])]);
}
$path = $image instanceof UploadedFile ? $image->getRealPath() : false;
$hash = is_string($path) ? hash_file('sha256', $path) : false;
throw_if($hash === false, InvalidArgumentException::class, 'The attached images could not be read.');
$hashes[] = $hash;
}
return $hashes;
}
}