-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Source specifying for relative links is allowed #232
Open
myhailo-chernyshov-rg
wants to merge
7
commits into
openedx:master
Choose a base branch
from
raccoongang:myhailochernyshov/source-specifying-for-relative-links-is-allowed
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d695ea4
feat: Source specifying for relative links is allowed
myhailo-chernyshov-rg 9df6eca
feat: `relative_links_source` CLI argument is validated
myhailo-chernyshov-rg e6c02b9
test: Existed tests are fixed
myhailo-chernyshov-rg 30bbef0
fix: IPv6 validation is fixed
myhailo-chernyshov-rg bc298c2
test: Relative links source validation is tested
myhailo-chernyshov-rg 751e8ed
style: Code style is improved
myhailo-chernyshov-rg 1b67ba1
fix: All static files are considered during relative external links p…
myhailo-chernyshov-rg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
CDATA_PATTERN = r"<!\[CDATA\[(?P<content>.*?)\]\]>" | ||
OLX_STATIC_DIR = "static" | ||
OLX_STATIC_PATH_TEMPLATE = f"/{OLX_STATIC_DIR}/{{static_filename}}" |
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,18 @@ | ||
from dataclasses import dataclass, field | ||
from collections import ChainMap | ||
from typing import Dict | ||
|
||
|
||
@dataclass | ||
class OlxToOriginalStaticFilePaths: | ||
""" | ||
Provide OLX static file to Common cartridge static file mappings. | ||
""" | ||
|
||
# Static files from `web_resources` directory | ||
web_resources: Dict[str, str] = field(default_factory=dict) | ||
# Static files that are outside of `web_resources` directory, but still required | ||
extra: Dict[str, str] = field(default_factory=dict) | ||
|
||
def __post_init__(self) -> None: | ||
self.all = ChainMap(self.extra, self.web_resources) |
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
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
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
Empty file.
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,62 @@ | ||
import argparse | ||
import re | ||
|
||
from cc2olx.utils import is_valid_ipv6_address | ||
|
||
|
||
class LinkSourceValidator: | ||
""" | ||
Validate a link source. | ||
""" | ||
|
||
UL = "\u00a1-\uffff" # Unicode letters range (must not be a raw string). | ||
|
||
# IP patterns | ||
IPV4_REGEX = ( | ||
r"(?:0|25[0-5]|2[0-4][0-9]|1[0-9]?[0-9]?|[1-9][0-9]?)" | ||
r"(?:\.(?:0|25[0-5]|2[0-4][0-9]|1[0-9]?[0-9]?|[1-9][0-9]?)){3}" | ||
) | ||
IPV6_REGEX = r"\[[0-9a-f:.]+\]" # (simple regex, validated later) | ||
|
||
# Host patterns | ||
HOSTNAME_REGEX = rf"[a-z{UL}0-9](?:[a-z{UL}0-9-]{{0,61}}[a-z{UL}0-9])?" | ||
# Max length for domain name labels is 63 characters per RFC 1034 sec. 3.1 | ||
DOMAIN_REGEX = rf"(?:\.(?!-)[a-z{UL}0-9-]{{1,63}}(?<!-))*" | ||
TLD_REGEX = ( | ||
r"\." # dot | ||
r"(?!-)" # can't start with a dash | ||
rf"(?:[a-z{UL}-]{{2,63}}" # domain label | ||
r"|xn--[a-z0-9]{1,59})" # or punycode label | ||
r"(?<!-)" # can't end with a dash | ||
r"\.?" # may have a trailing dot | ||
) | ||
HOST_REGEX = "(" + HOSTNAME_REGEX + DOMAIN_REGEX + TLD_REGEX + "|localhost)" | ||
|
||
LINK_SOURCE_REGEX = ( | ||
r"^https?://" # scheme is validated separately | ||
r"(?:[^\s:@/]+(?::[^\s:@/]*)?@)?" # user:pass authentication | ||
rf"(?P<netloc>{IPV4_REGEX}|{IPV6_REGEX}|{HOST_REGEX})" | ||
r"(?::[0-9]{1,5})?" # port | ||
r"/?" # trailing slash | ||
r"\Z" | ||
) | ||
|
||
message = "Enter a valid URL." | ||
|
||
def __call__(self, value: str) -> str: | ||
if not (link_source_match := re.fullmatch(self.LINK_SOURCE_REGEX, value, re.IGNORECASE)): | ||
raise argparse.ArgumentTypeError(self.message) | ||
|
||
self._validate_ipv6_address(link_source_match.group("netloc")) | ||
|
||
return value | ||
|
||
def _validate_ipv6_address(self, netloc: str) -> None: | ||
""" | ||
Check netloc correctness if it's an IPv6 address. | ||
""" | ||
potential_ipv6_regex = r"^\[(.+)\](?::[0-9]{1,5})?$" | ||
if netloc_match := re.search(potential_ipv6_regex, netloc): | ||
potential_ip = netloc_match[1] | ||
if not is_valid_ipv6_address(potential_ip): | ||
raise argparse.ArgumentTypeError(self.message) |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add some validation for the format of
relative_links_source
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validation is added on the argument parsing step.