-
Notifications
You must be signed in to change notification settings - Fork 385
Feature: Proof of Work captcha #4495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
crhallberg
wants to merge
28
commits into
vufind-org:dev
Choose a base branch
from
crhallberg:pow-captcha
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
0b2b78c
start: proof of work captcha feature.
crhallberg b3b6c80
feat: session id used to generate consistent secrets.
crhallberg 6fc0381
fix: alignment of captcha with comment.
crhallberg 356676b
fix: send less information back with form, since we have a better sta…
crhallberg 35a4318
doc: copyright 2
crhallberg 5a6f033
chore: qa-js-and-scss
crhallberg 843c2ea
chore: php-cs-fixer
crhallberg a21c1e2
doc: rename CAPTCHA
crhallberg 1d3336e
Update module/VuFind/src/VuFind/Captcha/PoW.php
crhallberg 5746da2
doc: update copyright
crhallberg 136e3cb
doc: copyright
crhallberg 04fe429
feat: add Altcha as an option.
crhallberg 6b5884f
fix: woke up in the middle of the night in a cold sweat with the abso…
crhallberg 8ff7271
refactor: pass Altcha instance from Factory.
crhallberg 761e780
rm: unused file since vendor supplies WebComponent.
crhallberg c97bfba
fix: remove postinstall.
crhallberg 8dc7d17
doc: add Altcha LICENSE.
crhallberg 62ca158
remove home-grown pow solution.
crhallberg d06cb50
fix: escape challenge JSON.
crhallberg 7f1b88a
Update config/vufind/config.ini
crhallberg 182f5bd
docs: fix parameter types.
crhallberg a9f9003
style: phpcs
crhallberg a358335
Merge branch 'pow-captcha' of https://github.com/crhallberg/vufind in…
crhallberg d6972fa
Merge branch 'dev' into pow-captcha
crhallberg fb11bf4
Merge branch 'dev' into pow-captcha
demiankatz de5dae0
Fix outdated license text.
demiankatz 826c5a9
Handle garbled input data more robustly.
demiankatz 2e485c6
php-cs-fixer
demiankatz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,103 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * ReCaptcha CAPTCHA. | ||
| * | ||
| * PHP version 8 | ||
| * | ||
| * Copyright (C) Villanova University 2020. | ||
crhallberg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * This program is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License version 2, | ||
| * as published by the Free Software Foundation. | ||
| * | ||
| * This program 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 this program; if not, write to the Free Software | ||
| * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| * | ||
| * @category VuFind | ||
| * @package CAPTCHA | ||
| * @author Mario Trojan <mario.trojan@uni-tuebingen.de> | ||
crhallberg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
| * @link https://vufind.org Main Page | ||
| */ | ||
|
|
||
| namespace VuFind\Captcha; | ||
|
|
||
| use Laminas\Mvc\Controller\Plugin\Params; | ||
|
|
||
| /** | ||
| * ReCaptcha CAPTCHA. | ||
crhallberg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * @category VuFind | ||
| * @package CAPTCHA | ||
| * @author Mario Trojan <mario.trojan@uni-tuebingen.de> | ||
| * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
| * @link https://vufind.org/wiki/development Wiki | ||
| */ | ||
| class PoW extends AbstractBase | ||
| { | ||
| /** | ||
| * Constructor | ||
| * | ||
| * @param string $hashAlgo hash algorithm (default sha256) | ||
| * @param int $difficulty number of zeroes needed for proof (default 5) | ||
| */ | ||
| public function __construct( | ||
| string $hashAlgo, | ||
| int $difficulty, | ||
| ) { | ||
| $this->hashAlgo = $hashAlgo; | ||
| $this->difficulty = $difficulty; | ||
| } | ||
|
|
||
| /** | ||
| * Get list of URLs with JS dependencies to load for the active CAPTCHA type. | ||
| * | ||
| * @return array | ||
| */ | ||
| public function getJsIncludes(): array | ||
| { | ||
| return ['captcha-pow.js']; | ||
| } | ||
|
|
||
| /** | ||
| * Pull the captcha field from controller params and check them for accuracy | ||
crhallberg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * @param Params $params Controller params | ||
| * | ||
| * @return bool | ||
| */ | ||
| public function verify(Params $params): bool | ||
| { | ||
| error_log($params->fromPost('pow-captcha-challenge')); | ||
| error_log($params->fromPost('pow-captcha-nonce')); | ||
|
|
||
| // @TODO: compare to Session challenge | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| public function getChallenge() { | ||
| // @TODO: random challenge | ||
| // @TODO: story in session for verify | ||
| return $this->challenge ?? "WOW VERY CHALLENGE MUCH FIND"; | ||
| } | ||
|
|
||
| public function getDifficulty() { | ||
| return $this->difficulty; | ||
| } | ||
|
|
||
| public function getHashAlgo() { | ||
| return $this->hashAlgo; | ||
| } | ||
|
|
||
| public function getStart() { | ||
| return random_int(0, 1000); | ||
| } | ||
| } | ||
This file contains hidden or 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,87 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Factory for Image CAPTCHA module. | ||
crhallberg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * PHP version 8 | ||
| * | ||
| * Copyright (C) Villanova University 2020. | ||
crhallberg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * This program is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License version 2, | ||
| * as published by the Free Software Foundation. | ||
| * | ||
| * This program 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 this program; if not, write to the Free Software | ||
| * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| * | ||
| * @category VuFind | ||
| * @package CAPTCHA | ||
| * @author Mario Trojan <mario.trojan@uni-tuebingen.de> | ||
crhallberg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
| * @link https://vufind.org/wiki/development Wiki | ||
| */ | ||
|
|
||
| namespace VuFind\Captcha; | ||
|
|
||
| use Laminas\ServiceManager\Exception\ServiceNotCreatedException; | ||
| use Laminas\ServiceManager\Exception\ServiceNotFoundException; | ||
| use Laminas\ServiceManager\Factory\FactoryInterface; | ||
| use Psr\Container\ContainerExceptionInterface as ContainerException; | ||
| use Psr\Container\ContainerInterface; | ||
|
|
||
| use function is_callable; | ||
|
|
||
| /** | ||
| * Image CAPTCHA factory. | ||
crhallberg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * @category VuFind | ||
| * @package CAPTCHA | ||
| * @author Mario Trojan <mario.trojan@uni-tuebingen.de> | ||
| * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
| * @link https://vufind.org/wiki/development Wiki | ||
| */ | ||
| class PoWFactory implements FactoryInterface | ||
| { | ||
| /** | ||
| * Create an object | ||
| * | ||
| * @param ContainerInterface $container Service manager | ||
| * @param string $requestedName Service being created | ||
| * @param null|array $options Extra options (optional) | ||
| * | ||
| * @return object | ||
| * | ||
| * @throws ServiceNotFoundException if unable to resolve the service. | ||
| * @throws ServiceNotCreatedException if an exception is raised when | ||
| * creating a service. | ||
| * @throws ContainerException&\Throwable if any other error occurs | ||
| */ | ||
| public function __invoke( | ||
| ContainerInterface $container, | ||
| $requestedName, | ||
| ?array $options = null | ||
| ) { | ||
| if (!empty($options)) { | ||
| throw new \Exception('Unexpected options passed to factory.'); | ||
| } | ||
|
|
||
| $config = $container | ||
| ->get(\VuFind\Config\PluginManager::class) | ||
| ->get('config'); | ||
|
|
||
| $hashAlgo = $config->Captcha->hashAlgo ?? 'sha256'; | ||
demiankatz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!in_array($hashAlgo, hash_algos())) { | ||
| throw new \Exception('Invalid hash algorithm: ' . $hashAlgo . '.'); | ||
| } | ||
|
|
||
| $difficulty = intval($config->Captcha->pow_difficulty ?? 5); | ||
|
|
||
| return new $requestedName($hashAlgo, $difficulty); | ||
| } | ||
| } | ||
This file contains hidden or 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,49 @@ | ||
| function hashChallenge(hashAlgo, challenge, nonce) { | ||
| return new Promise((resolve, reject) => { | ||
| let buffer = new TextEncoder().encode(`${challenge}:${nonce}`); | ||
| crypto.subtle.digest(phpAlgoToJS(hashAlgo), buffer.buffer).then((result) => { | ||
| // convert buffer to byte array | ||
| const bytes = Array.from(new Uint8Array(result)); | ||
|
|
||
| // convert bytes to hex string | ||
| const hex = bytes | ||
| .map((byte) => byte.toString(16).padStart(2, "0")) | ||
| .join("") | ||
|
|
||
| resolve(hex); | ||
| }, reject); | ||
| }); | ||
| } | ||
|
|
||
| function phpAlgoToJS(algo) { | ||
| return { | ||
| "sha1": "SHA-1", | ||
| "sha256": "SHA-256", | ||
| "sha384": "SHA-384", | ||
| "sha512": "SHA-512", | ||
| }[algo] ?? algo; | ||
| } | ||
|
|
||
|
|
||
| self.addEventListener("message", async (event) => { | ||
| const { challenge, difficulty, hashAlgo, start } = event.data; | ||
| const algo = phpAlgoToJS(hashAlgo); | ||
|
|
||
| let nonce = start; | ||
| const target = "0".repeat(difficulty); | ||
| console.log(target); | ||
| let attempt = await hashChallenge(algo, challenge, nonce); | ||
| while (!attempt.startsWith(target)) { | ||
| nonce += 1; | ||
| attempt = await hashChallenge(algo, challenge, nonce); | ||
|
|
||
| if (attempt.startsWith("0000")) { | ||
| console.log(attempt); | ||
| } | ||
| } | ||
|
|
||
| self.postMessage({ | ||
| nonce, | ||
| iters: nonce - start, | ||
| }); | ||
| }); |
This file contains hidden or 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,87 @@ | ||
| /* global VuFind */ | ||
|
|
||
| VuFind.register('pow-captcha', function PoWCaptchas() { | ||
| const READY_CLASS = "js-pow-captcha-ready"; | ||
| const CHALLENGE_SELECTOR = '[name="pow-captcha-challenge"]'; | ||
|
|
||
| function powCaptchaInit(form) { | ||
| if (form.classList.contains(READY_CLASS)) { | ||
| return; | ||
| } | ||
|
|
||
| form.classList.add(READY_CLASS); | ||
|
|
||
| // Wait for user interaction to start work | ||
|
|
||
| form.addEventListener("input", () => { powPerformWork(form) }, { once: true }); | ||
|
|
||
| const statusEl = form.querySelector(".pow-captcha-status"); | ||
| statusEl.textContent = "PoW Captcha waiting for interaction."; | ||
| } | ||
|
|
||
| function powPerformWork(form) { | ||
| const challenge = form.querySelector('[name="pow-captcha-challenge"]').value; | ||
| const difficulty = Number(form.querySelector('[name="pow-captcha-difficulty"]').value); | ||
| const hashAlgo = form.querySelector('[name="pow-captcha-hash-algo"]').value; | ||
| const start = Number(form.querySelector('[name="pow-captcha-start"]')?.value ?? 0); | ||
|
|
||
| const statusEl = form.querySelector(".pow-captcha-status"); | ||
| statusEl.textContent = `PoW Captcha doing work (${hashAlgo} x${difficulty}).`; | ||
|
|
||
| // Do the number crunching in a Web Worker for speed and responsiveness | ||
| const worker = new Worker(`${VuFind.path}/themes/root/js/captcha-pow-worker.js`); | ||
|
|
||
| worker.onmessage = (event) => { | ||
| worker.terminate(); | ||
| powResolveWork(form, event.data); | ||
| }; | ||
|
|
||
| worker.onerror = (event) => { | ||
| worker.terminate(); | ||
| powRejectWork(form, event); | ||
| }; | ||
|
|
||
| worker.postMessage({ challenge, difficulty, hashAlgo, start }); | ||
| } | ||
|
|
||
| function powResolveWork(form, { nonce, iters }) { | ||
| const nonceInput = form.querySelector('[name="pow-captcha-nonce"]'); | ||
| nonceInput.value = nonce; | ||
|
|
||
| const statusEl = form.querySelector(".pow-captcha-status"); | ||
| statusEl.textContent = `PoW Captcha: nonce (${nonce}) found after ${iters} iterations.`; | ||
| } | ||
|
|
||
| function powRejectWork(form, event) { | ||
| console.error(event); | ||
| } | ||
|
|
||
| function init() { | ||
| // Find PoW challenges | ||
| for (const pow of document.querySelectorAll(CHALLENGE_SELECTOR)) { | ||
| powCaptchaInit(pow.closest("form")); | ||
| } | ||
|
|
||
| // Listen for future forms | ||
| const observer = new MutationObserver(lookForNewPoWCaptchas); | ||
| observer.observe(document, { childList: true, subtree: true }); | ||
|
|
||
| function lookForNewPoWCaptchas(records, observer) { | ||
| for (const record of records) { | ||
| for (const addedNode of record.addedNodes) { | ||
| if (addedNode instanceof Text) { | ||
| continue; | ||
| } | ||
| if (addedNode.matches(CHALLENGE_SELECTOR)) { | ||
| powCaptchaInit(addedNode.closest("form")); | ||
| } | ||
| for (const pow of document.querySelectorAll(CHALLENGE_SELECTOR)) { | ||
| powCaptchaInit(pow.closest("form")); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return { init }; | ||
| }); |
This file contains hidden or 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,7 @@ | ||
| <?php ?> | ||
| <input type="hidden" name="pow-captcha-challenge" value="<?=$this->captcha->getChallenge() ?>"> | ||
| <input type="hidden" name="pow-captcha-difficulty" value="<?=$this->captcha->getDifficulty() ?>"> | ||
| <input type="hidden" name="pow-captcha-hash-algo" value="<?=$this->captcha->getHashAlgo() ?>"> | ||
| <input type="hidden" name="pow-captcha-start" value="<?=$this->captcha->getStart() ?>"> | ||
| <input type="hidden" name="pow-captcha-nonce" value=""> | ||
| <p class="pow-captcha-status">PoW Captcha not initiated.</p> | ||
crhallberg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.