Documentation
¶
Overview ¶
Package util provides common utility functions used across the mcp-oauth library.
This package contains helper functions for string manipulation, formatting, and other shared operations that don't fit into domain-specific packages. These utilities are used internally by multiple packages to avoid code duplication and maintain consistent behavior across the codebase.
Key utilities:
- SafeTruncate: Safely truncates strings for logging sensitive data
Package util provides common utility functions used across the mcp-oauth library. These utilities handle string manipulation, formatting, and other shared operations that don't fit into domain-specific packages.
Index ¶
Constants ¶
const MaxMetadataPathLength = 256
MaxMetadataPathLength is the maximum allowed length for custom metadata paths. This prevents DoS attacks through excessively long path registration.
const MaxPathSegments = 10
MaxPathSegments is the maximum number of path segments (slashes) allowed. This prevents DoS attacks through deeply nested paths.
Variables ¶
This section is empty.
Functions ¶
func NormalizeURL ¶ added in v0.1.45
NormalizeURL normalizes a URL for comparison by removing trailing slashes. This is used for RFC 8707 resource identifier and audience comparison, where URLs with and without trailing slashes should be considered equivalent.
Example:
NormalizeURL("https://example.com/") // Returns: "https://example.com"
NormalizeURL("https://example.com") // Returns: "https://example.com"
NormalizeURL("https://example.com///") // Returns: "https://example.com"
func PathMatchesPrefix ¶ added in v0.1.50
PathMatchesPrefix checks if resourcePath matches or starts with prefix. Handles path boundaries correctly: /mcp/files matches /mcp but not /mc.
This is a pure function used for longest-prefix matching in path configuration lookups. It ensures that path matching respects segment boundaries.
Returns false if either resourcePath or prefix is empty (empty prefix should not match anything in the context of path configuration).
Examples:
PathMatchesPrefix("/mcp/files", "/mcp") // true - valid prefix match
PathMatchesPrefix("/mcp", "/mcp") // true - exact match
PathMatchesPrefix("/mcpx", "/mcp") // false - not a segment boundary
PathMatchesPrefix("/other/mcp", "/mcp") // false - not a prefix
PathMatchesPrefix("/a", "") // false - empty prefix
func SafeTruncate ¶
SafeTruncate safely truncates a string to maxLen characters without panicking. Returns the original string if it's shorter than maxLen, otherwise returns the first maxLen characters. This prevents index out of bounds errors when logging sensitive data like tokens, where only a prefix should be shown.
If maxLen is negative, it's treated as 0 and returns an empty string.
Example:
SafeTruncate("very-long-token-abc123", 8) // Returns: "very-lon"
SafeTruncate("short", 10) // Returns: "short"
SafeTruncate("test", -1) // Returns: ""
func ValidateMetadataPath ¶ added in v0.1.50
ValidateMetadataPath validates a metadata path for security concerns. This is used by both the HTTP handler (for runtime requests) and config validation (for startup configuration).
Security checks performed:
- Path traversal sequences (..)
- Null bytes (can cause issues in some HTTP implementations)
- Excessive path length (DoS prevention)
- Excessive path segments (DoS prevention)
Returns nil if the path is valid, otherwise returns an error describing the issue.
Types ¶
type PathValidationError ¶ added in v0.1.50
PathValidationError represents a path validation failure.
func (*PathValidationError) Error ¶ added in v0.1.50
func (e *PathValidationError) Error() string
Error implements the error interface.