Skip to content

Commit 4cc26df

Browse files
authored
Fix malformed ActivityPub handles for email-based logins (#2082)
1 parent 4313bc9 commit 4cc26df

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: fixed
3+
4+
Fix malformed ActivityPub handles for users with email-based logins (e.g., from Site Kit Google authentication)

includes/model/class-user.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,14 @@ public function get_alternate_url() {
169169
* @return string The preferred username.
170170
*/
171171
public function get_preferred_username() {
172-
return \get_the_author_meta( 'login', $this->_id );
172+
$login = \get_the_author_meta( 'login', $this->_id );
173+
174+
// Handle cases where login is an email address (e.g., from Site Kit Google login).
175+
if ( \filter_var( $login, FILTER_VALIDATE_EMAIL ) ) {
176+
$login = \get_the_author_meta( 'user_nicename', $this->_id );
177+
}
178+
179+
return $login;
173180
}
174181

175182
/**

tests/includes/model/class-test-user.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,50 @@ public function test_get_moved_to() {
128128
$this->assertArrayHasKey( 'movedTo', $user );
129129
$this->assertSame( 'https://example.com', $user['movedTo'] );
130130
}
131+
132+
/**
133+
* Test that email-based usernames are properly sanitized for ActivityPub handles.
134+
*
135+
* @covers ::get_preferred_username
136+
* @covers ::get_webfinger
137+
*/
138+
public function test_email_username_sanitization() {
139+
// Test with email-based login (e.g., from Site Kit Google login).
140+
$user_id = self::factory()->user->create(
141+
array(
142+
'role' => 'author',
143+
'user_login' => '[email protected]',
144+
)
145+
);
146+
$user = User::from_wp_user( $user_id );
147+
148+
// Preferred username should be sanitized.
149+
$this->assertSame( 'testuser123gmail-com', $user->get_preferred_username() );
150+
151+
// Webfinger should not have double @.
152+
$expected_webfinger = 'testuser123gmail-com@' . \wp_parse_url( \home_url(), \PHP_URL_HOST );
153+
$this->assertSame( $expected_webfinger, $user->get_webfinger() );
154+
155+
// Test another email format.
156+
$user_id2 = self::factory()->user->create(
157+
array(
158+
'role' => 'author',
159+
'user_login' => '[email protected]',
160+
)
161+
);
162+
$user2 = User::from_wp_user( $user_id2 );
163+
164+
$this->assertSame( 'admingooglemail-com', $user2->get_preferred_username() );
165+
166+
// Test normal username (no email) remains unchanged.
167+
$user_id3 = self::factory()->user->create(
168+
array(
169+
'role' => 'author',
170+
'user_login' => 'normaluser',
171+
)
172+
);
173+
$user3 = User::from_wp_user( $user_id3 );
174+
175+
$this->assertSame( 'normaluser', $user3->get_preferred_username() );
176+
}
131177
}

0 commit comments

Comments
 (0)