diff --git a/lib/bot.php b/lib/bot.php index 240e149..911e85f 100644 --- a/lib/bot.php +++ b/lib/bot.php @@ -288,16 +288,6 @@ function doarchive( $pdata .= str_repeat('=', $level) . $array['header'] . str_repeat('=', $level) . $array['content']; } - if (substr(strtolower(str_replace('_', ' ', $archiveprefix)), 0, strlen($page)) != strtolower($page)) { - $expected_key = hash('sha256', trim($page) . trim($config->archiveprefix) . trim(Config::$archive_key)); - if (trim($key) != $expected_key) { - $logger->error( - 'Incorrect key for archive prefix; page=' . $page . ', prefix=' . $config->archiveprefix - ); - $archiveprefix = $page . '/Archives/'; - } - } - if ($age == '99999') { $age = 0; } diff --git a/lib/config/functions.php b/lib/config/functions.php index 9351ed7..48eff05 100644 --- a/lib/config/functions.php +++ b/lib/config/functions.php @@ -304,6 +304,17 @@ function build_config_from_config_block(string $page, RawConfig $block) $config->once = $options['once'] === '1'; } + // If the archive prefix is not under the same page, then it requires a key, + // verify the key is correct, otherwise use a default archive prefix. + if (substr(strtolower(str_replace('_', ' ', $config->archiveprefix)), 0, strlen($page)) != strtolower($page)) { + $expected_key = hash('sha256', trim($page) . trim($config->archiveprefix) . trim(Config::$archive_key)); + if ($config->key != $expected_key) { + $logger->error('Incorrect key for archive prefix; page=' . $page . ', prefix=' . $config->archiveprefix); + $config->archiveprefix = $page . '/Archives/'; + $config->key = DefaultConfig::$key; + } + } + $config->is_valid = true; return $config; } diff --git a/tests/ConfigGenerationTest.php b/tests/ConfigGenerationTest.php index 4af8bc3..582302f 100644 --- a/tests/ConfigGenerationTest.php +++ b/tests/ConfigGenerationTest.php @@ -68,4 +68,20 @@ public function testGeneratedConfigMatchesExpectedConfig( $this->assertEquals($generated_config, $expected_config); } + + public function testMismatchedArchivePrefixWithoutValidKeyResetsToDefault(): void + { + $raw_config = '{{User:ClueBot III/ArchiveThis' . + '|archiveprefix=Some Other Page/Archives/' . + '|key=totally-wrong-key' . + '|format=Y/F}}'; + + $config_blocks = find_config_blocks("ClueBot III", $raw_config); + $this->assertCount(1, $config_blocks); + + $config = build_config_from_config_block("Test Page", $config_blocks[0]); + + $this->assertEquals("Test Page/Archives/", $config->archiveprefix); + $this->assertEquals(DefaultConfig::$key, $config->key); + } } diff --git a/tests/data/config-snippets/invalid-key/meta.json b/tests/data/config-snippets/invalid-key/meta.json new file mode 100644 index 0000000..b35f26e --- /dev/null +++ b/tests/data/config-snippets/invalid-key/meta.json @@ -0,0 +1,3 @@ +{ + "title": "Talk:Example Page" +} diff --git a/tests/data/config-snippets/invalid-key/normative.txt b/tests/data/config-snippets/invalid-key/normative.txt new file mode 100644 index 0000000..1a30efd --- /dev/null +++ b/tests/data/config-snippets/invalid-key/normative.txt @@ -0,0 +1,4 @@ +{{User:ClueBot III/ArchiveThis +|archiveprefix=Talk:Example Page/Archives/ +|age=2160 +}} \ No newline at end of file diff --git a/tests/data/config-snippets/invalid-key/page.txt b/tests/data/config-snippets/invalid-key/page.txt new file mode 100644 index 0000000..6d06bd8 --- /dev/null +++ b/tests/data/config-snippets/invalid-key/page.txt @@ -0,0 +1 @@ +{{User:ClueBot III/ArchiveThis|archiveprefix=Talk:NotMyPage/Archive|age=2160|key=invalid}}