Skip to content
Merged
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
24 changes: 24 additions & 0 deletions addons/glusterfs/docker/entrypoint.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
import json
import os
import re
import subprocess

obj = json.load(open("/etc/fluid/config/config.json"))

mount_point = obj["mounts"][0]["mountPoint"]
target_path = obj["targetPath"]

# Normalize first to resolve redundant separators, '.' and '..' components
target_path = os.path.normpath(target_path)

# Validate that the normalized path is an absolute POSIX path
if not os.path.isabs(target_path) or not target_path.startswith('/'):
print(f"Error: target_path must be absolute: {target_path}")
exit(1)

# Safety check: ensure no '..' components remain after normalization
if '..' in target_path.split('/'):
print(f"Error: Path traversal using '..' is not allowed in target_path: {target_path}")
exit(1)
Comment on lines +20 to +22
Copy link

Copilot AI Feb 14, 2026

Choose a reason for hiding this comment

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

The path traversal check for '..' is performed before normalization. This creates a gap where paths like '/path/./subdir' or paths with multiple slashes would pass this check but might represent the same file system location as a path with '..'. While not directly a '..' bypass, checking before normalization means you're not validating the canonical form of the path. If normalization is moved to occur first (as suggested in another comment), this check can serve as a post-normalization safety check to catch any remaining '..' components, which should not exist in a properly normalized absolute path.

Copilot uses AI. Check for mistakes.

# Validate that the path contains only safe characters
if not re.match(r'^[/a-zA-Z0-9._-]+$', target_path):
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.

security-medium medium

The re module is used here (e.g., re.match() for target_path validation) but is not imported. This will cause a NameError: name 're' is not defined at runtime, leading to a crash and a Denial of Service (DoS) for the addon. Consequently, the intended security validation is never performed. Please add import re at the beginning of the file.

print(f"Error: target_path contains invalid characters: {target_path}")
exit(1)

# Prevent mounting on the root directory
if target_path == '/':
print("Error: target_path resolves to the root directory '/' and is not allowed.")
exit(1)
Comment on lines +12 to +32
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.

security-medium medium

This section has critical vulnerabilities. The current root directory check can be bypassed using paths with double leading slashes (e.g., //), as os.path.normpath preserves these, allowing // to resolve to root despite not matching /. Additionally, the regex validation using the $ anchor can be bypassed by a trailing newline. Beyond these bypasses, the current checks do not prevent users from specifying arbitrary absolute paths like /etc or /usr/bin for target_path, which could lead to mounting volumes over sensitive system directories and potential container escapes. It is crucial to implement robust normalization, use re.fullmatch, and enforce that target_path is restricted to a designated safe base directory.

target_path = os.path.normpath(target_path)
if target_path.startswith('//') and not target_path.startswith('///'):
    target_path = '/' + target_path.lstrip('/')

# Validate that the normalized path is an absolute POSIX path
if not os.path.isabs(target_path) or not target_path.startswith('/'):
    print(f"Error: target_path must be absolute: {target_path}")
    exit(1)

# Safety check: ensure no '..' components remain after normalization
if '..' in target_path.split('/'):
    print(f"Error: Path traversal using '..' is not allowed in target_path: {target_path}")
    exit(1)

# Validate that the path contains only safe characters
if not re.fullmatch(r'^[/a-zA-Z0-9._-]+$', target_path):
    print(f"Error: target_path contains invalid characters: {target_path}")
    exit(1)

# Prevent mounting on the root directory
if target_path == '/':
    print("Error: target_path resolves to the root directory '/' and is not allowed.")
    exit(1)


os.makedirs(target_path, exist_ok=True)
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.

high

The normalized target_path could become the root directory / (for example, if the input was /. or //). Mounting on the root directory is dangerous and should be explicitly prevented. Please add a check for this case before creating the directory.

if target_path == '/':
    print("Error: target_path resolves to the root directory '/' and is not allowed.")
    exit(1)
os.makedirs(target_path, exist_ok=True)


if len(mount_point.split(":")) != 2:
Expand Down
Loading