Skip to content

Commit 356823b

Browse files
pfefferleobenland
andauthored
Added setting to enable/disable Authorized-Fetch (#1017)
* Added setting to enable/disable Authorized-Fetch Fixes #726 * update changelog * follow the spec https://keepachangelog.com/en/1.1.0/ * hide if const is set props @obenland * Update templates/settings.php Co-authored-by: Konstantin Obenland <[email protected]> * Update templates/settings.php Co-authored-by: Konstantin Obenland <[email protected]> --------- Co-authored-by: Konstantin Obenland <[email protected]>
1 parent 893d06c commit 356823b

File tree

8 files changed

+74
-4
lines changed

8 files changed

+74
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
* Setting to enable/disable Authorized-Fetch
13+
1014
### Improved
1115

1216
* Added screen reader text to the "Follow Me" block for improved accessibility

includes/class-activitypub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public static function render_activitypub_template( $template ) {
119119
* @see https://www.w3.org/wiki/SocialCG/ActivityPub/Primer/Authentication_Authorization#Authorized_fetch
120120
* @see https://swicg.github.io/activitypub-http-signature/#authorized-fetch
121121
*/
122-
if ( $activitypub_template && ACTIVITYPUB_AUTHORIZED_FETCH ) {
122+
if ( $activitypub_template && use_authorized_fetch() ) {
123123
$verification = Signature::verify_http_signature( $_SERVER );
124124
if ( \is_wp_error( $verification ) ) {
125125
header( 'HTTP/1.1 401 Unauthorized' );

includes/class-admin.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,16 @@ public static function register_settings() {
301301
)
302302
);
303303

304+
\register_setting(
305+
'activitypub',
306+
'activitypub_authorized_fetch',
307+
array(
308+
'type' => 'boolean',
309+
'description' => \__( 'Require HTTP signature authentication.', 'activitypub' ),
310+
'default' => false,
311+
)
312+
);
313+
304314
// Blog-User Settings.
305315
\register_setting(
306316
'activitypub_blog',

includes/constants.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
\defined( 'ACTIVITYPUB_USERNAME_REGEXP' ) || \define( 'ACTIVITYPUB_USERNAME_REGEXP', '(?:([A-Za-z0-9\._-]+)@((?:[A-Za-z0-9_-]+\.)+[A-Za-z]+))' );
1717
\defined( 'ACTIVITYPUB_URL_REGEXP' ) || \define( 'ACTIVITYPUB_URL_REGEXP', '(https?:|www\.)\S+[\w\/]' );
1818
\defined( 'ACTIVITYPUB_CUSTOM_POST_CONTENT' ) || \define( 'ACTIVITYPUB_CUSTOM_POST_CONTENT', "[ap_title type=\"html\"]\n\n[ap_content]\n\n[ap_hashtags]" );
19-
\defined( 'ACTIVITYPUB_AUTHORIZED_FETCH' ) || \define( 'ACTIVITYPUB_AUTHORIZED_FETCH', false );
2019
\defined( 'ACTIVITYPUB_DISABLE_REWRITES' ) || \define( 'ACTIVITYPUB_DISABLE_REWRITES', false );
2120
\defined( 'ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS' ) || \define( 'ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS', false );
2221
// Disable reactions like `Like` and `Announce` by default.

includes/functions.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,6 +1503,31 @@ function get_upload_baseurl() {
15031503
return apply_filters( 'activitypub_get_upload_baseurl', $upload_dir['baseurl'] );
15041504
}
15051505

1506+
/**
1507+
* Check if Authorized-Fetch is enabled.
1508+
*
1509+
* @see https://docs.joinmastodon.org/admin/config/#authorized_fetch
1510+
*
1511+
* @return boolean True if Authorized-Fetch is enabled, false otherwise.
1512+
*/
1513+
function use_authorized_fetch() {
1514+
$use = false;
1515+
1516+
// Prefer the constant over the option.
1517+
if ( \defined( 'ACTIVITYPUB_AUTHORIZED_FETCH' ) ) {
1518+
$use = ACTIVITYPUB_AUTHORIZED_FETCH;
1519+
} else {
1520+
$use = (bool) \get_option( 'activitypub_authorized_fetch', '0' );
1521+
}
1522+
1523+
/**
1524+
* Filters whether to use Authorized-Fetch.
1525+
*
1526+
* @param boolean $use_authorized_fetch True if Authorized-Fetch is enabled, false otherwise.
1527+
*/
1528+
return apply_filters( 'activitypub_use_authorized_fetch', $use );
1529+
}
1530+
15061531
/**
15071532
* Check if an ID is from the same domain as the site.
15081533
*

includes/rest/class-server.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Activitypub\Signature;
1414
use Activitypub\Model\Application;
1515

16+
use function Activitypub\use_authorized_fetch;
17+
1618
/**
1719
* ActivityPub Server REST-Class.
1820
*
@@ -128,7 +130,7 @@ public static function authorize_activitypub_requests( $response, $handler, $req
128130
// POST-Requests are always signed.
129131
'GET' !== $request->get_method() ||
130132
// GET-Requests only require a signature in secure mode.
131-
( 'GET' === $request->get_method() && ACTIVITYPUB_AUTHORIZED_FETCH )
133+
( 'GET' === $request->get_method() && use_authorized_fetch() )
132134
) {
133135
$verified_request = Signature::verify_http_signature( $request );
134136
if ( \is_wp_error( $verified_request ) ) {

readme.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ The plugin uses PHP Constants to enable, disable or change its default behaviour
107107
* `ACTIVITYPUB_USERNAME_REGEXP` - Change the default regex to detect @-replies in a text. Default: `(?:([A-Za-z0-9\._-]+)@((?:[A-Za-z0-9_-]+\.)+[A-Za-z]+))`.
108108
* `ACTIVITYPUB_URL_REGEXP` - Change the default regex to detect urls in a text. Default: `(www.|http:|https:)+[^\s]+[\w\/]`.
109109
* `ACTIVITYPUB_CUSTOM_POST_CONTENT` - Change the default template for Activities. Default: `<strong>[ap_title]</strong>\n\n[ap_content]\n\n[ap_hashtags]\n\n[ap_shortlink]`.
110-
* `ACTIVITYPUB_AUTHORIZED_FETCH` - Enable AUTHORIZED_FETCH. Default: `false`.
110+
* `ACTIVITYPUB_AUTHORIZED_FETCH` - Enable AUTHORIZED_FETCH.
111111
* `ACTIVITYPUB_DISABLE_REWRITES` - Disable auto generation of `mod_rewrite` rules. Default: `false`.
112112
* `ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS` - Block incoming replies/comments/likes. Default: `false`.
113113
* `ACTIVITYPUB_DISABLE_OUTGOING_INTERACTIONS` - Disable outgoing replies/comments/likes. Default: `false`.
@@ -134,6 +134,7 @@ For reasons of data protection, it is not possible to see the followers of other
134134

135135
= Unreleased =
136136

137+
* Added: Setting to enable/disable Authorized-Fetch
137138
* Improved: Added screen reader text for the "Follow Me" block for improved accessibility
138139
* Improved: Added `media_type` support to Activity-Object-Transformers
139140
* Improved: Clarified settings page text around which users get Activitypub profiles

templates/settings.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,35 @@
237237
<?php \do_settings_fields( 'activitypub', 'general' ); ?>
238238
<?php \do_settings_fields( 'activitypub', 'server' ); ?>
239239
</div>
240+
<div class="box">
241+
<h3><?php \esc_html_e( 'Security', 'activitypub' ); ?></h3>
242+
<table class="form-table">
243+
<tbody>
244+
<?php if ( ! defined( 'ACTIVITYPUB_AUTHORIZED_FETCH' ) ) : ?>
245+
<tr>
246+
<th scope="row">
247+
<?php \esc_html_e( 'Authorized-Fetch', 'activitypub' ); ?>
248+
</th>
249+
<td>
250+
<p>
251+
<label>
252+
<input type="checkbox" name="activitypub_authorized_fetch" id="activitypub_authorized_fetch" value="1" <?php \checked( '1', \get_option( 'activitypub_authorized_fetch', '0' ) ); ?> />
253+
<?php \esc_html_e( 'Require HTTP signature authentication on ActivityPub representations of public posts and profiles.', 'activitypub' ); ?>
254+
</label>
255+
</p>
256+
<p class="description">
257+
<?php \esc_html_e( '⚠ Secure mode has its limitations, which is why it is not enabled by default. It is not fully supported by all software in the fediverse, and some features may break, especially when interacting with Mastodon servers older than version 3.0. Additionally, since it requires authentication for public content, caching is not possible, leading to higher computational costs.', 'activitypub' ); ?>
258+
</p>
259+
<p class="description">
260+
<?php \esc_html_e( '⚠ Secure mode does not hide the HTML representations of public posts and profiles. While HTML is a less consistant format (that potentially changes often) compared to first-class ActivityPub representations or the REST API, it still poses a potential risk for content scraping.', 'activitypub' ); ?>
261+
</p>
262+
</td>
263+
</tr>
264+
<?php endif; ?>
265+
</tbody>
266+
</table>
267+
<?php \do_settings_fields( 'activitypub', 'security' ); ?>
268+
</div>
240269
<?php \do_settings_sections( 'activitypub' ); ?>
241270

242271
<?php \submit_button(); ?>

0 commit comments

Comments
 (0)