aboutsummaryrefslogtreecommitdiffhomepage
path: root/routes/push.php
blob: 344412cb15ab850b71286be265f929bd15cbc0c3 (plain)
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
<?php

use Digitigrade\HttpResponseStatus\BadRequest;
use Digitigrade\HttpResponseStatus\Forbidden;
use Digitigrade\HttpResponseStatus\Unauthorized;
use Digitigrade\Job\ProcessIncomingPush;
use Digitigrade\Model\Instance;
use Digitigrade\Router;

Router::getInstance()->mount('/push', function (array $args) {
    // receive incoming pushes
    $instance = Instance::findByRequestHeaders();
    if ($instance == null) {
        throw new Unauthorized('please identify yourself with a valid Authorization header!');
    }
    if (!$instance->auth->inboundPushEnabled) {
        throw new Forbidden('you are not permitted to push as i have not subscribed to you');
    }

    $body = file_get_contents('php://input');
    $obj = json_decode($body);
    if ($obj === null) {
        throw new BadRequest("request body doesn't look like valid json");
    }
    if (!isset($obj->type, $obj->self)) {
        throw new BadRequest('the object needs to have `type` and `self` properties');
    }
    if (!str_starts_with($obj->self, 'https://')) {
        throw new BadRequest('dodgy looking `self` uri!');
    }
    if (hostname_from_uri($obj->self) != $instance->domain) {
        throw new Forbidden('you may not push objects belonging to a different instance');
    }
    if (!in_array($obj->type, ['actor', 'note', 'interaction', 'extension', 'tombstone'])) {
        throw new BadRequest('invalid object type!');
    }

    (new ProcessIncomingPush($obj))->submit();
});

Router::getInstance()->mount('/push/simple', function (array $args) {
    // receive incoming simple pushes
    $uri = file_get_contents('php://input');
    if (!str_starts_with($uri, 'https://')) {
        throw new BadRequest('dodgy looking uri!');
    }

    $result = send_request_authenticated($uri);
    if (!str_contains($result->headers[0], '200')) {
        throw new BadRequest('unable to fetch that uri!');
    }
    $obj = json_decode($result->body);

    if ($obj === null) {
        throw new BadRequest("fetched content doesn't look like valid json");
    }
    if (!isset($obj->type, $obj->self)) {
        throw new BadRequest('the object needs to have `type` and `self` properties');
    }
    if ($obj->self != $uri) {
        throw new BadRequest('fetched object has a different uri from the one you gave me');
    }
    if (!in_array($obj->type, ['actor', 'note', 'interaction', 'extension', 'tombstone'])) {
        throw new BadRequest('invalid object type!');
    }

    // only add ratelimiting delay after having validated it, to mitigate the
    // possibility of denial-of-service attacks (still possible though)
    // FIXME: use a proper rate limit system
    sleep(5);
    // and only add the job if the client actually waited for the delay time
    if (!connection_aborted()) {
        // also wait a minute before processing the push
        (new ProcessIncomingPush($obj))->submitDelayed(60);
    }
});