-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use regex to check for prompt injection attempts. First code with uni…
…t tests
- Loading branch information
1 parent
7dfab70
commit d8d9754
Showing
7 changed files
with
263 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
namespace qtype_aitext; | ||
|
||
/** | ||
* Class logging | ||
* | ||
* @package qtype_aitext | ||
* @copyright 2025 Marcus Green | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class log { | ||
public function insert(int $aitextid, string $prompt) :bool { | ||
global $DB, $USER; | ||
if(get_config('qtype_aitext', 'logallprompts') == '1') { | ||
$record = new \stdClass(); | ||
$record->aitext = $aitextid; | ||
$record->userid = $USER->id; | ||
$record->prompt = $prompt; | ||
$record->pattern = ''; | ||
$record->timeupdated = time(); | ||
|
||
$DB->insert_record('qtype_aitext_log', $record); | ||
return true; | ||
} | ||
|
||
if(get_config('qtype_aitext', 'regularexpressions') == '1') { | ||
$patterns = explode("\n", get_config('qtype_aitext', 'injectionprompts')); | ||
foreach ($patterns as $pattern) { | ||
if (preg_match($pattern, $prompt)) { | ||
// Typically a prompt injection attempt. | ||
$record = new \stdClass(); | ||
$record->aitext = $aitextid; | ||
$record->userid = $USER->id; | ||
$record->prompt = $prompt; | ||
$record->pattern = $pattern; | ||
$record->timeupdated = time(); | ||
$DB->insert_record('qtype_aitext_log', $record); | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
|
||
/** | ||
* Tests for AI Text | ||
* | ||
* @package qtype_aitext | ||
* @category test | ||
* @copyright 2025 Marcus Green | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
namespace qtype_aitext; | ||
use qtype_aitext; | ||
use qtype_aitext_test_helper; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
global $CFG; | ||
require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); | ||
require_once($CFG->dirroot . '/question/type/aitext/tests/helper.php'); | ||
require_once($CFG->dirroot . '/question/type/aitext/questiontype.php'); | ||
|
||
class log_test extends \advanced_testcase { | ||
/** | ||
* Always aitext | ||
* | ||
* @var mixed | ||
*/ | ||
protected $question; | ||
|
||
protected function setUp(): void { | ||
parent::setUp(); | ||
// $this->qtype->id = rand(100, 1000); | ||
$this->question = qtype_aitext_test_helper::make_aitext_question([]); | ||
$this->setAdminUser(); | ||
} | ||
|
||
public function test_insert_all_prompts() { | ||
global $DB; | ||
$this->resetAfterTest(true); | ||
|
||
// Set config for logging all prompts | ||
set_config('logallprompts', '1', 'qtype_aitext'); | ||
|
||
$log = new log(); | ||
$prompt = "Test prompt text"; | ||
|
||
// Test the insert | ||
$result = $log->insert($this->question->id,$prompt); | ||
|
||
// // Assert the result is true | ||
$this->assertTrue($result); | ||
|
||
// // Verify the database record | ||
$records = $DB->get_records('qtype_aitext_log'); | ||
$this->assertCount(1, $records); | ||
} | ||
|
||
public function test_insert_injection_detection() { | ||
global $DB; | ||
$this->resetAfterTest(true); | ||
|
||
|
||
// Set config for regular expressions | ||
set_config('regularexpressions', '1', 'qtype_aitext'); | ||
set_config('injectionprompts', '/malicious|hack/', 'qtype_aitext'); | ||
|
||
$log = new log(); | ||
$prompt = "This is a malicious prompt"; | ||
|
||
// Test the insert | ||
$result = $log->insert($this->question->id,$prompt); | ||
|
||
// Assert the result is true | ||
$this->assertTrue($result); | ||
|
||
// Verify the database record | ||
$records = $DB->get_records('qtype_aitext_log'); | ||
$this->assertCount(1, $records); | ||
|
||
// $record = reset($records); | ||
// $this->assertEquals($prompt, $record->prompt); | ||
// $this->assertEquals('/malicious|hack/', $record->pattern); | ||
} | ||
|
||
public function test_insert_no_logging() { | ||
global $DB; | ||
$this->resetAfterTest(true); | ||
|
||
// Ensure both logging options are disabled | ||
set_config('logallprompts', '0', 'qtype_aitext'); | ||
set_config('regularexpressions', '0', 'qtype_aitext'); | ||
|
||
$log = new log(); | ||
$prompt = "Normal prompt text"; | ||
|
||
// Test the insert | ||
$result = $log->insert($this->question->id,$prompt); | ||
|
||
// Assert the result is false | ||
$this->assertFalse($result); | ||
|
||
// Verify no records were created | ||
$records = $DB->get_records('qtype_aitext_log'); | ||
$this->assertCount(0, $records); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters