Skip to content

Commit

Permalink
Add scheduled task to cleanup old CSP data #90
Browse files Browse the repository at this point in the history
  • Loading branch information
bwalkerl committed Jan 2, 2025
1 parent a1eb7ef commit c3398cc
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 2 deletions.
52 changes: 52 additions & 0 deletions classes/task/cleanup_csp_task.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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 local_csp\task;

use core\task\scheduled_task;

/**
* Scheduled task to cleanup old CSP records.
*
* @package local_csp
* @author Benjamin Walker <[email protected]>
* @copyright 2024 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cleanup_csp_task extends scheduled_task {
/**
* Get task name.
*/
public function get_name(): string {
return get_string('cleanup_csp_task', 'local_csp');
}

/**
* Execute the task.
*/
public function execute() {
global $DB;

// Clean up all CSP records that haven't had any recent violations.
$duration = get_config('local_csp', 'cleanup_duration');
if (is_numeric($duration)) {
$params = [
'timeexpired' => time() - $duration,
];
$DB->delete_records_select('local_csp', 'COALESCE(timeupdated, timecreated) < :timeexpired', $params);
}
}
}
38 changes: 38 additions & 0 deletions db/tasks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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/>.

/**
* Schedule tasks definition.
*
* @package local_csp
* @author Benjamin Walker <[email protected]>
* @copyright 2024 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

$tasks = [
[
'classname' => 'local_csp\task\cleanup_csp_task',
'blocking' => 0,
'minute' => '0',
'hour' => '0',
'day' => '*',
'month' => '*',
'dayofweek' => '*',
],
];
3 changes: 3 additions & 0 deletions lang/en/local_csp.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
$string['blockeddomain'] = 'Domain';
$string['blockeduri'] = 'Blocked URI';
$string['blockedurlpaths'] = 'Blocked paths';
$string['cleanupduration'] = 'CSP cleanup duration';
$string['cleanupdurationdescription'] = 'Removes CSP records that have not had any violations during the selected time period. The counts of records that have had violations will not be reset. Setting the expiry to 0 will remove all records.';
$string['cleanup_csp_task'] = 'Cleanup old CSP data task';
$string['configurecspheader'] = 'Configure CSP header';
$string['cspdirectives'] = 'CSP directives';
$string['cspdirectivesinfo'] = '<p>Example of CSP directives (please refer to the above link for exact syntax):<br /><span style="color:#00acdf">script-src https:; style-src cdn.example.com; default-src \'self\';</span></p>';
Expand Down
8 changes: 8 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,12 @@
'',
PARAM_TEXT
));

$settings->add(new admin_setting_configduration(
'local_csp/cleanup_duration',
get_string('cleanupduration', 'local_csp'),
get_string('cleanupdurationdescription', 'local_csp'),
26 * WEEKSECS,
WEEKSECS
));
}
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

defined('MOODLE_INTERNAL') || die;

$plugin->version = 2024052400;
$plugin->release = 2024052400;
$plugin->version = 2025010200;
$plugin->release = 2025010200;
$plugin->requires = 2015051100;
$plugin->maturity = MATURITY_STABLE;
$plugin->component = 'local_csp';
Expand Down

0 comments on commit c3398cc

Please sign in to comment.