VOOZH about

URL: https://deepwiki.com/hypervel/router/3.2-signed-urls-and-security

⇱ Signed URLs and Security | hypervel/router | DeepWiki


Loading...
Menu

Signed URLs and Security

Purpose and Scope

This document describes the cryptographic signing system for URLs in hypervel/router. Signed URLs protect routes by requiring a valid signature that proves the URL was generated by the application. This prevents unauthorized access to sensitive operations and enables time-limited URL sharing.

For general URL generation concepts, see UrlGenerator Class. For implementing signature validation in routes, see Signature Validation Middleware.


Overview

The signing system uses HMAC-SHA256 to cryptographically sign URLs, ensuring they cannot be tampered with. The signature is computed from the complete URL (including query parameters) and a secret key. Signed URLs can optionally include an expiration timestamp for temporary access control.

Key Components:

ComponentLocationPurpose
signedRoute()src/UrlGenerator.php197-216Generates signed URLs for named routes
temporarySignedRoute()src/UrlGenerator.php239-242Generates signed URLs with expiration
hasValidSignature()src/UrlGenerator.php247-251Validates signature and expiration
hasCorrectSignature()src/UrlGenerator.php264-285Validates signature only (ignores expiration)
ValidateSignaturesrc/Middleware/ValidateSignature.php15-92Middleware for automatic validation
InvalidSignatureExceptionsrc/Exceptions/InvalidSignatureException.php9-18Exception thrown on invalid signatures

Sources: src/UrlGenerator.php1-519 src/Middleware/ValidateSignature.php1-92


Signature Generation Process

Algorithm and Reserved Parameters

Signed URLs are created by:

  1. Ensuring parameters don't use reserved names (signature, expires)
  2. Adding an optional expires parameter (Unix timestamp)
  3. Sorting parameters alphabetically by key
  4. Generating the base URL with all parameters
  5. Computing HMAC-SHA256 hash using the signing key
  6. Appending the signature as a query parameter

Diagram: Signature Generation Process

Sources: src/UrlGenerator.php197-234


Reserved Parameters

Two parameter names are reserved for the signing system and cannot be used in route parameters:

ParameterPurposeError Message
signatureHolds the HMAC-SHA256 hash"Signature" is a reserved parameter when generating signed routes. Please rename your route parameter.
expiresHolds the expiration timestamp"Expires" is a reserved parameter when generating signed routes. Please rename your route parameter.

The ensureSignedRouteParametersAreNotReserved() method at src/UrlGenerator.php221-234 enforces this constraint.

Sources: src/UrlGenerator.php221-234


Creating Signed URLs

Method Signatures


Diagram: Signed URL Generation Methods

Sources: src/UrlGenerator.php197-242


Parameter Reference

ParameterTypeDefaultDescription
nameBackedEnum|stringrequiredNamed route identifier
parametersarray[]Route parameters and query parameters
expirationDateInterval|DateTimeInterface|int|nullnullExpiration time (timestamp, interval, or datetime)
absolutebooltrueGenerate absolute or relative URL
serverstring'http'Server name for route resolution

Sources: src/UrlGenerator.php197-242


Usage Examples

The signedRoute() method generates a signed URL for a named route:


The temporarySignedRoute() method is a convenience wrapper that requires an expiration:


Sources: src/UrlGenerator.php197-242


Signature Validation

Validation Methods

The UrlGenerator provides three validation methods:


Diagram: Signature Validation Flow

Sources: src/UrlGenerator.php247-295


Validation Method Comparison

MethodValidates SignatureValidates ExpirationUse Case
hasValidSignature()Complete validation (recommended)
hasCorrectSignature()Signature check only
signatureHasNotExpired()Expiration check only
hasValidRelativeSignature()Validate relative URLs

The hasValidSignature() method at src/UrlGenerator.php247-251 performs both signature and expiration validation:


Sources: src/UrlGenerator.php247-295


Absolute vs Relative Signatures

Signature Modes

Signed URLs can be validated in two modes:

ModeURL FormatUse CaseValidation Call
Absolutehttps://example.com/path?signature=...External links, email linkshasValidSignature($request, true)
Relative/path?signature=...Internal redirects, same-domain linkshasValidSignature($request, false)

The difference affects which portion of the URL is included in the signature computation:

  • Absolute mode: Includes scheme and domain (e.g., https://example.com/path?param=value)
  • Relative mode: Includes only the path (e.g., /path?param=value)

Sources: src/UrlGenerator.php247-285


Signature Reconstruction

The hasCorrectSignature() method at src/UrlGenerator.php264-285 reconstructs the original URL for comparison:


Sources: src/UrlGenerator.php264-285


ValidateSignature Middleware

Middleware Configuration

The ValidateSignature middleware provides automatic signature validation for routes. It supports both absolute and relative validation modes with parameter exclusions.


Diagram: ValidateSignature Middleware Flow

Sources: src/Middleware/ValidateSignature.php56-81


Applying the Middleware

The middleware can be applied with different configurations:

ConfigurationMethod CallDescription
Absolute (default)ValidateSignature::classValidates absolute URLs
Absolute with ignoresValidateSignature::absolute(['param1', 'param2'])Validates absolute URLs, ignores specified parameters
RelativeValidateSignature::relative()Validates relative URLs
Relative with ignoresValidateSignature::relative(['param1', 'param2'])Validates relative URLs, ignores specified parameters

The absolute() and relative() static methods at src/Middleware/ValidateSignature.php37-54 generate middleware strings with encoded configuration:


Sources: src/Middleware/ValidateSignature.php37-54


Parameter Exclusions

Three levels of parameter exclusions are supported:


Diagram: Parameter Exclusion Hierarchy

Global exclusions can be configured for all validation operations:


The except() method at src/Middleware/ValidateSignature.php86-91 modifies the static $neverValidate property, which applies to all instances.

Sources: src/Middleware/ValidateSignature.php22-91


Signing Key Configuration

Default Signing Key

The signing key defaults to the application key configured in config/autoload/app.php:


The getSignedKey() method at src/UrlGenerator.php477-485 retrieves this key:


Sources: src/UrlGenerator.php477-485


Custom Signing Keys

For specialized use cases, a custom signing key can be set using setSignedKey() at src/UrlGenerator.php447-452:


Use cases for custom keys:

  • Separate signing keys for different URL types
  • Key rotation without changing application key
  • Integration with external systems

Sources: src/UrlGenerator.php447-485


Security Considerations

Key Security Best Practices


Diagram: Security Architecture

Sources: src/UrlGenerator.php197-285


Security Features

FeatureImplementationProtection Against
HMAC-SHA256hash_hmac('sha256', ...)Signature forgery
Timing-safe comparisonhash_equals()Timing attacks
Parameter sortingksort($parameters)Parameter order tampering
Reserved parametersensureSignedRouteParametersAreNotReserved()Parameter injection
Expiration timestampsexpires query parameterReplay attacks
Signature parameter exclusionAlways excluded from validationSignature removal

The signature validation at src/UrlGenerator.php277-282 uses hash_equals() for timing-safe comparison:


This prevents timing attacks where an attacker could determine the correct signature by measuring comparison times.

Sources: src/UrlGenerator.php197-285


Error Handling

InvalidSignatureException

When signature validation fails, the InvalidSignatureException at src/Exceptions/InvalidSignatureException.php9-18 is thrown:


Response details:

  • HTTP status code: 403 Forbidden
  • Error message: "Invalid signature."
  • Thrown by: ValidateSignature middleware

The exception extends HttpException, allowing the framework's exception handler to convert it to an appropriate HTTP response.

Sources: src/Exceptions/InvalidSignatureException.php9-18 src/Middleware/ValidateSignature.php64


Integration with Request Validation

The ValidateSignature middleware integrates with the request abstraction through the RequestContract interface. The middleware uses $request->hasValidSignatureWhileIgnoring() at src/Middleware/ValidateSignature.php60 which is a method provided by the request implementation that delegates to the UrlGenerator validation methods.


Diagram: Request Validation Sequence

Sources: src/Middleware/ValidateSignature.php56-65 src/UrlGenerator.php247-295