Fix federation (#6346)

* Prepared new docker

* Docker setup improvements and fixes

* Update DOCKER_SETUP.md

* Update DOCKER_SETUP.md

* Update DOCKER_SETUP.md

* Update DOCKER_SETUP.md

* Update DOCKER_SETUP.md

* Update DOCKER_SETUP.md

* Update .dockerignore

* DB/Redis health check

* updated health checks

* updated install guide

* updated DOCKER_SETUP

* Updated DOCKER_SETUP

* Updated DOCKER_SETUP

* Update and rename DOCKER_SETUP.md to DOCKER_COMPOSE_SETUP.md

* RemoveUnreachableStatementRector

* Update GroupActivityPubService.php

* Larastan: Add @property

* Delete rector.php

* more properties

* 2fa_enabled needs to be refactored

* Fix AdminUser.php syntax error - remove invalid property declaration

* Update AdminUser.php

* update

* Upgrade predis from v2.0 to v3.2

- Update predis/predis from ^2.0 to ^3.2
- Predis v3.2.0 includes RESP3 support, improved pipeline abstractions
- No code changes required, configuration remains compatible
- Tested successfully with tinker

* Upgrade predis from v2.0 to v3.2

* Update

* Update .env.docker.example

* Update DOCKER_COMPOSE_SETUP.md

* Create ImageDriverManager.php

* Update image.php

* Update ImageResizePipeline.php

* Update StoryComposeController.php

* Update AvatarOptimize.php

* Update Image.php

* Update composer.json

* Update AvatarOptimize.php

* update lock

* Update composer.lock

* update

* update

* update

* Allow to set new image types in admin interface

* Update composer, fix deps

* Update composer

* Update DOCKER_COMPOSE_SETUP.md - Added troubleshooting (#6344)

* Staging (#6343)

* Prepared new docker

* Docker setup improvements and fixes

* Update DOCKER_SETUP.md

* Update DOCKER_SETUP.md

* Update DOCKER_SETUP.md

* Update DOCKER_SETUP.md

* Update DOCKER_SETUP.md

* Update DOCKER_SETUP.md

* Update .dockerignore

* DB/Redis health check

* updated health checks

* updated install guide

* updated DOCKER_SETUP

* Updated DOCKER_SETUP

* Updated DOCKER_SETUP

* Update and rename DOCKER_SETUP.md to DOCKER_COMPOSE_SETUP.md

* RemoveUnreachableStatementRector

* Update GroupActivityPubService.php

* Larastan: Add @property

* Delete rector.php

* more properties

* 2fa_enabled needs to be refactored

* Fix AdminUser.php syntax error - remove invalid property declaration

* Update AdminUser.php

* update

* Upgrade predis from v2.0 to v3.2

- Update predis/predis from ^2.0 to ^3.2
- Predis v3.2.0 includes RESP3 support, improved pipeline abstractions
- No code changes required, configuration remains compatible
- Tested successfully with tinker

* Upgrade predis from v2.0 to v3.2

* Update

* Update .env.docker.example

* Update DOCKER_COMPOSE_SETUP.md

* Create ImageDriverManager.php

* Update image.php

* Update ImageResizePipeline.php

* Update StoryComposeController.php

* Update AvatarOptimize.php

* Update Image.php

* Update composer.json

* Update AvatarOptimize.php

* update lock

* Update composer.lock

* update

* update

* update

* Allow to set new image types in admin interface

* Update composer, fix deps

* Update composer

---------

Co-authored-by: Shlee <github@shl.ee>
Co-authored-by: Your Name <you@example.com>
Co-authored-by: Severin <savewish@icloud.com>

* Update DOCKER_COMPOSE_SETUP.md

* Update DOCKER_COMPOSE_SETUP.md

---------

Co-authored-by: dansup <danielsupernault@gmail.com>
Co-authored-by: Your Name <you@example.com>
Co-authored-by: Severin <savewish@icloud.com>

* Regression - Fix federation - Update ActivityHandler.php (#6345)

* Update ActivityHandler.php

* Update ActivityHandler.php

* Revert inbox changes

* Fix InboxWorker

---------

Co-authored-by: Shlee <github@shl.ee>
Co-authored-by: Your Name <you@example.com>
Co-authored-by: Severin <savewish@icloud.com>
pull/6349/head
dansup 6 days ago committed by GitHub
parent db0b096798
commit 40a5130430
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -34,6 +34,14 @@ This setup uses `serversideup/php:8.4-fpm-nginx` as the base image and is design
docker compose build
```
#### Container Build Troubleshooting ####
`open /home/username/pixelfed/storage/app/public/m/_v2/xxxxxxxxxxxxxxxxxx/xxxxxxxxxxx-xxxxxxxxxx/xxxxxxxxxxxx: permission denied` or similar might require fixing local permissions.
```bash
sudo find storage/ -type d -exec chmod 755 {} \; # set all directories to rwx by user/group
sudo find storage/ -type f -exec chmod 644 {} \; # set all files to rw by user/group
```
4. **Build and start the containers:**
```bash
docker compose up -d db redis # Bootstrap the database and Redis.

@ -166,7 +166,7 @@ class FederationController extends Controller
$headers = $request->headers->all();
$payload = $request->getContent();
if (! $payload) {
if (! $payload || empty($payload)) {
return;
}
$obj = json_decode($payload, true, 8);
@ -220,7 +220,7 @@ class FederationController extends Controller
$headers = $request->headers->all();
$payload = $request->getContent();
if (! $payload) {
if (! $payload || empty($payload)) {
return;
}

@ -42,33 +42,7 @@ class ActivityHandler implements ShouldQueue
*/
public function handle()
{
$headers = $this->headers;
$username = $this->username;
$payload = $this->payload;
// Verify required data exists
if (!$headers) {
Log::info("ActivityHandler: No headers provided, skipping job");
return;
}
if (!$username) {
Log::info("ActivityHandler: No username provided, skipping job");
return;
}
if (!$payload) {
Log::info("ActivityHandler: No payload provided, skipping job");
return;
}
try {
(new Inbox($headers, $username, $payload))->handle();
} catch (\Exception $e) {
Log::warning("ActivityHandler: Failed to handle activity for username {$username}: " . $e->getMessage());
throw $e;
}
(new Inbox($this->headers, $this->username, $this->payload))->handle();
return;
}
}

@ -5,9 +5,9 @@ namespace App\Jobs\InboxPipeline;
use Cache;
use App\Profile;
use App\Util\ActivityPub\{
Helpers,
HttpSignature,
Inbox
Helpers,
HttpSignature,
Inbox
};
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
@ -18,229 +18,202 @@ use App\Jobs\DeletePipeline\DeleteRemoteProfilePipeline;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Lottery;
use Illuminate\Support\Facades\Log;
class InboxValidator implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $username;
protected $headers;
protected $payload;
public $timeout = 300;
public $tries = 1;
public $maxExceptions = 1;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($username, $headers, $payload)
{
$this->username = $username;
$this->headers = $headers;
$this->payload = $payload;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$username = $this->username;
$headers = $this->headers;
$payload = $this->payload;
// Verify username exists
if (!$username) {
Log::info("InboxValidator: Username not provided, skipping job");
return;
}
// Verify headers exist
if (!$headers) {
Log::info("InboxValidator: Headers not provided, skipping job");
return;
}
// Verify payload exists
if (!$payload) {
Log::info("InboxValidator: Payload not provided, skipping job");
return;
}
if(!$headers || !$payload || !isset($headers['signature']) || !isset($headers['date'])) {
Log::info("InboxValidator: Invalid headers or payload structure, skipping job");
return;
}
$payload = json_decode($payload, true, 8);
if(isset($payload['id'])) {
$lockKey = 'pf:ap:user-inbox:activity:' . hash('sha256', $payload['id']);
if(Cache::get($lockKey) !== null) {
// Job processed already
return 1;
}
Cache::put($lockKey, 1, 3600);
}
$profile = Profile::whereNull('domain')->whereUsername($username)->first();
// Verify profile exists
if (!$profile) {
Log::info("InboxValidator: Profile not found for username {$username}, skipping job");
return;
}
if(!$profile || !$headers || !$payload) {
return;
}
if($profile->status != null) {
return;
}
if($this->verifySignature($headers, $profile, $payload) == true) {
if(isset($payload['type']) && in_array($payload['type'], ['Follow', 'Accept']) ) {
ActivityHandler::dispatch($headers, $profile, $payload)->onQueue('follow');
} else {
$onQueue = Lottery::odds(1, 12)->winner(fn () => 'high')->loser(fn () => 'inbox')->choose();
ActivityHandler::dispatch($headers, $profile, $payload)->onQueue($onQueue);
}
return;
} else {
return;
}
}
protected function verifySignature($headers, $profile, $payload)
{
$body = $this->payload;
$bodyDecoded = $payload;
$signature = is_array($headers['signature']) ? $headers['signature'][0] : $headers['signature'];
$date = is_array($headers['date']) ? $headers['date'][0] : $headers['date'];
if(!$signature) {
return false;
}
if(!$date) {
return false;
}
if(!now()->parse($date)->gt(now()->subDays(1)) ||
!now()->parse($date)->lt(now()->addDays(1))
) {
return false;
}
if(!isset($bodyDecoded['id'])) {
return false;
}
$signatureData = HttpSignature::parseSignatureHeader($signature);
if(!isset($signatureData['keyId'], $signatureData['signature'], $signatureData['headers']) || isset($signatureData['error'])) {
return false;
}
$keyId = Helpers::validateUrl($signatureData['keyId']);
$id = Helpers::validateUrl($bodyDecoded['id']);
$keyDomain = parse_url($keyId, PHP_URL_HOST);
$idDomain = parse_url($id, PHP_URL_HOST);
if(isset($bodyDecoded['object'])
&& is_array($bodyDecoded['object'])
&& isset($bodyDecoded['object']['attributedTo'])
) {
$attr = Helpers::pluckval($bodyDecoded['object']['attributedTo']);
if(is_array($attr)) {
if(isset($attr['id'])) {
$attr = $attr['id'];
} else {
$attr = "";
}
}
if(parse_url($attr, PHP_URL_HOST) !== $keyDomain) {
return false;
}
}
if(!$keyDomain || !$idDomain || $keyDomain !== $idDomain) {
return false;
}
$actor = Profile::whereKeyId($keyId)->first();
if(!$actor) {
$actorUrl = Helpers::pluckval($bodyDecoded['actor']);
$actor = Helpers::profileFirstOrNew($actorUrl);
}
if(!$actor) {
return false;
}
$pkey = openssl_pkey_get_public($actor->public_key);
if(!$pkey) {
return false;
}
$inboxPath = "/users/{$profile->username}/inbox";
list($verified, $headers) = HttpSignature::verify($pkey, $signatureData, $headers, $inboxPath, $body);
if($verified == 1) {
return true;
} else {
return false;
}
}
protected function blindKeyRotation($headers, $profile, $payload)
{
$signature = is_array($headers['signature']) ? $headers['signature'][0] : $headers['signature'];
$date = is_array($headers['date']) ? $headers['date'][0] : $headers['date'];
if(!$signature) {
return;
}
if(!$date) {
return;
}
if(!now()->parse($date)->gt(now()->subDays(1)) ||
!now()->parse($date)->lt(now()->addDays(1))
) {
return;
}
$signatureData = HttpSignature::parseSignatureHeader($signature);
if(!isset($signatureData['keyId'], $signatureData['signature'], $signatureData['headers']) || isset($signatureData['error'])) {
return;
}
$keyId = Helpers::validateUrl($signatureData['keyId']);
$actor = Profile::whereKeyId($keyId)->whereNotNull('remote_url')->first();
if(!$actor) {
return;
}
if(Helpers::validateUrl($actor->remote_url) == false) {
return;
}
try {
$res = Http::withOptions(['allow_redirects' => false])->timeout(20)->withHeaders([
'Accept' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
'User-Agent' => 'PixelfedBot v0.1 - https://pixelfed.org',
])->get($actor->remote_url);
} catch (ConnectionException $e) {
return false;
}
if(!$res->ok()) {
return false;
}
$res = json_decode($res->body(), true, 8);
if(!$res || !isset($res['publicKey']) || !isset($res['publicKey']['id'])) {
return;
}
if($res['publicKey']['id'] !== $actor->key_id) {
return;
}
$actor->public_key = $res['publicKey']['publicKeyPem'];
$actor->save();
return $this->verifySignature($headers, $profile, $payload);
}
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $username;
protected $headers;
protected $payload;
public $timeout = 300;
public $tries = 1;
public $maxExceptions = 1;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($username, $headers, $payload)
{
$this->username = $username;
$this->headers = $headers;
$this->payload = $payload;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$username = $this->username;
$headers = $this->headers;
if(empty($headers) || empty($this->payload) || !isset($headers['signature']) || !isset($headers['date'])) {
return;
}
$payload = json_decode($this->payload, true, 8);
if(isset($payload['id'])) {
$lockKey = 'pf:ap:user-inbox:activity:' . hash('sha256', $payload['id']);
if(Cache::get($lockKey) !== null) {
// Job processed already
return 1;
}
Cache::put($lockKey, 1, 3600);
}
$profile = Profile::whereNull('domain')->whereUsername($username)->first();
if(empty($profile) || empty($headers) || empty($payload)) {
return;
}
if($profile->status != null) {
return;
}
if($this->verifySignature($headers, $profile, $payload) == true) {
if(isset($payload['type']) && in_array($payload['type'], ['Follow', 'Accept']) ) {
ActivityHandler::dispatch($headers, $profile, $payload)->onQueue('follow');
} else {
$onQueue = Lottery::odds(1, 12)->winner(fn () => 'high')->loser(fn () => 'inbox')->choose();
ActivityHandler::dispatch($headers, $profile, $payload)->onQueue($onQueue);
}
return;
} else {
return;
}
}
protected function verifySignature($headers, $profile, $payload)
{
$body = $this->payload;
$bodyDecoded = $payload;
$signature = is_array($headers['signature']) ? $headers['signature'][0] : $headers['signature'];
$date = is_array($headers['date']) ? $headers['date'][0] : $headers['date'];
if(!$signature) {
return false;
}
if(!$date) {
return false;
}
if(!now()->parse($date)->gt(now()->subDays(1)) ||
!now()->parse($date)->lt(now()->addDays(1))
) {
return false;
}
if(!isset($bodyDecoded['id'])) {
return false;
}
$signatureData = HttpSignature::parseSignatureHeader($signature);
if(!isset($signatureData['keyId'], $signatureData['signature'], $signatureData['headers']) || isset($signatureData['error'])) {
return false;
}
$keyId = Helpers::validateUrl($signatureData['keyId']);
$id = Helpers::validateUrl($bodyDecoded['id']);
$keyDomain = parse_url($keyId, PHP_URL_HOST);
$idDomain = parse_url($id, PHP_URL_HOST);
if(isset($bodyDecoded['object'])
&& is_array($bodyDecoded['object'])
&& isset($bodyDecoded['object']['attributedTo'])
) {
$attr = Helpers::pluckval($bodyDecoded['object']['attributedTo']);
if(is_array($attr)) {
if(isset($attr['id'])) {
$attr = $attr['id'];
} else {
$attr = "";
}
}
if(parse_url($attr, PHP_URL_HOST) !== $keyDomain) {
return false;
}
}
if(!$keyDomain || !$idDomain || $keyDomain !== $idDomain) {
return false;
}
$actor = Profile::whereKeyId($keyId)->first();
if(!$actor) {
$actorUrl = Helpers::pluckval($bodyDecoded['actor']);
$actor = Helpers::profileFirstOrNew($actorUrl);
}
if(!$actor) {
return false;
}
$pkey = openssl_pkey_get_public($actor->public_key);
if(!$pkey) {
return false;
}
$inboxPath = "/users/{$profile->username}/inbox";
list($verified, $headers) = HttpSignature::verify($pkey, $signatureData, $headers, $inboxPath, $body);
if($verified == 1) {
return true;
} else {
return false;
}
}
protected function blindKeyRotation($headers, $profile, $payload)
{
$signature = is_array($headers['signature']) ? $headers['signature'][0] : $headers['signature'];
$date = is_array($headers['date']) ? $headers['date'][0] : $headers['date'];
if(!$signature) {
return;
}
if(!$date) {
return;
}
if(!now()->parse($date)->gt(now()->subDays(1)) ||
!now()->parse($date)->lt(now()->addDays(1))
) {
return;
}
$signatureData = HttpSignature::parseSignatureHeader($signature);
if(!isset($signatureData['keyId'], $signatureData['signature'], $signatureData['headers']) || isset($signatureData['error'])) {
return;
}
$keyId = Helpers::validateUrl($signatureData['keyId']);
$actor = Profile::whereKeyId($keyId)->whereNotNull('remote_url')->first();
if(!$actor) {
return;
}
if(Helpers::validateUrl($actor->remote_url) == false) {
return;
}
try {
$res = Http::withOptions(['allow_redirects' => false])->timeout(20)->withHeaders([
'Accept' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
'User-Agent' => 'PixelfedBot v0.1 - https://pixelfed.org',
])->get($actor->remote_url);
} catch (ConnectionException $e) {
return false;
}
if(!$res->ok()) {
return false;
}
$res = json_decode($res->body(), true, 8);
if(!$res || empty($res) || !isset($res['publicKey']) || !isset($res['publicKey']['id'])) {
return;
}
if($res['publicKey']['id'] !== $actor->key_id) {
return;
}
$actor->public_key = $res['publicKey']['publicKeyPem'];
$actor->save();
return $this->verifySignature($headers, $profile, $payload);
}
}

@ -5,9 +5,9 @@ namespace App\Jobs\InboxPipeline;
use Cache;
use App\Profile;
use App\Util\ActivityPub\{
Helpers,
HttpSignature,
Inbox
Helpers,
HttpSignature,
Inbox
};
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
@ -17,198 +17,183 @@ use Illuminate\Queue\SerializesModels;
use App\Jobs\DeletePipeline\DeleteRemoteProfilePipeline;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Log;
class InboxWorker implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $headers;
protected $payload;
public $timeout = 300;
public $tries = 1;
public $maxExceptions = 1;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($headers, $payload)
{
$this->headers = $headers;
$this->payload = $payload;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$profile = null;
$headers = $this->headers;
$payload = $this->payload;
// Verify headers exist
if (!$headers) {
Log::info("InboxWorker: Headers not provided, skipping job");
return;
}
// Verify payload exists
if (!$payload) {
Log::info("InboxWorker: Payload not provided, skipping job");
return;
}
if(!$headers || !$payload || !isset($headers['signature']) || !isset($headers['date'])) {
Log::info("InboxWorker: Invalid headers or payload structure, skipping job");
return;
}
$payload = json_decode($payload, true, 8);
if(isset($payload['id'])) {
$lockKey = 'pf:ap:user-inbox:activity:' . hash('sha256', $payload['id']);
if(Cache::get($lockKey) !== null) {
// Job processed already
return 1;
}
Cache::put($lockKey, 1, 3600);
}
if($this->verifySignature($headers, $payload) == true) {
ActivityHandler::dispatch($headers, $profile, $payload)->onQueue('shared');
return;
} else {
return;
}
}
protected function verifySignature($headers, $payload)
{
$body = $this->payload;
$bodyDecoded = $payload;
$signature = is_array($headers['signature']) ? $headers['signature'][0] : $headers['signature'];
$date = is_array($headers['date']) ? $headers['date'][0] : $headers['date'];
if(!$signature) {
return false;
}
if(!$date) {
return false;
}
if(!now()->parse($date)->gt(now()->subDays(1)) ||
!now()->parse($date)->lt(now()->addDays(1))
) {
return false;
}
if(!isset($bodyDecoded['id'])) {
return false;
}
$signatureData = HttpSignature::parseSignatureHeader($signature);
if(!isset($signatureData['keyId'], $signatureData['signature'], $signatureData['headers']) || isset($signatureData['error'])) {
return false;
}
$keyId = Helpers::validateUrl($signatureData['keyId']);
$id = Helpers::validateUrl($bodyDecoded['id']);
$keyDomain = parse_url($keyId, PHP_URL_HOST);
$idDomain = parse_url($id, PHP_URL_HOST);
if(isset($bodyDecoded['object'])
&& is_array($bodyDecoded['object'])
&& isset($bodyDecoded['object']['attributedTo'])
) {
$attr = Helpers::pluckval($bodyDecoded['object']['attributedTo']);
if(is_array($attr)) {
if(isset($attr['id'])) {
$attr = $attr['id'];
} else {
$attr = "";
}
}
if(parse_url($attr, PHP_URL_HOST) !== $keyDomain) {
return false;
}
}
if(!$keyDomain || !$idDomain || $keyDomain !== $idDomain) {
return false;
}
$actor = Profile::whereKeyId($keyId)->first();
if(!$actor) {
$actorUrl = Helpers::pluckval($bodyDecoded['actor']);
$actor = Helpers::profileFirstOrNew($actorUrl);
}
if(!$actor) {
return false;
}
$pkey = openssl_pkey_get_public($actor->public_key);
if(!$pkey) {
return false;
}
$inboxPath = "/f/inbox";
list($verified, $headers) = HttpSignature::verify($pkey, $signatureData, $headers, $inboxPath, $body);
if($verified == 1) {
return true;
} else {
return false;
}
}
protected function blindKeyRotation($headers, $payload)
{
$signature = is_array($headers['signature']) ? $headers['signature'][0] : $headers['signature'];
$date = is_array($headers['date']) ? $headers['date'][0] : $headers['date'];
if(!$signature) {
return;
}
if(!$date) {
return;
}
if(!now()->parse($date)->gt(now()->subDays(1)) ||
!now()->parse($date)->lt(now()->addDays(1))
) {
return;
}
$signatureData = HttpSignature::parseSignatureHeader($signature);
if(!isset($signatureData['keyId'], $signatureData['signature'], $signatureData['headers']) || isset($signatureData['error'])) {
return;
}
$keyId = Helpers::validateUrl($signatureData['keyId']);
$actor = Profile::whereKeyId($keyId)->whereNotNull('remote_url')->first();
if(!$actor) {
return;
}
if(Helpers::validateUrl($actor->remote_url) == false) {
return;
}
try {
$res = Http::withOptions(['allow_redirects' => false])->timeout(20)->withHeaders([
'Accept' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
'User-Agent' => 'PixelfedBot v0.1 - https://pixelfed.org',
])->get($actor->remote_url);
} catch (ConnectionException $e) {
return false;
}
if(!$res->ok()) {
return false;
}
$res = json_decode($res->body(), true, 8);
if(!$res || !isset($res['publicKey']) || !isset($res['publicKey']['id'])) {
return;
}
if($res['publicKey']['id'] !== $actor->key_id) {
return;
}
$actor->public_key = $res['publicKey']['publicKeyPem'];
$actor->save();
return $this->verifySignature($headers, $payload);
}
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $headers;
protected $payload;
public $timeout = 300;
public $tries = 1;
public $maxExceptions = 1;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($headers, $payload)
{
$this->headers = $headers;
$this->payload = $payload;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$profile = null;
$headers = $this->headers;
if(empty($headers) || empty($this->payload) || !isset($headers['signature']) || !isset($headers['date'])) {
return;
}
$payload = json_decode($this->payload, true, 8);
if(isset($payload['id'])) {
$lockKey = 'pf:ap:user-inbox:activity:' . hash('sha256', $payload['id']);
if(Cache::get($lockKey) !== null) {
// Job processed already
return 1;
}
Cache::put($lockKey, 1, 3600);
}
if($this->verifySignature($headers, $payload) == true) {
ActivityHandler::dispatch($headers, $profile, $payload)->onQueue('shared');
return;
} else {
return;
}
}
protected function verifySignature($headers, $payload)
{
$body = $this->payload;
$bodyDecoded = $payload;
$signature = is_array($headers['signature']) ? $headers['signature'][0] : $headers['signature'];
$date = is_array($headers['date']) ? $headers['date'][0] : $headers['date'];
if(!$signature) {
return false;
}
if(!$date) {
return false;
}
if(!now()->parse($date)->gt(now()->subDays(1)) ||
!now()->parse($date)->lt(now()->addDays(1))
) {
return false;
}
if(!isset($bodyDecoded['id'])) {
return false;
}
$signatureData = HttpSignature::parseSignatureHeader($signature);
if(!isset($signatureData['keyId'], $signatureData['signature'], $signatureData['headers']) || isset($signatureData['error'])) {
return false;
}
$keyId = Helpers::validateUrl($signatureData['keyId']);
$id = Helpers::validateUrl($bodyDecoded['id']);
$keyDomain = parse_url($keyId, PHP_URL_HOST);
$idDomain = parse_url($id, PHP_URL_HOST);
if(isset($bodyDecoded['object'])
&& is_array($bodyDecoded['object'])
&& isset($bodyDecoded['object']['attributedTo'])
) {
$attr = Helpers::pluckval($bodyDecoded['object']['attributedTo']);
if(is_array($attr)) {
if(isset($attr['id'])) {
$attr = $attr['id'];
} else {
$attr = "";
}
}
if(parse_url($attr, PHP_URL_HOST) !== $keyDomain) {
return false;
}
}
if(!$keyDomain || !$idDomain || $keyDomain !== $idDomain) {
return false;
}
$actor = Profile::whereKeyId($keyId)->first();
if(!$actor) {
$actorUrl = Helpers::pluckval($bodyDecoded['actor']);
$actor = Helpers::profileFirstOrNew($actorUrl);
}
if(!$actor) {
return false;
}
$pkey = openssl_pkey_get_public($actor->public_key);
if(!$pkey) {
return false;
}
$inboxPath = "/f/inbox";
list($verified, $headers) = HttpSignature::verify($pkey, $signatureData, $headers, $inboxPath, $body);
if($verified == 1) {
return true;
} else {
return false;
}
}
protected function blindKeyRotation($headers, $payload)
{
$signature = is_array($headers['signature']) ? $headers['signature'][0] : $headers['signature'];
$date = is_array($headers['date']) ? $headers['date'][0] : $headers['date'];
if(!$signature) {
return;
}
if(!$date) {
return;
}
if(!now()->parse($date)->gt(now()->subDays(1)) ||
!now()->parse($date)->lt(now()->addDays(1))
) {
return;
}
$signatureData = HttpSignature::parseSignatureHeader($signature);
if(!isset($signatureData['keyId'], $signatureData['signature'], $signatureData['headers']) || isset($signatureData['error'])) {
return;
}
$keyId = Helpers::validateUrl($signatureData['keyId']);
$actor = Profile::whereKeyId($keyId)->whereNotNull('remote_url')->first();
if(!$actor) {
return;
}
if(Helpers::validateUrl($actor->remote_url) == false) {
return;
}
try {
$res = Http::withOptions(['allow_redirects' => false])->timeout(20)->withHeaders([
'Accept' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
'User-Agent' => 'PixelfedBot v0.1 - https://pixelfed.org',
])->get($actor->remote_url);
} catch (ConnectionException $e) {
return false;
}
if(!$res->ok()) {
return false;
}
$res = json_decode($res->body(), true, 8);
if(!$res || empty($res) || !isset($res['publicKey']) || !isset($res['publicKey']['id'])) {
return;
}
if($res['publicKey']['id'] !== $actor->key_id) {
return;
}
$actor->public_key = $res['publicKey']['publicKeyPem'];
$actor->save();
return $this->verifySignature($headers, $payload);
}
}

@ -182,7 +182,7 @@ class Helpers
return $uri->toString();
} catch (UriException|\Exception $e) {
} catch (UriException $e) {
return false;
}
}
@ -302,7 +302,7 @@ class Helpers
$uri = Uri::new($url);
$host = $uri->getHost();
if (! $host) {
if (! $host || empty($host)) {
return false;
}
@ -338,7 +338,7 @@ class Helpers
return Cache::remember($key, $ttl, function () use ($url) {
$res = ActivityPubFetchService::get($url);
if (! $res) {
if (! $res || empty($res)) {
return false;
}
$res = json_decode($res, true, 8);
@ -477,6 +477,7 @@ class Helpers
public static function isValidStatusData(?array $res): bool
{
return $res &&
! empty($res) &&
! isset($res['error']) &&
isset($res['@context']) &&
isset($res['published']);

Loading…
Cancel
Save