util

package
v0.2.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 9, 2025 License: Apache-2.0 Imports: 1 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const MaxMetadataPathLength = 256

MaxMetadataPathLength is the maximum allowed length for custom metadata paths. This prevents DoS attacks through excessively long path registration.

View Source
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

func NormalizeURL(url string) string

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

func PathMatchesPrefix(resourcePath, prefix string) bool

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

func SafeTruncate(s string, maxLen int) string

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

func ValidateMetadataPath(path string) error

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

type PathValidationError struct {
	Path   string
	Reason string
}

PathValidationError represents a path validation failure.

func (*PathValidationError) Error added in v0.1.50

func (e *PathValidationError) Error() string

Error implements the error interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL