Skip to content

Commit

Permalink
Disallow periods at the start and end of new usernames
Browse files Browse the repository at this point in the history
Work towards resolving an ambiguity when parsing mentions out of text like "I
like this @bob." by disallowing periods at the start and end of usernames.

There are a small number (~2000 or <0.1%) of existing accounts which do contain
periods at the start or end of usernames.

Fixes #9335
  • Loading branch information
robertknight committed Feb 10, 2025
1 parent e3b812b commit e500c11
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/_extra/api-reference/hypothesis-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ components:
The userID should be of the format `acct:<username>@<authority>`
schema:
type: string
pattern: 'acct:^[A-Za-z0-9._]{3,30}@.*$'
pattern: 'acct:^[A-Za-z0.9_][A-Za-z0-9._]{1,28}[A-Za-z0-9_]@.*$'

# -------------------------
# Reusable responses
Expand Down
2 changes: 1 addition & 1 deletion docs/_extra/api-reference/hypothesis-v2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ components:
The userID should be of the format `acct:<username>@<authority>`
schema:
type: string
pattern: 'acct:^[A-Za-z0-9._]{3,30}@.*$'
pattern: 'acct:^[A-Za-z0.9_][A-Za-z0-9._]{1,28}[A-Za-z0-9_]@.*$'

# -------------------------
# Reusable responses
Expand Down
4 changes: 3 additions & 1 deletion h/accounts/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ class RegisterSchema(CSRFSchema):
validators.Length(min=USERNAME_MIN_LENGTH, max=USERNAME_MAX_LENGTH),
colander.Regex(
USERNAME_PATTERN,
msg=_("Must have only letters, numbers, periods, and underscores."),
msg=_(
"Must have only letters, numbers, periods and underscores. May not start or end with period."
),
),
unique_username,
unblacklisted_username,
Expand Down
6 changes: 5 additions & 1 deletion h/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

USERNAME_MIN_LENGTH = 3
USERNAME_MAX_LENGTH = 30
USERNAME_PATTERN = "(?i)^[A-Z0-9._]+$"

# nb. This pattern is used in Python code, JSON schemas and HTML forms, so it
# needs to use portable syntax.
USERNAME_PATTERN = "^[A-Za-z0-9_][A-Za-z0-9._]+[A-Za-z0-9_]$"

EMAIL_MAX_LENGTH = 100
DISPLAY_NAME_MAX_LENGTH = 30
USER_PUBID_LENGTH = 12
Expand Down
3 changes: 2 additions & 1 deletion h/schemas/api/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
EMAIL_MAX_LENGTH,
USERNAME_MAX_LENGTH,
USERNAME_MIN_LENGTH,
USERNAME_PATTERN,
)
from h.schemas.base import JSONSchema

Expand All @@ -18,7 +19,7 @@ class CreateUserAPISchema(JSONSchema):
"type": "string",
"minLength": USERNAME_MIN_LENGTH,
"maxLength": USERNAME_MAX_LENGTH,
"pattern": "^[A-Za-z0-9._]+$",
"pattern": USERNAME_PATTERN,
},
"email": {
"type": "string",
Expand Down
2 changes: 1 addition & 1 deletion h/templates/admin/users.html.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<form method="POST" action="{{request.route_path('admin.users_rename')}}" class="form-inline">
<input type="hidden" name="csrf_token" value="{{ get_csrf_token() }}">
<input type="hidden" name="userid" value="{{user.userid}}">
<input type="text" name="new_username" placeholder="New Username" pattern="^[A-Za-z0-9._]+$">
<input type="text" name="new_username" placeholder="New Username" pattern="{{ username_pattern }}">
<button class="btn" type="submit">Change username</button>
</form>

Expand Down
2 changes: 2 additions & 0 deletions h/views/admin/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from h import models
from h.accounts.events import ActivationEvent
from h.i18n import TranslationString as _
from h.models.user import USERNAME_PATTERN
from h.security import Permission
from h.services.user_rename import UserRenameError, UserRenameService

Expand Down Expand Up @@ -69,6 +70,7 @@ def users_index(request):
"authority": authority,
"user": user,
"user_meta": user_meta,
"username_pattern": USERNAME_PATTERN,
"format_date": format_date,
}

Expand Down
6 changes: 3 additions & 3 deletions tests/unit/h/accounts/schemas_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ def test_it_is_invalid_when_username_too_short(self, pyramid_request):
schema = schemas.RegisterSchema().bind(request=pyramid_request)

with pytest.raises(colander.Invalid) as exc:
schema.deserialize({"username": "a"})
assert exc.value.asdict()["username"] == ("Must be 3 characters or more.")
schema.deserialize({"username": "ab"})
assert "Must be 3 characters or more." in exc.value.asdict()["username"]

def test_it_is_invalid_when_username_too_long(self, pyramid_request):
schema = schemas.RegisterSchema().bind(request=pyramid_request)
Expand All @@ -102,7 +102,7 @@ def test_it_is_invalid_with_invalid_characters_in_username(self, pyramid_request
with pytest.raises(colander.Invalid) as exc:
schema.deserialize({"username": "Fred Flintstone"})
assert exc.value.asdict()["username"] == (
"Must have only letters, numbers, periods, and underscores."
"Must have only letters, numbers, periods and underscores. May not start or end with period."
)

def test_it_is_invalid_with_false_privacy_accepted(self, pyramid_request):
Expand Down
12 changes: 10 additions & 2 deletions tests/unit/h/schemas/api/user_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,16 @@ def test_it_raises_when_username_too_long(self, schema, payload):
with pytest.raises(ValidationError):
schema.validate(payload)

def test_it_raises_when_username_format_invalid(self, schema, payload):
payload["username"] = "dagr!un"
@pytest.mark.parametrize(
"username",
[
"invalid!chars",
".starts_with_period",
"ends_with_period.",
],
)
def test_it_raises_when_username_format_invalid(self, schema, payload, username):
payload["username"] = username

with pytest.raises(ValidationError):
schema.validate(payload)
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/h/views/admin/users_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest import mock

import pytest
from h_matchers import Any
from pyramid import httpexceptions

from h.models import Annotation
Expand Down Expand Up @@ -35,6 +36,7 @@ def test_users_index(pyramid_request):
"username": None,
"authority": None,
"user": None,
"username_pattern": Any.string(),
"user_meta": {},
"format_date": format_date,
}
Expand Down Expand Up @@ -89,6 +91,7 @@ def test_users_index_no_user_found(models, pyramid_request):
"authority": "foo.org",
"user": None,
"user_meta": {},
"username_pattern": Any.string(),
"format_date": format_date,
}

Expand All @@ -107,6 +110,7 @@ def test_users_index_user_marked_as_deleted(models, pyramid_request, factories):
"authority": "foo.org",
"user": None,
"user_meta": {},
"username_pattern": Any.string(),
"format_date": format_date,
}

Expand All @@ -128,6 +132,7 @@ def test_users_index_user_found(
"authority": "foo.org",
"user": user,
"user_meta": {"annotations_count": 8},
"username_pattern": Any.string(),
"format_date": format_date,
}

Expand Down

0 comments on commit e500c11

Please sign in to comment.