Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions specifyweb/backend/trees/default_tree_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ def _config_dir() -> Path:
def _is_remote_source(source: str | Path) -> bool:
return urlparse(str(source)).scheme in ('http', 'https')


def _is_allowed_remote_mapping_url(source: str) -> bool:
parsed = urlparse(source.strip())
if parsed.scheme != 'https' or parsed.netloc != 'files.specifysoftware.org':
Comment thread
grantfitzsimmons marked this conversation as resolved.
return False

if parsed.path in KNOWN_REMOTE_DEFAULT_TREE_PATHS:
return True

if parsed.path.startswith('/treerows/'):
stem = Path(unquote(parsed.path)).stem.lower()
Comment thread
grantfitzsimmons marked this conversation as resolved.
match = re.fullmatch(r'col\d+_(.+)', stem)
if match is not None:
stem = match.group(1)
stem = TREE_ROW_DISCIPLINE_ALIASES.get(stem, stem)
return bool(re.fullmatch(r'[a-z0-9_]+', stem))

return False


def _resolve_in_config(relative_path: Path) -> Optional[Path]:
config_dir = _config_dir()
if relative_path.is_absolute():
Expand Down Expand Up @@ -106,6 +126,12 @@ def load_default_tree_json(source: str) -> Any:
if not _is_remote_source(source):
raise FileNotFoundError(local_path)

if not _is_remote_source(source):
raise ValueError('Default tree source is not allowed.')

if not _is_allowed_remote_mapping_url(source):
raise ValueError('Remote mapping URL is not allowed.')
Comment thread
grantfitzsimmons marked this conversation as resolved.

response = requests.get(source)
response.raise_for_status()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an explicit timeout to this request call to avoid hanging workers (requests.get(source, timeout=(5, 30))) in commit cc978b4.

return response.json()
Expand Down