| @@ -11,11 +11,10 @@ | ||
| 11 | 11 | namespace Activitypub\Rest; |
| 12 | 12 | |
| 13 | 13 | use Activitypub\Collection\Remote_Actors; |
| 14 | 14 | use Activitypub\Http; |
| 15 | +use Activitypub\Webfinger; | |
| 15 | 16 | |
| 16 | -use function Activitypub\is_actor; | |
| 17 | - | |
| 18 | 17 | /** |
| 19 | 18 | * Proxy Controller. |
| 20 | 19 | * |
| 21 | 20 | * Provides a bridge between C2S OAuth authentication and S2S HTTP Signature authentication. |
| @@ -52,9 +51,9 @@ | ||
| 52 | 51 | 'callback' => array( $this, 'create_item' ), |
| 53 | 52 | 'permission_callback' => array( $this, 'verify_authentication' ), |
| 54 | 53 | 'args' => array( |
| 55 | 54 | 'id' => array( |
| 56 | - 'description' => 'The URI of the remote ActivityPub object to fetch.', | |
| 55 | + 'description' => 'The remote ActivityPub object to fetch: an HTTPS URL or an acct identifier (`user@host`, `@user@host`, or `acct:user@host`).', | |
| 57 | 56 | 'type' => 'string', |
| 58 | 57 | 'required' => true, |
| 59 | 58 | 'sanitize_callback' => array( $this, 'sanitize_url' ), |
| 60 | 59 | 'validate_callback' => array( $this, 'validate_url' ), |
| @@ -74,11 +73,10 @@ | ||
| 74 | 73 | 'callback' => array( $this, 'get_stream' ), |
| 75 | 74 | 'permission_callback' => array( $this, 'get_stream_permissions_check' ), |
| 76 | 75 | 'args' => array( |
| 77 | 76 | 'id' => array( |
| 78 | - 'description' => 'The remote object ID (URI) whose eventStream to proxy.', | |
| 77 | + 'description' => 'The remote actor identifier (URL or WebFinger acct) whose eventStream to proxy.', | |
| 79 | 78 | 'type' => 'string', |
| 80 | - 'format' => 'uri', | |
| 81 | 79 | 'required' => true, |
| 82 | 80 | 'sanitize_callback' => array( $this, 'sanitize_url' ), |
| 83 | 81 | 'validate_callback' => array( $this, 'validate_url' ), |
| 84 | 82 | ), |
| @@ -88,33 +86,51 @@ | ||
| 88 | 86 | ); |
| 89 | 87 | } |
| 90 | 88 | |
| 91 | 89 | /** |
| 92 | - * Sanitizes the URL parameter. | |
| 90 | + * Sanitize the `id` parameter. | |
| 93 | 91 | * |
| 92 | + * Accepts either an HTTPS URL or an acct identifier (`user@host`, | |
| 93 | + * `@user@host`, or `acct:user@host`). Acct identifiers are returned | |
| 94 | + * as-is; URLs are run through `sanitize_url()`. Matches the dual-shape | |
| 95 | + * contract of `Remote_Actors::fetch_by_various()`. | |
| 96 | + * | |
| 94 | 97 | * @see https://developer.wordpress.org/reference/functions/sanitize_url/ |
| 95 | 98 | * |
| 96 | - * @param string $url The urlencoded URL to sanitize. | |
| 97 | - * @return string The sanitized URL. | |
| 99 | + * @param string $url The urlencoded URL or acct identifier to sanitize. | |
| 100 | + * @return string The sanitized value. | |
| 98 | 101 | */ |
| 99 | 102 | public function sanitize_url( $url ) { |
| 100 | - // Decode and sanitize the URL. | |
| 101 | - return sanitize_url( urldecode( $url ) ); | |
| 103 | + $decoded = \urldecode( $url ); | |
| 104 | + | |
| 105 | + if ( Webfinger::is_acct( $decoded ) ) { | |
| 106 | + return $decoded; | |
| 107 | + } | |
| 108 | + | |
| 109 | + return \sanitize_url( $decoded ); | |
| 102 | 110 | } |
| 111 | + | |
| 103 | 112 | /** |
| 104 | - * Validate the URL parameter. | |
| 113 | + * Validate the `id` parameter. | |
| 105 | 114 | * |
| 106 | - * Uses wp_http_validate_url() which blocks local/private IPs and restricts ports. | |
| 115 | + * Accepts either an HTTPS URL (validated via `wp_http_validate_url()`, | |
| 116 | + * which blocks local/private IPs and restricts ports) or an acct | |
| 117 | + * identifier in any of the forms accepted by `Webfinger::is_acct()`: | |
| 118 | + * `user@host`, `@user@host`, or `acct:user@host`. Matches the | |
| 119 | + * dual-shape contract of `Remote_Actors::fetch_by_various()`. | |
| 107 | 120 | * |
| 108 | 121 | * @see https://developer.wordpress.org/reference/functions/wp_http_validate_url/ |
| 109 | 122 | * |
| 110 | - * @param string $url The URL to validate. | |
| 123 | + * @param string $url The URL or acct identifier to validate. | |
| 111 | 124 | * @return bool True if valid, false otherwise. |
| 112 | 125 | */ |
| 113 | 126 | public function validate_url( $url ) { |
| 114 | - // Decode the url. | |
| 115 | - $decoded_url = urldecode( $url ); | |
| 127 | + $decoded_url = \urldecode( $url ); | |
| 116 | 128 | |
| 129 | + if ( Webfinger::is_acct( $decoded_url ) ) { | |
| 130 | + return true; | |
| 131 | + } | |
| 132 | + | |
| 117 | 133 | // Must be HTTPS. |
| 118 | 134 | if ( 'https' !== \wp_parse_url( $decoded_url, PHP_URL_SCHEME ) ) { |
| 119 | 135 | return false; |
| 120 | 136 | } |
| @@ -162,9 +178,12 @@ | ||
| 162 | 178 | return $response; |
| 163 | 179 | } |
| 164 | 180 | } |
| 165 | 181 | |
| 166 | - // Fall back to fetching as a generic object. | |
| 182 | + /* | |
| 183 | + * Fall back to fetching as a generic object. Actors are already resolved and | |
| 184 | + * cached above via fetch_by_various(), so this path only proxies the object. | |
| 185 | + */ | |
| 167 | 186 | $object = Http::get_remote_object( $url ); |
| 168 | 187 | |
| 169 | 188 | if ( \is_wp_error( $object ) ) { |
| 170 | 189 | return new \WP_Error( |
| @@ -171,13 +190,8 @@ | ||
| 171 | 190 | 'activitypub_fetch_failed', |
| 172 | 191 | \__( 'Failed to fetch the remote object.', 'activitypub' ), |
| 173 | 192 | array( 'status' => 502 ) |
| 174 | 193 | ); |
| 175 | - } | |
| 176 | - | |
| 177 | - // If it's an actor, store it for future use. | |
| 178 | - if ( is_actor( $object ) ) { | |
| 179 | - Remote_Actors::upsert( $object ); | |
| 180 | 194 | } |
| 181 | 195 | |
| 182 | 196 | $response = new \WP_REST_Response( $object, 200 ); |
| 183 | 197 | $response->header( 'Content-Type', 'application/activity+json; charset=' . \get_option( 'blog_charset' ) ); |