Skip to content

Commit

Permalink
DOP-4086: mut-redirects: Warn on redirects with invalid redirect targ…
Browse files Browse the repository at this point in the history
…ets (#73)

* DOP-4086: mut-redirects: Warn on redirects with invalid redirect targets

* Test and release with Python 3.12 to fix macos CI

* Allow target redirects to not have a scheme if they have an absolute path component

* Update mut/redirects/redirect_main.py

Co-authored-by: Allison Reinheimer Moore <allison.moore@mongodb.com>

---------

Co-authored-by: Allison Reinheimer Moore <allison.moore@mongodb.com>
  • Loading branch information
i80and and schmalliso authored Jun 14, 2024
1 parent 0b31c25 commit fe64f20
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
fetch-depth: 0
- uses: actions/setup-python@v4
with:
python-version: '3.11'
python-version: '3.12'
architecture: 'x64'
- name: Setup poetry
run: python3 -m pip install poetry
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
platform: [ubuntu-20.04, macos-latest]
python-version: ['3.8', '3.12']
python-version: ['3.12']
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v3
Expand Down
28 changes: 24 additions & 4 deletions mut/redirects/redirect_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import os
import re
import sys
import urllib.parse
from typing import List, Optional, Dict, Tuple, Pattern, IO
from docopt import docopt

Expand Down Expand Up @@ -52,6 +53,16 @@ def generate_rule(
old_url_sub = self.rule_substitute(old_url, version)
new_url_sub = self.rule_substitute(new_url, version)

# S3 redirects must either have an HTTP scheme *or* have an absolute path
parsed_new_url = urllib.parse.urlparse(new_url_sub)
if parsed_new_url.scheme not in {
"http",
"https",
} and not parsed_new_url.path.startswith("/"):
raise ValueError(
f"Invalid redirect target: '{new_url_sub}'. Redirect targets must be absolute HTTP URLs."
)

# reformatting the old url
if len(old_url_sub) > 0:
if old_url_sub[0] != "/":
Expand All @@ -60,7 +71,7 @@ def generate_rule(
new_rule = RuleDefinition(is_temp, version, old_url_sub, new_url_sub, False)

# check for symlinks
if len(self.symlinks) > 0 and version is not "raw":
if len(self.symlinks) > 0 and version != "raw":
for symlink in self.symlinks:
if version == symlink[1]:
self.generate_rule(is_temp, symlink[0], old_url, new_url, True)
Expand Down Expand Up @@ -332,7 +343,8 @@ def parse_line(
rc.generate_rule(is_temp, version, old_url, new_url)


def parse_source_file(source_path: str, output: Optional[str]) -> None:
def parse_source_file(source_path: str, output: Optional[str]) -> bool:
have_error = False
version_regex = re.compile(r"([\[\(])([\w.\*]+)(?:-([\w.\*]+))?([\]\)](.))")
url_regex = re.compile(r":(?:[ \t\f\v])(.*)(?:[ \t\f\v]->)(.*)")

Expand All @@ -345,7 +357,12 @@ def parse_source_file(source_path: str, output: Optional[str]) -> None:
for line_num, line in enumerate(file, start=1):
if not line or line.startswith("#"):
continue
parse_line(line, rc, line_num, version_regex, url_regex)

try:
parse_line(line, rc, line_num, version_regex, url_regex)
except ValueError as err:
have_error = True
print(f"{line_num}: {str(err)}", file=sys.stderr)

# Remove unknown symlinks
if root is not None:
Expand All @@ -368,6 +385,8 @@ def parse_source_file(source_path: str, output: Optional[str]) -> None:
with open(output, "w") as f:
write_to_file(rc.rules, f)

return have_error


def main() -> None:
"""Main entry point for mut redirects to create .htaccess file."""
Expand All @@ -376,7 +395,8 @@ def main() -> None:
output = options["--output"]

# Parse source_path and write to file
parse_source_file(source_path, output)
if parse_source_file(source_path, output):
sys.exit(1)


if __name__ == "__main__":
Expand Down

0 comments on commit fe64f20

Please sign in to comment.