Skip to content
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

Fix path traversal check #623

Merged
merged 3 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions nemoguardrails/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def server(
if config:
# We make sure there is no trailing separator, as that might break things in
# single config mode.
api.app.rails_config_path = config[0].rstrip(os.path.sep)
api.app.rails_config_path = os.path.expanduser(config[0].rstrip(os.path.sep))
else:
# If we don't have a config, we try to see if there is a local config folder
local_path = os.getcwd()
Expand Down Expand Up @@ -189,6 +189,6 @@ def version_callback(value: bool):
def cli(
_: Optional[bool] = typer.Option(
None, "-v", "--version", callback=version_callback, is_eager=True
)
),
):
pass
7 changes: 6 additions & 1 deletion nemoguardrails/server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import json
import logging
import os.path
import re
import time
import warnings
from typing import Any, List, Optional
Expand Down Expand Up @@ -253,7 +254,11 @@ def _get_rails(config_ids: List[str]) -> LLMRails:
base_path = os.path.abspath(app.rails_config_path)
full_path = os.path.normpath(os.path.join(base_path, config_id))

if not full_path.startswith(base_path + os.sep):
# @NOTE: (Rdinu) Reject config_ids that contain dangerous characters or sequences
if re.search(r"[\\/]|(\.\.)", config_id):
raise ValueError("Invalid config_id.")

if os.path.commonprefix([full_path, base_path]) != base_path:
raise ValueError("Access to the specified path is not allowed.")

rails_config = RailsConfig.from_path(full_path)
Expand Down