@@ -2646,6 +2646,49 @@ defined as annotations:
2646
2646
:doc: `another way to enforce HTTP or HTTPS </security/force_https >`
2647
2647
via the ``requires_channel `` setting.
2648
2648
2649
+ Signing URIs
2650
+ ~~~~~~~~~~~~
2651
+
2652
+ A signed URI is an URI that includes a hash value that depends on the contents of
2653
+ the URI. This way, you can later check the integrity of the signed URI by
2654
+ recomputing its hash value and comparing it with the hash included in the URI.
2655
+
2656
+ Symfony provides a utility to sign URIs via the :class: `Symfony\\ Component\\ HttpKernel\\ UriSigner `
2657
+ service, which you can inject in your services or controllers::
2658
+
2659
+ // src/Service/SomeService.php
2660
+ namespace App\Service;
2661
+
2662
+ use Symfony\Component\HttpKernel\UriSigner;
2663
+
2664
+ class SomeService
2665
+ {
2666
+ public function __construct(
2667
+ private UriSigner $uriSigner,
2668
+ ) {
2669
+ }
2670
+
2671
+ public function someMethod()
2672
+ {
2673
+ // ...
2674
+
2675
+ // generate a URL youself or get it somehow...
2676
+ $url = 'https://example.com/foo/bar?sort=desc';
2677
+
2678
+ // sign the URL (it adds a query parameter called '_hash')
2679
+ $signedUrl = $this->uriSigner->sign($url);
2680
+ // $url = 'https://example.com/foo/bar?sort=desc&_hash=e4a21b9'
2681
+
2682
+ // check the URL signature
2683
+ $uriSignatureIsValid = $this->uriSigner->check($signedUrl);
2684
+ // $uriSignatureIsValid = true
2685
+
2686
+ // if you have access to the current Request object, you can use this
2687
+ // other method to pass the entire Request object instead of the URI:
2688
+ $uriSignatureIsValid = $this->uriSigner->checkRequest($request);
2689
+ }
2690
+ }
2691
+
2649
2692
Troubleshooting
2650
2693
---------------
2651
2694
0 commit comments