| @@ -1,15 +1,15 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Server REST-Class file. | |
| 4 | - * | |
| 5 | - * @package Activitypub | |
| 6 | - */ | |
| 2 | +namespace Activitypub\Rest; | |
| 7 | 3 | |
| 8 | -namespace Activitypub\Rest; | |
| 4 | +use stdClass; | |
| 5 | +use WP_Error; | |
| 6 | +use WP_REST_Response; | |
| 7 | +use Activitypub\Signature; | |
| 8 | +use Activitypub\Model\Application; | |
| 9 | 9 | |
| 10 | 10 | /** |
| 11 | - * ActivityPub Server REST-Class. | |
| 11 | + * ActivityPub Server REST-Class | |
| 12 | 12 | * |
| 13 | 13 | * @author Django Doucet |
| 14 | 14 | * |
| 15 | 15 | * @see https://www.w3.org/TR/activitypub/#security-verification |
| @@ -15,60 +15,117 @@ | ||
| 15 | 15 | * @see https://www.w3.org/TR/activitypub/#security-verification |
| 16 | 16 | */ |
| 17 | 17 | class Server { |
| 18 | 18 | /** |
| 19 | - * Initialize the class, registering WordPress hooks. | |
| 19 | + * Initialize the class, registering WordPress hooks | |
| 20 | 20 | */ |
| 21 | 21 | public static function init() { |
| 22 | - \add_filter( 'rest_request_before_callbacks', array( self::class, 'validate_requests' ), 9, 3 ); | |
| 23 | - \add_filter( 'rest_request_parameter_order', array( self::class, 'request_parameter_order' ), 10, 2 ); | |
| 22 | + self::register_routes(); | |
| 24 | 23 | |
| 25 | - \add_filter( 'rest_post_dispatch', array( self::class, 'filter_output' ), 10, 3 ); | |
| 26 | - \add_filter( 'rest_post_dispatch', array( self::class, 'add_cors_headers' ), 10, 3 ); | |
| 24 | + \add_filter( 'rest_request_before_callbacks', array( self::class, 'validate_activitypub_requests' ), 9, 3 ); | |
| 25 | + \add_filter( 'rest_request_before_callbacks', array( self::class, 'authorize_activitypub_requests' ), 10, 3 ); | |
| 27 | 26 | } |
| 28 | 27 | |
| 29 | 28 | /** |
| 30 | - * Callback function to validate incoming ActivityPub requests | |
| 29 | + * Register routes | |
| 30 | + */ | |
| 31 | + public static function register_routes() { | |
| 32 | + \register_rest_route( | |
| 33 | + ACTIVITYPUB_REST_NAMESPACE, | |
| 34 | + '/application', | |
| 35 | + array( | |
| 36 | + array( | |
| 37 | + 'methods' => \WP_REST_Server::READABLE, | |
| 38 | + 'callback' => array( self::class, 'application_actor' ), | |
| 39 | + 'permission_callback' => '__return_true', | |
| 40 | + ), | |
| 41 | + ) | |
| 42 | + ); | |
| 43 | + } | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * Render Application actor profile | |
| 31 | 47 | * |
| 32 | - * @param \WP_REST_Response|\WP_HTTP_Response|\WP_Error|mixed $response Result to send to the client. | |
| 33 | - * Usually a WP_REST_Response or WP_Error. | |
| 34 | - * @param array $handler Route handler used for the request. | |
| 35 | - * @param \WP_REST_Request $request Request used to generate the response. | |
| 48 | + * @return WP_REST_Response The JSON profile of the Application Actor. | |
| 49 | + */ | |
| 50 | + public static function application_actor() { | |
| 51 | + $user = new Application(); | |
| 52 | + | |
| 53 | + $json = $user->to_array(); | |
| 54 | + | |
| 55 | + $rest_response = new WP_REST_Response( $json, 200 ); | |
| 56 | + $rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) ); | |
| 57 | + | |
| 58 | + return $rest_response; | |
| 59 | + } | |
| 60 | + | |
| 61 | + /** | |
| 62 | + * Callback function to authorize each api requests | |
| 36 | 63 | * |
| 37 | - * @return mixed|\WP_Error The response, error, or modified response. | |
| 64 | + * @see WP_REST_Request | |
| 65 | + * | |
| 66 | + * @see https://www.w3.org/wiki/SocialCG/ActivityPub/Primer/Authentication_Authorization#Authorized_fetch | |
| 67 | + * @see https://swicg.github.io/activitypub-http-signature/#authorized-fetch | |
| 68 | + * | |
| 69 | + * @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response Result to send to the client. | |
| 70 | + * Usually a WP_REST_Response or WP_Error. | |
| 71 | + * @param array $handler Route handler used for the request. | |
| 72 | + * @param WP_REST_Request $request Request used to generate the response. | |
| 73 | + * | |
| 74 | + * @return mixed|WP_Error The response, error, or modified response. | |
| 38 | 75 | */ |
| 39 | - public static function validate_requests( $response, $handler, $request ) { | |
| 76 | + public static function authorize_activitypub_requests( $response, $handler, $request ) { | |
| 40 | 77 | if ( 'HEAD' === $request->get_method() ) { |
| 41 | 78 | return $response; |
| 42 | 79 | } |
| 43 | 80 | |
| 81 | + if ( \is_wp_error( $response ) ) { | |
| 82 | + return $response; | |
| 83 | + } | |
| 84 | + | |
| 44 | 85 | $route = $request->get_route(); |
| 45 | 86 | |
| 87 | + // check if it is an activitypub request and exclude webfinger and nodeinfo endpoints | |
| 46 | 88 | if ( |
| 47 | - \is_wp_error( $response ) || | |
| 48 | - ! \str_starts_with( $route, '/' . ACTIVITYPUB_REST_NAMESPACE ) | |
| 89 | + ! \str_starts_with( $route, '/' . ACTIVITYPUB_REST_NAMESPACE ) || | |
| 90 | + \str_starts_with( $route, '/' . \trailingslashit( ACTIVITYPUB_REST_NAMESPACE ) . 'webfinger' ) || | |
| 91 | + \str_starts_with( $route, '/' . \trailingslashit( ACTIVITYPUB_REST_NAMESPACE ) . 'nodeinfo' ) || | |
| 92 | + \str_starts_with( $route, '/' . \trailingslashit( ACTIVITYPUB_REST_NAMESPACE ) . 'application' ) | |
| 49 | 93 | ) { |
| 50 | 94 | return $response; |
| 51 | 95 | } |
| 52 | 96 | |
| 53 | - $params = $request->get_json_params(); | |
| 97 | + /** | |
| 98 | + * Filter to defer signature verification | |
| 99 | + * | |
| 100 | + * Skip signature verification for debugging purposes or to reduce load for | |
| 101 | + * certain Activity-Types, like "Delete". | |
| 102 | + * | |
| 103 | + * @param bool $defer Whether to defer signature verification. | |
| 104 | + * @param WP_REST_Request $request The request used to generate the response. | |
| 105 | + * | |
| 106 | + * @return bool Whether to defer signature verification. | |
| 107 | + */ | |
| 108 | + $defer = \apply_filters( 'activitypub_defer_signature_verification', false, $request ); | |
| 54 | 109 | |
| 55 | - // Type is required for ActivityPub requests, so it fail later in the process. | |
| 56 | - if ( ! isset( $params['type'] ) ) { | |
| 110 | + if ( $defer ) { | |
| 57 | 111 | return $response; |
| 58 | 112 | } |
| 59 | 113 | |
| 60 | 114 | if ( |
| 61 | - ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS && | |
| 62 | - in_array( $params['type'], array( 'Create', 'Like', 'Announce' ), true ) | |
| 115 | + // POST-Requests are always signed | |
| 116 | + 'GET' !== $request->get_method() || | |
| 117 | + // GET-Requests only require a signature in secure mode | |
| 118 | + ( 'GET' === $request->get_method() && ACTIVITYPUB_AUTHORIZED_FETCH ) | |
| 63 | 119 | ) { |
| 64 | - return new \WP_Error( | |
| 65 | - 'activitypub_server_does_not_accept_incoming_interactions', | |
| 66 | - \__( 'This server does not accept incoming interactions.', 'activitypub' ), | |
| 67 | - // We have to use a 2XX status code here, because otherwise the response will be | |
| 68 | - // treated as an error and Mastodon might block this WordPress instance. | |
| 69 | - array( 'status' => 202 ) | |
| 70 | - ); | |
| 120 | + $verified_request = Signature::verify_http_signature( $request ); | |
| 121 | + if ( \is_wp_error( $verified_request ) ) { | |
| 122 | + return new WP_Error( | |
| 123 | + 'activitypub_signature_verification', | |
| 124 | + $verified_request->get_error_message(), | |
| 125 | + array( 'status' => 401 ) | |
| 126 | + ); | |
| 127 | + } | |
| 71 | 128 | } |
| 72 | 129 | |
| 73 | 130 | return $response; |
| 74 | 131 | } |
| @@ -73,136 +130,50 @@ | ||
| 73 | 130 | return $response; |
| 74 | 131 | } |
| 75 | 132 | |
| 76 | 133 | /** |
| 77 | - * Modify the parameter priority order for a REST API request. | |
| 134 | + * Callback function to validate incoming ActivityPub requests | |
| 78 | 135 | * |
| 79 | - * @param string[] $order Array of types to check, in order of priority. | |
| 80 | - * @param \WP_REST_Request $request The request object. | |
| 136 | + * @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response Result to send to the client. | |
| 137 | + * Usually a WP_REST_Response or WP_Error. | |
| 138 | + * @param array $handler Route handler used for the request. | |
| 139 | + * @param WP_REST_Request $request Request used to generate the response. | |
| 81 | 140 | * |
| 82 | - * @return string[] The modified order of types to check. | |
| 141 | + * @return mixed|WP_Error The response, error, or modified response. | |
| 83 | 142 | */ |
| 84 | - public static function request_parameter_order( $order, $request ) { | |
| 85 | - $route = $request->get_route(); | |
| 86 | - | |
| 87 | - // Check if it is an activitypub request and exclude webfinger and nodeinfo endpoints. | |
| 88 | - if ( ! \str_starts_with( $route, '/' . ACTIVITYPUB_REST_NAMESPACE ) ) { | |
| 89 | - return $order; | |
| 143 | + public static function validate_activitypub_requests( $response, $handler, $request ) { | |
| 144 | + if ( 'HEAD' === $request->get_method() ) { | |
| 145 | + return $response; | |
| 90 | 146 | } |
| 91 | 147 | |
| 92 | - $method = $request->get_method(); | |
| 93 | - | |
| 94 | - if ( \WP_REST_Server::CREATABLE !== $method ) { | |
| 95 | - return $order; | |
| 96 | - } | |
| 97 | - | |
| 98 | - return array( | |
| 99 | - 'JSON', | |
| 100 | - 'POST', | |
| 101 | - 'URL', | |
| 102 | - 'defaults', | |
| 103 | - ); | |
| 104 | - } | |
| 105 | - | |
| 106 | - /** | |
| 107 | - * Filters the REST API response to properly handle the ActivityPub error formatting. | |
| 108 | - * | |
| 109 | - * @see https://codeberg.org/fediverse/fep/src/branch/main/fep/c180/fep-c180.md | |
| 110 | - * | |
| 111 | - * @param \WP_HTTP_Response $response Result to send to the client. Usually a `WP_REST_Response`. | |
| 112 | - * @param \WP_REST_Server $server Server instance. | |
| 113 | - * @param \WP_REST_Request $request Request used to generate the response. | |
| 114 | - * | |
| 115 | - * @return \WP_HTTP_Response The filtered response. | |
| 116 | - */ | |
| 117 | - public static function filter_output( $response, $server, $request ) { | |
| 118 | 148 | $route = $request->get_route(); |
| 119 | 149 | |
| 120 | - // Check if it is an activitypub request and exclude webfinger and nodeinfo endpoints. | |
| 121 | - if ( ! \str_starts_with( $route, '/' . ACTIVITYPUB_REST_NAMESPACE ) ) { | |
| 150 | + if ( | |
| 151 | + \is_wp_error( $response ) || | |
| 152 | + ! \str_starts_with( $route, '/' . ACTIVITYPUB_REST_NAMESPACE ) | |
| 153 | + ) { | |
| 122 | 154 | return $response; |
| 123 | 155 | } |
| 124 | 156 | |
| 125 | - // Exclude OAuth endpoints - they have their own error format per RFC 6749. | |
| 126 | - if ( \str_starts_with( $route, '/' . ACTIVITYPUB_REST_NAMESPACE . '/oauth' ) ) { | |
| 127 | - return $response; | |
| 128 | - } | |
| 157 | + $params = $request->get_json_params(); | |
| 129 | 158 | |
| 130 | - // Only alter responses that return an error status code. | |
| 131 | - if ( $response->get_status() < 400 ) { | |
| 159 | + // Type is required for ActivityPub requests, so it fail later in the process | |
| 160 | + if ( ! isset( $params['type'] ) ) { | |
| 132 | 161 | return $response; |
| 133 | 162 | } |
| 134 | 163 | |
| 135 | - $data = $response->get_data(); | |
| 136 | - | |
| 137 | - // Ensure that `$data` was already converted to a response. | |
| 138 | - if ( \is_wp_error( $data ) ) { | |
| 139 | - $response = \rest_convert_error_to_response( $data ); | |
| 140 | - $data = $response->get_data(); | |
| 164 | + if ( | |
| 165 | + ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS && | |
| 166 | + in_array( $params['type'], array( 'Create', 'Like', 'Announce' ), true ) | |
| 167 | + ) { | |
| 168 | + return new WP_Error( | |
| 169 | + 'activitypub_server_does_not_accept_incoming_interactions', | |
| 170 | + \__( 'This server does not accept incoming interactions.', 'activitypub' ), | |
| 171 | + // We have to use a 2XX status code here, because otherwise the response will be | |
| 172 | + // treated as an error and Mastodon might block this WordPress instance. | |
| 173 | + array( 'status' => 202 ) | |
| 174 | + ); | |
| 141 | 175 | } |
| 142 | 176 | |
| 143 | - $error = array( | |
| 144 | - 'type' => 'about:blank', | |
| 145 | - 'title' => $data['code'] ?? '', | |
| 146 | - 'detail' => $data['message'] ?? '', | |
| 147 | - 'status' => $response->get_status(), | |
| 148 | - | |
| 149 | - /* | |
| 150 | - * Provides the unstructured error data. | |
| 151 | - * | |
| 152 | - * @see https://nodeinfo.diaspora.software/schema.html#metadata. | |
| 153 | - */ | |
| 154 | - 'metadata' => $data, | |
| 155 | - ); | |
| 156 | - | |
| 157 | - $response->set_data( $error ); | |
| 158 | - | |
| 159 | 177 | return $response; |
| 160 | - } | |
| 161 | - | |
| 162 | - /** | |
| 163 | - * Add CORS headers to ActivityPub REST responses. | |
| 164 | - * | |
| 165 | - * @param \WP_REST_Response $response The REST response. | |
| 166 | - * @param \WP_REST_Server $server The REST server instance. | |
| 167 | - * @param \WP_REST_Request $request The request object. | |
| 168 | - * | |
| 169 | - * @return \WP_REST_Response The modified response. | |
| 170 | - */ | |
| 171 | - public static function add_cors_headers( $response, $server, $request ) { | |
| 172 | - $route = $request->get_route(); | |
| 173 | - $namespace = '/' . ACTIVITYPUB_REST_NAMESPACE; | |
| 174 | - | |
| 175 | - // Only add CORS to ActivityPub endpoints, except the interactive OAuth authorize endpoint. | |
| 176 | - if ( ! \str_starts_with( $route, $namespace ) || \str_starts_with( $route, $namespace . '/oauth/authorize' ) ) { | |
| 177 | - return $response; | |
| 178 | - } | |
| 179 | - | |
| 180 | - /* | |
| 181 | - * ActivityPub data is meant to be publicly readable by federation peers | |
| 182 | - * and browser-side clients. We do not enable credentialed cross-origin | |
| 183 | - * access: cookie auth would still be rejected by WordPress core's | |
| 184 | - * REST nonce check, and OAuth Bearer tokens travel in the | |
| 185 | - * Authorization header — which is permitted via Allow-Headers and | |
| 186 | - * does not require Allow-Credentials. | |
| 187 | - */ | |
| 188 | - $response->header( 'Access-Control-Allow-Origin', '*' ); | |
| 189 | - $response->header( 'Access-Control-Allow-Methods', 'GET, POST, OPTIONS' ); | |
| 190 | - $response->header( 'Access-Control-Allow-Headers', 'Accept, Content-Type, Authorization, Last-Event-ID' ); | |
| 191 | - | |
| 192 | - return $response; | |
| 193 | - } | |
| 194 | - | |
| 195 | - /** | |
| 196 | - * Send CORS headers directly via header(). | |
| 197 | - * | |
| 198 | - * Use this for endpoints that bypass the REST response flow | |
| 199 | - * (e.g. SSE streams that call exit() instead of returning a WP_REST_Response). | |
| 200 | - * | |
| 201 | - * @since 8.1.0 | |
| 202 | - */ | |
| 203 | - public static function send_cors_headers() { | |
| 204 | - \header( 'Access-Control-Allow-Origin: *' ); | |
| 205 | - \header( 'Access-Control-Allow-Methods: GET, POST, OPTIONS' ); | |
| 206 | - \header( 'Access-Control-Allow-Headers: Accept, Content-Type, Authorization, Last-Event-ID' ); | |
| 207 | 178 | } |
| 208 | 179 | } |