Skip to content

Commit d5c8610

Browse files
authored
Plug a hole (#1881)
* If not running captcha it's still there *(even prior to the refactor... saw it in a .user.js a while back)* * Fix non-captcha'd site forks * Refocus on SITEKEY instead of SECRET ... don't want accidental exposure from other devs. Post #944 #1867 NOTE: * Special thanks to datinginfos *(a spammer)* for confirming. ;) :) Auto-merge
1 parent 72734c5 commit d5c8610

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

controllers/auth.js

+25-4
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ exports.preauth = function (aReq, aRes, aNext) {
8484
var authedUser = aReq.session.user;
8585

8686
var username = aReq.body.username;
87-
var SECRET = process.env.HCAPTCHA_SECRET_KEY;
8887
var SITEKEY = process.env.HCAPTCHA_SITE_KEY;
8988

9089
if (!authedUser) {
@@ -115,19 +114,28 @@ exports.preauth = function (aReq, aRes, aNext) {
115114
return;
116115
}
117116

118-
if (aUser || !SECRET) {
117+
if (aUser) {
119118
// Ensure that casing is identical so we still have it, correctly, when they
120119
// get back from authentication
121120
aReq.body.username = aUser.name;
122121

123-
// Skip captcha for known individual and not implemented
122+
if (aUser) {
123+
aReq.knownUser = true;
124+
}
125+
126+
// Skip captcha for known individual
124127
exports.auth(aReq, aRes, aNext);
125128
} else {
126129
// Match cleansed name and this is the casing they have chosen
127130
aReq.body.username = username;
128131

129132
// Validate captcha for unknown individual
130-
aNext();
133+
if (!SITEKEY) {
134+
// Skip captcha for not implemented
135+
exports.auth(aReq, aRes, aNext);
136+
} else {
137+
aNext();
138+
}
131139
}
132140
});
133141
} else {
@@ -197,6 +205,10 @@ exports.auth = function (aReq, aRes, aNext) {
197205
// Save redirect url from the form submission on the session
198206
aReq.session.redirectTo = aReq.body.redirectTo || getRedirect(aReq);
199207

208+
// Save the known user on the session and remove
209+
aReq.session.knownUser = aReq.knownUser;
210+
delete aReq.knownUser;
211+
200212
// Save the token from the captcha on the session and remove from body
201213
if (captchaToken) {
202214
aReq.session.captchaToken = captchaToken;
@@ -301,8 +313,17 @@ exports.callback = function (aReq, aRes, aNext) {
301313
var strategy = aReq.params.strategy;
302314
var username = aReq.session.username;
303315
var newstrategy = aReq.session.newstrategy;
316+
var knownUser = aReq.session.knownUser;
317+
var captchaToken = aReq.session.captchaToken;
304318
var strategyInstance = null;
305319
var doneUri = aReq.session.user ? '/user/preferences' : '/';
320+
var SITEKEY = process.env.HCAPTCHA_SITE_KEY;
321+
322+
323+
if (SITEKEY && !knownUser && !captchaToken) {
324+
aRes.redirect('/login?fail');
325+
return;
326+
}
306327

307328
// The callback was called improperly or sesssion expired
308329
if (!strategy || !username) {

controllers/index.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ exports.register = function (aReq, aRes) {
218218
var authedUser = aReq.session.user;
219219
var tasks = [];
220220

221-
var SECRET = process.env.HCAPTCHA_SECRET_KEY;
222221
var SITEKEY = process.env.HCAPTCHA_SITE_KEY;
223222

224223
// If already logged in, go back.
@@ -227,8 +226,7 @@ exports.register = function (aReq, aRes) {
227226
return;
228227
}
229228

230-
options.hasCaptcha = (SECRET ? true : false);
231-
options.hcaptchaSiteKey = (SITEKEY ? SITEKEY : '');
229+
options.hasCaptcha = (SITEKEY ? SITEKEY : '');
232230

233231
options.redirectTo = getRedirect(aReq);
234232

@@ -262,10 +260,9 @@ exports.register = function (aReq, aRes) {
262260
//
263261

264262
Strategy.find({}, function (aErr, aAvailableStrategies) {
265-
var SECRET = process.env.HCAPTCHA_SECRET_KEY;
266263
var SITEKEY = process.env.HCAPTCHA_SITE_KEY;
267264
var defaultCSP = ' \'self\'';
268-
var captchaCSP = (SECRET ? ' hcaptcha.com *.hcaptcha.com' : '');
265+
var captchaCSP = (SITEKEY ? ' hcaptcha.com *.hcaptcha.com' : '');
269266

270267
if (aErr || !aAvailableStrategies) {
271268
statusCodePage(aReq, aRes, aNext, {
@@ -283,7 +280,7 @@ exports.register = function (aReq, aRes) {
283280
});
284281
});
285282

286-
options.hasCaptcha = (SECRET ? true : false);
283+
options.hasCaptcha = (SITEKEY ? SITEKEY : '');
287284

288285
options.nonce = crypto.randomBytes(512).toString('hex');
289286
defaultCSP += ' \'nonce-' + options.nonce + '\'';

routes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ var rateLimit = require('express-rate-limit');
99
var MongoStore = require('rate-limit-mongo');
1010
var exec = require('child_process').exec;
1111
var hcaptcha = require('express-hcaptcha');
12-
var SECRET = process.env.HCAPTCHA_SECRET_KEY;
1312
var SITEKEY = process.env.HCAPTCHA_SITE_KEY;
13+
var SECRET = process.env.HCAPTCHA_SECRET_KEY;
1414

1515
//
1616
var main = require('./controllers/index');

views/pages/loginPage.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ <h3>
4141
<div style="width: 100%;">
4242
<div class="input-group-addon">
4343
{{#hasCaptcha}}
44-
<div class="h-captcha" data-sitekey="{{hcaptchaSiteKey}}"></div>
44+
<div class="h-captcha" data-sitekey="{{hasCaptcha}}"></div>
4545
{{/hasCaptcha}}
4646
<p>
4747
<ul class="nav nav-pills nav-justified">

0 commit comments

Comments
 (0)