Skip to content

Commit

Permalink
Compatibility for Ajax forms
Browse files Browse the repository at this point in the history
  • Loading branch information
www-data committed Dec 17, 2019
1 parent b2592b9 commit 90c8371
Show file tree
Hide file tree
Showing 8 changed files with 10,139 additions and 14 deletions.
5 changes: 4 additions & 1 deletion src/Controllers/HiddenCaptchaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ public function getToken(Request $request)
abort(503);
}

if (hash('sha256', $name.csrf_token().'hiddencaptcha') !== $signature) {
$mix = mix('captcha.min.js', '/assets/vendor/hidden-captcha');

if (hash('sha256', $name.csrf_token().$mix.'hiddencaptcha') !== $signature) {
abort(503);
}

// Generate the token
$token = [
'timestamp' => $ts,
Expand Down
10,090 changes: 10,090 additions & 0 deletions src/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/public/assets/vendor/hidden-captcha/captcha.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/public/assets/vendor/hidden-captcha/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"/captcha.min.js": "/captcha.min.js?id=298e069444a012efdc13"
"/captcha.min.js": "/captcha.min.js?id=a6d0f6d9d5c794f7430d"
}
38 changes: 30 additions & 8 deletions src/resources/js/captcha.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
document.addEventListener('DOMContentLoaded', function () {
window.hiddenCaptcha = function () {
let captchas = document.querySelectorAll('input[name="_captcha"');

captchas.forEach(function (captcha) {
for (let i = 0; i < captchas.length; i++) {
let captcha = captchas[i];
if (captcha.getAttribute('value') !== null) {
continue;
}

let csrf = captcha.getAttribute('data-csrf');
let random = captcha.nextElementSibling.getAttribute('name');
let src = document.getElementById('captcha-script').getAttribute('src');

sha256(random+csrf+'hiddencaptcha').then(function (hash) {
sha256(random + csrf + src + 'hiddencaptcha').then(function (hash) {
var xhr = new XMLHttpRequest();
xhr.open('POST', "/captcha-token");
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.setRequestHeader("X-CSRF-TOKEN", csrf);
xhr.setRequestHeader("X-SIGNATURE", hash);

xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
let json = JSON.parse(xhr.responseText);
Expand All @@ -20,11 +25,26 @@ document.addEventListener('DOMContentLoaded', function () {
}
};

xhr.send('name='+random);
xhr.send('name=' + random);
})
}
}

// Fix for MS Edge
if (typeof TextEncoder === 'undefined') {
var TextEncoder = function TextEncoder()
{}

});
});
TextEncoder.prototype.encode = function (s) {
const e = new Uint8Array(s.length);

for (let i = 0; i < s.length; i += 1) {
e[i] = s.charCodeAt(i);
}

return e;
}
}

async function sha256(message)
{
Expand All @@ -33,4 +53,6 @@ async function sha256(message)
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map(b => ('00' + b.toString(16)).slice(-2)).join('');
return hashHex;
}
}

window.hiddenCaptcha();
10 changes: 9 additions & 1 deletion src/views/captcha.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<input type="hidden" name="_captcha" data-csrf="{{ csrf_token() }}" /><input type="hidden" name="{{ $random }}" />
@if(!defined('LOAD_HIDDEN_CAPTCHA'))
<script src="{{ mix('captcha.min.js', '/assets/vendor/hidden-captcha') }}"></script>
<script>
if(document.getElementById('captcha-script') === null) {
var s = document.createElement('script');
s.id = "captcha-script";
s.src = "{{ mix('captcha.min.js', '/assets/vendor/hidden-captcha') }}";
document.head.appendChild(s);
}
</script>
@php(define('LOAD_HIDDEN_CAPTCHA', true))
@endif
<script>if(typeof hiddenCaptcha !== 'undefined') { hiddenCaptcha() }</script>
4 changes: 3 additions & 1 deletion tests/CaptchaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ public function testHiddenCaptcha()
$csrf = $m[1];
$random = $m[2];

$mix = mix('captcha.min.js', '/assets/vendor/hidden-captcha');

$response = $this->post('/captcha-token', ['name' => $random], [
'X-SIGNATURE' => hash('sha256', $random.$csrf.'hiddencaptcha'),
'X-SIGNATURE' => hash('sha256', $random.$csrf.$mix.'hiddencaptcha'),
])->content();

$json = json_decode($response);
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected function getPackageProviders($app)
protected function getPackageAliases($app)
{
return [
'HiddenCaptcha' => \SebastienHeyd\HiddenCaptcha\Facade::class,
'HiddenCaptcha' => \SebastienHeyd\HiddenCaptcha\Facades\HiddenCaptcha::class,
];
}
}

0 comments on commit 90c8371

Please sign in to comment.