aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/forms/endpoint.py
blob: e063e7344b9436fefa2fe153f799dc01d1acccba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
#
# SPDX-License-Identifier: BSD-3-Clause

"""Webmention submission form."""

from flask_wtf import FlaskForm
from wtforms import Field, StringField, ValidationError
from wtforms.validators import InputRequired

from .validators import (
    AllowedTargetHostname,
    MaxURLBytes,
    RejectNonPublicIPLiteral,
    StrictHTTPURL,
)


class WebmentionSubmissionForm(FlaskForm):
    """Form for receiving a Webmention.

    The source must be a public HTTP or HTTPS URL distinct from the target.
    The target must be an HTTP or HTTPS URL using an allowed hostname.
    """

    source = StringField(
        "source",
        validators=[
            InputRequired(),
            MaxURLBytes(),
            StrictHTTPURL(),
            RejectNonPublicIPLiteral(),
        ],
    )
    target = StringField(
        "target",
        validators=[
            InputRequired(),
            MaxURLBytes(),
            StrictHTTPURL(),
            AllowedTargetHostname(),
        ],
    )

    def validate_source(self, field: Field) -> None:
        """Reject a source URL that equals the target URL.

        :param field: Source URL field being validated.
        :raises ValidationError: If the source URL equals the target URL.
        """
        if field.data == self.target.data:
            raise ValidationError(field.gettext("Field must not be equal to target."))