-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moderation: move domains ban/safe-list to DB table
- Loading branch information
Showing
8 changed files
with
134 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2024 CERN. | ||
# | ||
# ZenodoRDM is free software; you can redistribute it and/or modify it | ||
# under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""Domain moderation tests.""" | ||
|
||
import pytest | ||
|
||
from zenodo_rdm.moderation.models import LinkDomain, LinkDomainStatus | ||
|
||
|
||
@pytest.fixture | ||
def domains(db): | ||
"""Create test domains.""" | ||
domains = [ | ||
LinkDomain.create("blog.io", LinkDomainStatus.SAFE), | ||
LinkDomain.create("spam.blog.io", LinkDomainStatus.BANNED), | ||
LinkDomain.create("edu.ch", LinkDomainStatus.SAFE), | ||
LinkDomain.create("cam", LinkDomainStatus.BANNED), | ||
] | ||
db.session.commit() | ||
return domains | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"domain,expected_status", | ||
[ | ||
("https://blog.io/article", LinkDomainStatus.SAFE), | ||
("https://spam.blog.io/article", LinkDomainStatus.BANNED), | ||
("http://other.blog.io/article", LinkDomainStatus.SAFE), | ||
("https://physics.edu.ch/article", LinkDomainStatus.SAFE), | ||
("https://math.edu.ch/article", LinkDomainStatus.SAFE), | ||
("http://spam.cam/content", LinkDomainStatus.BANNED), | ||
("http://sub.spam.cam/content", LinkDomainStatus.BANNED), | ||
], | ||
) | ||
def test_lookup_domain(domains, domain, expected_status): | ||
"""Test domain lookup.""" | ||
assert LinkDomain.lookup_domain(domain).status == expected_status |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2024 CERN. | ||
# | ||
# ZenodoRDM is free software; you can redistribute it and/or modify | ||
# it under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""Moderation models.""" | ||
|
||
import enum | ||
from urllib.parse import urlparse | ||
|
||
from invenio_db import db | ||
from sqlalchemy_utils import ChoiceType, Timestamp | ||
|
||
|
||
class LinkDomainStatus(enum.Enum): | ||
"""Link domain status.""" | ||
|
||
SAFE = "S" | ||
BANNED = "B" | ||
MODERATED = "M" | ||
|
||
|
||
class LinkDomain(db.Model, Timestamp): | ||
"""Link domain model.""" | ||
|
||
__tablename__ = "link_domains" | ||
|
||
id = db.Column(db.Integer, primary_key=True) | ||
|
||
domain = db.Column(db.Text, nullable=False, unique=True) | ||
status = db.Column( | ||
ChoiceType(LinkDomainStatus, impl=db.CHAR(1)), | ||
nullable=False, | ||
) | ||
score = db.Column(db.Integer, nullable=True) | ||
reason = db.Column(db.Text, nullable=True) | ||
|
||
@classmethod | ||
def create(cls, domain, status, score=None, reason=None): | ||
"""Create a link domain.""" | ||
parts = domain.strip(".").split(".") | ||
domain = "." + ".".join(parts[::-1]).lower() | ||
ld = cls(domain=domain, status=status, score=score, reason=reason) | ||
db.session.add(ld) | ||
return ld | ||
|
||
@classmethod | ||
def lookup_domain(cls, url): | ||
"""Lookup the status of a URL's domain.""" | ||
try: | ||
parsed = urlparse(url) | ||
except ValueError: | ||
return None | ||
|
||
domain = parsed.netloc or "" | ||
domain = domain.lstrip("www.") | ||
domain_parts = domain.split(".") | ||
if not domain_parts: | ||
return None | ||
|
||
reversed_domain = "." + ".".join(domain_parts[::-1]).lower() | ||
return ( | ||
cls.query.filter( | ||
# Exact match | ||
(LinkDomain.domain == reversed_domain) | ||
# Or subdomain match | ||
| db.literal(reversed_domain).like(LinkDomain.domain + ".%") | ||
) | ||
# Order by length of domain to get the most specific match | ||
.order_by(db.func.length(LinkDomain.domain).desc()) | ||
.limit(1) | ||
.scalar() | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters