Skip to content

Fix malformed ActivityPub handles for email-based logins #2082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/changelog/2082-from-description
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Fix malformed ActivityPub handles for users with email-based logins (e.g., from Site Kit Google authentication)
9 changes: 8 additions & 1 deletion includes/model/class-user.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,14 @@ public function get_alternate_url() {
* @return string The preferred username.
*/
public function get_preferred_username() {
return \get_the_author_meta( 'login', $this->_id );
$login = \get_the_author_meta( 'login', $this->_id );

// Handle cases where login is an email address (e.g., from Site Kit Google login).
if ( \filter_var( $login, FILTER_VALIDATE_EMAIL ) ) {
$login = \str_replace( array( '@', '.' ), '-', $login );
}

return $login;
}

/**
Expand Down
46 changes: 46 additions & 0 deletions tests/includes/model/class-test-user.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,50 @@ public function test_get_moved_to() {
$this->assertArrayHasKey( 'movedTo', $user );
$this->assertSame( 'https://example.com', $user['movedTo'] );
}

/**
* Test that email-based usernames are properly sanitized for ActivityPub handles.
*
* @covers ::get_preferred_username
* @covers ::get_webfinger
*/
public function test_email_username_sanitization() {
// Test with email-based login (e.g., from Site Kit Google login).
$user_id = self::factory()->user->create(
array(
'role' => 'author',
'user_login' => '[email protected]',
)
);
$user = User::from_wp_user( $user_id );

// Preferred username should be sanitized.
$this->assertSame( 'testuser123-gmail-com', $user->get_preferred_username() );

// Webfinger should not have double @.
$expected_webfinger = 'testuser123-gmail-com@' . \wp_parse_url( \home_url(), \PHP_URL_HOST );
$this->assertSame( $expected_webfinger, $user->get_webfinger() );

// Test another email format.
$user_id2 = self::factory()->user->create(
array(
'role' => 'author',
'user_login' => '[email protected]',
)
);
$user2 = User::from_wp_user( $user_id2 );

$this->assertSame( 'admin-googlemail-com', $user2->get_preferred_username() );

// Test normal username (no email) remains unchanged.
$user_id3 = self::factory()->user->create(
array(
'role' => 'author',
'user_login' => 'normaluser',
)
);
$user3 = User::from_wp_user( $user_id3 );

$this->assertSame( 'normaluser', $user3->get_preferred_username() );
}
}
Loading