Documentation
¶
Index ¶
- Variables
- func AllowedDomains() []string
- func Alphanumeric(s string) string
- func Filename(s string) (string, error)
- func Identifier(s string) (string, error)
- func ModuleName(s string) (string, error)
- func SanitizePath(path string) (string, error)
- func Username(s string) (string, error)
- func ValidateInputFile(filePath string) (string, error)
- func ValidatePathWithinBase(basePath, targetPath string) (string, error)
- func ValidateURL(rawURL string, allowedDomains []string) error
Constants ¶
This section is empty.
Variables ¶
var (
ErrInvalidFilename = errorx.IllegalArgument.New("invalid filename")
)
Functions ¶
func AllowedDomains ¶
func AllowedDomains() []string
AllowedDomains returns the allowlist of trusted domains for software downloads.
func Alphanumeric ¶
Alphanumeric ensures the input string to be ascii alphanumeric
func Identifier ¶
Identifier validates and sanitizes a string to be a safe identifier. It only allows alphanumeric characters (a-z, A-Z, 0-9), underscores, and hyphens. This is useful for validating module names, filenames, usernames, and other identifiers. Returns an error if the identifier is empty or contains no valid characters after sanitization.
func SanitizePath ¶
SanitizePath validates and sanitizes the given path according to strict security rules.
Specifically, it:
- Rejects paths containing shell metacharacters (e.g., ; & | $ ` < > ( ) { } [ ] * ? ~).
- Rejects path traversal attempts (e.g., segments like "../", "/..", or paths ending with "..").
- Requires the input path to be absolute.
- Normalizes the path by removing redundant slashes and dot directories (using filepath.Clean).
- May return a cleaned version of the input path that differs from the original.
Returns the sanitized (cleaned) path, or an error if the input is invalid or unsafe.
func Username ¶
Username validates and sanitizes a username string to prevent security vulnerabilities.
This function is particularly important when dealing with environment variables like SUDO_USER that could be manipulated by attackers. It ensures that the username:
- Is not empty (precondition check)
- Contains only alphanumeric characters (a-z, A-Z, 0-9), underscores, and hyphens
- Does not contain path traversal sequences (e.g., "..", "/")
- Does not contain shell metacharacters or special characters
- Contains at least one valid character after sanitization
Returns the sanitized username, or an error if the username is invalid or unsafe.
func ValidateInputFile ¶
ValidateInputFile validates a file path intended for reading user-provided input files.
This function provides comprehensive validation to prevent path traversal attacks and ensure the file is safe to read. It:
- Converts relative paths to absolute paths
- Sanitizes the path to prevent path traversal and shell injection
- Verifies the file exists
- Ensures the path points to a regular file (not a directory, device, socket, etc.)
This is designed to be used in defense-in-depth scenarios where the same validation is applied at multiple layers (CLI entry point and internal APIs).
Returns the sanitized absolute path or an error if validation fails.
func ValidatePathWithinBase ¶
ValidatePathWithinBase validates that a path is within a specific base directory.
This function:
- Sanitizes the input path
- Ensures the sanitized path starts with the base directory
- Prevents path traversal outside the base directory
Returns the sanitized path or an error if the path is outside the base directory.
func ValidateURL ¶
ValidateURL validates a URL to ensure it's safe to use for downloads.
This function provides SSRF (Server-Side Request Forgery) protection by checking that:
- The URL is not empty and can be parsed
- The scheme is HTTPS only (HTTP is rejected for security)
- The host is not empty
- The host is in the allowed domain list for trusted registries
Returns an error if the URL is invalid or unsafe.
Types ¶
This section is empty.