Skip to content

Commit 16e819e

Browse files
committed
automatically generate csrf tokens for http_post in testing
1 parent eb5409d commit 16e819e

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

test/phpunit-bootstrap.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
require_once __DIR__ . "/../resources/lib/exceptions/EncodingUnknownException.php";
2626
require_once __DIR__ . "/../resources/lib/exceptions/EncodingConversionException.php";
2727

28+
use UnityWebPortal\lib\CSRFToken;
2829
use UnityWebPortal\lib\UnityGroup;
2930
use UnityWebPortal\lib\UnityHTTPD;
3031
use UnityWebPortal\lib\UnitySQL;
@@ -97,7 +98,7 @@ function switchUser(
9798
ensure(!is_null($USER));
9899
}
99100

100-
function http_post(string $phpfile, array $post_data): void
101+
function http_post(string $phpfile, array $post_data, bool $do_generate_csrf_token = true): void
101102
{
102103
global $LDAP,
103104
$SQL,
@@ -115,6 +116,9 @@ function http_post(string $phpfile, array $post_data): void
115116
$_SERVER["REQUEST_METHOD"] = "POST";
116117
$_SERVER["PHP_SELF"] = preg_replace("/.*webroot\//", "/", $phpfile);
117118
$_SERVER["REQUEST_URI"] = preg_replace("/.*webroot\//", "/", $phpfile); // Slightly imprecise because it doesn't include get parameters
119+
if (!array_key_exists("csrf_token", $post_data) && $do_generate_csrf_token) {
120+
$post_data["csrf_token"] = CSRFToken::generate();
121+
}
118122
$_POST = $post_data;
119123
ob_start();
120124
$post_did_redirect_or_die = false;

0 commit comments

Comments
 (0)