Skip to content

Commit

Permalink
Check if input blogs are HTTPS
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Moura <[email protected]>
  • Loading branch information
phsmoura committed Jun 7, 2024
1 parent 2222c91 commit ead4779
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
23 changes: 18 additions & 5 deletions noggin/form/edit_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ def _validate(form, field):
raise ValidationError(_("This does not look like a valid server name."))


class HTTPSValidator:
def __init__(self, message=_('HTTPS is required')):
self.message = message

def __call__(self, form, field):
url = field.data
if url and not url.startswith('https://'):
raise ValidationError(self.message)


class UserSettingsProfileForm(BaseForm):
firstname = StringField(
_('First Name'),
Expand Down Expand Up @@ -122,15 +132,18 @@ class UserSettingsProfileForm(BaseForm):
_('GitLab Username'), validators=[Optional()], filters=[strip_at]
)

website_url = URLField(
_('Website or Blog URL'),
validators=[Optional(), URL(message=_('Valid URL required'))],
website_url = FieldList(
URLField(
validators=[Optional(), URL(message=_('Valid URL required')), HTTPSValidator()],
render_kw={"rows": 6},
),
label=_('Website or Blog URL'),
)

rss_url = FieldList(
URLField(
validators=[Optional(), URL(message=_('Valid URL required'))],
widget=FieldWithClearButtonWidget(URLField.widget),
validators=[Optional(), URL(message=_('Valid URL required')), HTTPSValidator()],
render_kw={"rows": 6},
),
label=_('RSS URL'),
)
Expand Down
29 changes: 28 additions & 1 deletion tests/unit/form/test_edit_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
from bs4 import BeautifulSoup

from noggin.form.edit_user import UserSettingsProfileForm
from noggin.form.edit_user import UserSettingsProfileForm, HTTPSValidator


Obj = namedtuple("Obj", ["ircnick"])
Expand Down Expand Up @@ -92,3 +92,30 @@ def test_form_edit_user_ircnick_valid_empty(app):
html = BeautifulSoup(form.ircnick.entries[0](), 'html.parser')
msg = html.select_one("div.invalid-feedback")
assert msg is None


def test_valid_https_url():
validator = HTTPSValidator()
url = 'https://example.com'
assertIsNone(validator(None, url))


def test_invalid_http_url():
validator = HTTPSValidator()
url = 'http://example.com'
with assertRaises(ValidationError):
validator(None, url)


def test_invalid_non_url():
validator = HTTPSValidator()
url = 'not a url'
with assertRaises(ValidationError):
validator(None, url)


def test_invalid_empty_url():
validator = HTTPSValidator()
url = ''
with assertRaises(ValidationError):
validator(None, url)

0 comments on commit ead4779

Please sign in to comment.