Skip to content

[HttpKernel] Document UriSigner #18518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2617,6 +2617,49 @@ defined as annotations:
:doc:`another way to enforce HTTP or HTTPS </security/force_https>`
via the ``requires_channel`` setting.

Signing URIs
~~~~~~~~~~~~

A signed URI is an URI that includes a hash value that depends on the contents of
the URI. This way, you can later check the integrity of the signed URI by
recomputing its hash value and comparing it with the hash included in the URI.

Symfony provides a utility to sign URIs via the :class:`Symfony\\Component\\HttpKernel\\UriSigner`
service, which you can inject in your services or controllers::

// src/Service/SomeService.php
namespace App\Service;

use Symfony\Component\HttpKernel\UriSigner;

class SomeService
{
public function __construct(
private UriSigner $uriSigner,
) {
}

public function someMethod()
{
// ...

// generate a URL youself or get it somehow...
$url = 'https://example.com/foo/bar?sort=desc';

// sign the URL (it adds a query parameter called '_hash')
$signedUrl = $this->uriSigner->sign($url);
// $url = 'https://example.com/foo/bar?sort=desc&_hash=e4a21b9'

// check the URL signature
$uriSignatureIsValid = $this->uriSigner->check($signedUrl);
// $uriSignatureIsValid = true

// if you have access to the current Request object, you can use this
// other method to pass the entire Request object instead of the URI:
$uriSignatureIsValid = $this->uriSigner->checkRequest($request);
}
}

Troubleshooting
---------------

Expand Down