Skip to content

Commit c5524e1

Browse files
author
David Brooks
committed
Add ability to bypass state validation, for Platform Storage use-case
1 parent dd3e41c commit c5524e1

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/lti/LTI_Message_Launch.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,19 @@ public static function from_cache($launch_id, Database $database, Cache $cache =
7373
* Validates all aspects of an incoming LTI message launch and caches the launch if successful.
7474
*
7575
* @param array|string $request An array of post request parameters. If not set will default to $_POST.
76+
* @param bool $insecurelyBypassStateValidation If true, the caller is responsible for securing against CSRF.
7677
*
7778
* @throws LTI_Exception Will throw an LTI_Exception if validation fails.
7879
* @return LTI_Message_Launch Will return $this if validation is successful.
7980
*/
80-
public function validate(array $request = null) {
81+
public function validate(array $request = null, $insecurelyBypassStateValidation = false) {
8182

8283
if ($request === null) {
8384
$request = $_POST;
8485
}
8586
$this->request = $request;
8687

87-
return $this->validate_state()
88+
return $this->validate_state($insecurelyBypassStateValidation)
8889
->validate_jwt_format()
8990
->validate_nonce()
9091
->validate_registration()
@@ -242,7 +243,13 @@ private function cache_launch_data() {
242243
return $this;
243244
}
244245

245-
private function validate_state() {
246+
/**
247+
* @param bool $insecurelyBypassStateValidation If true, the caller is responsible for securing against CSRF.
248+
*/
249+
private function validate_state($insecurelyBypassStateValidation = false) {
250+
if ($insecurelyBypassStateValidation) {
251+
return $this;
252+
}
246253
// Check State for OIDC.
247254
$expectedState = $this->cookie->get_cookie('lti1p3_' . $this->request['state']);
248255
if (empty($expectedState) || $expectedState !== $this->request['state']) {

tests/unit/LTI_Message_Launch_Test.php

+17
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,23 @@ public function testValidateStateWithInvalidStateThrowsException()
5151
)->validate();
5252
}
5353

54+
public function testValidateStateWithInvalidStateThrowsNoStateNotFoundExceptionIfUserBypassesStateValidation()
55+
{
56+
$this->setExpectedException(LTI_JWT_Exception::class, 'Missing id_token');
57+
/** @var Cookie|\PHPUnit_Framework_MockObject_MockObject $cookie */
58+
$cookie = $this->getMockBuilder(Cookie::class)
59+
->setMethods(['get_cookie'])
60+
->getMock();
61+
62+
$cookie->expects($this->never())->method('get_cookie');
63+
64+
LTI_Message_Launch::newInstance(
65+
new DummyDatabase(),
66+
null,
67+
$cookie
68+
)->validate(null, true);
69+
}
70+
5471
public function testValidateState()
5572
{
5673
$state = uniqid();

0 commit comments

Comments
 (0)