Skip to content

Commit 5025c33

Browse files
committed
Add csrf_header() and csrf_meta() helper functions
1 parent 198c264 commit 5025c33

3 files changed

Lines changed: 62 additions & 0 deletions

File tree

system/Common.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,25 @@ function csrf_token(): string
723723

724724
//--------------------------------------------------------------------
725725

726+
if (! function_exists('csrf_header'))
727+
{
728+
/**
729+
* Returns the CSRF header name.
730+
* Can be used in Views by adding it to the meta tag
731+
* or used in javascript to define a header name when using APIs.
732+
*
733+
* @return string
734+
*/
735+
function csrf_header(): string
736+
{
737+
$config = config(App::class);
738+
739+
return $config->CSRFHeaderName;
740+
}
741+
}
742+
743+
//--------------------------------------------------------------------
744+
726745
if (! function_exists('csrf_hash'))
727746
{
728747
/**
@@ -759,6 +778,23 @@ function csrf_field(string $id = null): string
759778

760779
//--------------------------------------------------------------------
761780

781+
if (! function_exists('csrf_meta'))
782+
{
783+
/**
784+
* Generates a meta tag for use within javascript calls.
785+
*
786+
* @param string|null $id
787+
*
788+
* @return string
789+
*/
790+
function csrf_meta(string $id = null): string
791+
{
792+
return '<meta' . (! empty($id) ? ' id="' . esc($id, 'attr') . '"' : '') . ' name="' . csrf_header() . '" content="' . csrf_hash() . '" />';
793+
}
794+
}
795+
796+
//--------------------------------------------------------------------
797+
762798
if (! function_exists('force_https'))
763799
{
764800
/**

tests/system/CommonFunctionsTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,11 @@ public function testCSRFToken()
251251
$this->assertEquals('csrf_test_name', csrf_token());
252252
}
253253

254+
public function testCSRFHeader()
255+
{
256+
$this->assertEquals('X-CSRF-TOKEN', csrf_header());
257+
}
258+
254259
public function testHash()
255260
{
256261
$this->assertEquals(32, strlen(csrf_hash()));
@@ -261,6 +266,11 @@ public function testCSRFField()
261266
$this->assertContains('<input type="hidden" ', csrf_field());
262267
}
263268

269+
public function testCSRFMeta()
270+
{
271+
$this->assertContains('<meta name="X-CSRF-TOKEN" ', csrf_meta());
272+
}
273+
264274
// ------------------------------------------------------------------------
265275

266276
/**

user_guide_src/source/general/common_functions.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ Miscellaneous Functions
176176

177177
Returns the name of the current CSRF token.
178178

179+
.. php:function:: csrf_header ()
180+
181+
:returns: The name of the header for current CSRF token.
182+
:rtype: string
183+
184+
The name of the header for current CSRF token.
185+
179186
.. php:function:: csrf_hash ()
180187
181188
:returns: The current value of the CSRF hash.
@@ -192,6 +199,15 @@ Miscellaneous Functions
192199

193200
<input type="hidden" name="{csrf_token}" value="{csrf_hash}">
194201

202+
.. php:function:: csrf_meta ()
203+
204+
:returns: A string with the HTML for meta tag with all required CSRF information.
205+
:rtype: string
206+
207+
Returns a meta tag with the CSRF information already inserted:
208+
209+
<meta name="{csrf_header}" content="{csrf_hash}">
210+
195211
.. php:function:: force_https ( $duration = 31536000 [, $request = null [, $response = null]] )
196212
197213
:param int $duration: The number of seconds browsers should convert links to this resource to HTTPS.

0 commit comments

Comments
 (0)