util

package
v0.2.21 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2025 License: Apache-2.0 Imports: 2 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, IP classification, 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
  • ClassifyIP: Classifies IP addresses for SSRF protection (public, private, loopback, etc.)
  • IsLinkLocal: Checks if an IP is link-local (cloud metadata SSRF protection)
  • IsLoopbackHostname: Checks if a hostname represents a loopback address
  • ValidateMetadataPath: Validates paths for security concerns (path traversal, etc.)

Package util provides common utility functions used across the mcp-oauth library.

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 IsLinkLocal added in v0.2.18

func IsLinkLocal(ip net.IP) bool

IsLinkLocal checks if an IP address is link-local (unicast or multicast). This includes:

  • IPv4 link-local: 169.254.0.0/16 (also catches cloud metadata 169.254.169.254)
  • IPv6 link-local unicast: fe80::/10
  • IPv6 link-local multicast: ff02::/16

Link-local addresses are a significant security concern in cloud environments as they can access instance metadata services (AWS, GCP, Azure).

func IsLoopbackHostname added in v0.2.18

func IsLoopbackHostname(hostname string) bool

IsLoopbackHostname checks if a hostname represents a loopback address. This includes the entire 127.0.0.0/8 range (RFC 1122) and IPv6 ::1. Expects hostname without port (as returned by url.URL.Hostname()).

Note: This function does NOT consider 0.0.0.0 as loopback (it's "unspecified").

func IsPrivateOrInternal added in v0.2.18

func IsPrivateOrInternal(ip net.IP) bool

IsPrivateOrInternal checks if an IP is private, loopback, link-local, or unspecified. This is a convenience function for SSRF protection that returns true for any non-public IP address.

Used by client ID metadata document fetching for comprehensive SSRF protection.

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 IPClassification added in v0.2.18

type IPClassification int

IPClassification represents the security classification of an IP address. This is used for SSRF protection in redirect URI validation and client ID metadata fetching.

const (
	// IPClassificationPublic indicates a publicly routable IP address.
	IPClassificationPublic IPClassification = iota
	// IPClassificationLoopback indicates a loopback address (127.0.0.0/8, ::1).
	IPClassificationLoopback
	// IPClassificationPrivate indicates a private/internal address (RFC 1918, ULA).
	IPClassificationPrivate
	// IPClassificationLinkLocal indicates a link-local address (169.254.x.x, fe80::/10).
	IPClassificationLinkLocal
	// IPClassificationUnspecified indicates an unspecified address (0.0.0.0, ::).
	IPClassificationUnspecified
)

func ClassifyIP added in v0.2.18

func ClassifyIP(ip net.IP) IPClassification

ClassifyIP returns the security classification of an IP address. This is the single source of truth for IP classification used across the library for SSRF protection in redirect URI validation and client metadata fetching.

Classifications:

  • Unspecified: 0.0.0.0, :: (always dangerous, undefined behavior)
  • Loopback: 127.0.0.0/8, ::1 (allowed for native apps per RFC 8252)
  • LinkLocal: 169.254.0.0/16, fe80::/10 (cloud metadata SSRF risk)
  • Private: RFC 1918 (10/8, 172.16/12, 192.168/16), fc00::/7 (SSRF to internal networks)
  • Public: All other addresses (generally safe)

func (IPClassification) String added in v0.2.18

func (c IPClassification) String() string

String returns a human-readable name for the IP classification.

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