Skip to content

Node.js based forum software built for the modern web

fix: revalidate pending registration policy (#14571)

Sebastian Albrecht9db146c9cfd9
diff --git a/src/controllers/authentication.js b/src/controllers/authentication.js
index 3ab550a5fb..2ec606b947 100644
--- a/src/controllers/authentication.js
+++ b/src/controllers/authentication.js
@@ -72,18 +72,23 @@ async function registerAndLoginUser(req, res, userData) {
return complete;
}
-// POST /register
-authenticationController.register = async function (req, res) {
+async function validateRegistrationPolicy(userData) {
const registrationType = meta.config.registrationType || 'normal';
-
if (registrationType === 'disabled') {
- return res.sendStatus(403);
+ return false;
}
+ if (userData.token || registrationType === 'invite-only' || registrationType === 'admin-invite-only') {
+ await user.verifyInvitation(userData);
+ }
+ return true;
+}
+// POST /register
+authenticationController.register = async function (req, res) {
const userData = req.body;
try {
- if (userData.token || registrationType === 'invite-only' || registrationType === 'admin-invite-only') {
- await user.verifyInvitation(userData);
+ if (!await validateRegistrationPolicy(userData)) {
+ return res.sendStatus(403);
}
user.checkUsernameLength(userData.username);
@@ -114,6 +119,14 @@ authenticationController.register = async function (req, res) {
// POST /register/complete
authenticationController.registerComplete = async function (req, res) {
try {
+ if (
+ req.session.registration?.register === true &&
+ !await validateRegistrationPolicy(req.session.registration)
+ ) {
+ delete req.session.registration;
+ return res.sendStatus(403);
+ }
+
// For the interstitials that respond, execute the callback with the form body
const data = await user.interstitials.get(req, req.session.registration);
const callbacks = data.interstitials.reduce((memo, cur) => {
diff --git a/test/authentication.js b/test/authentication.js
index 4fb9fcb5ad..256a89e254 100644
--- a/test/authentication.js
+++ b/test/authentication.js
@@ -369,6 +369,95 @@ describe('authentication', () => {
assert.equal(body, '[[register:invite.error-invite-only]]');
});
+ describe('pending registrations', () => {
+ let previousConfig;
+
+ before(() => {
+ previousConfig = {
+ registrationType: meta.config.registrationType,
+ registrationApprovalType: meta.config.registrationApprovalType,
+ gdpr_enabled: meta.config.gdpr_enabled,
+ sendValidationEmail: meta.config.sendValidationEmail,
+ };
+ });
+
+ beforeEach(() => {
+ meta.config.registrationType = 'normal';
+ meta.config.registrationApprovalType = 'normal';
+ meta.config.gdpr_enabled = 1;
+ meta.config.sendValidationEmail = 0;
+ });
+
+ after(() => {
+ Object.assign(meta.config, previousConfig);
+ });
+
+ function registrationBody(username) {
+ return {
+ username,
+ email: `${username}@example.org`,
+ password: 'pending-registration-password',
+ 'password-confirm': 'pending-registration-password',
+ };
+ }
+
+ async function parkRegistration(username) {
+ const jar = request.jar();
+ const { response, body } = await helpers.request('post', '/register', {
+ jar,
+ body: registrationBody(username),
+ });
+ assert.strictEqual(response.status, 200);
+ assert.strictEqual(body.next, `${nconf.get('relative_path')}/register/complete`);
+ assert.strictEqual(await user.getUidByUsername(username), null);
+ return jar;
+ }
+
+ async function completeRegistration(jar) {
+ return await helpers.request('post', '/register/complete', {
+ jar,
+ body: {
+ gdpr_agree_data: 'on',
+ gdpr_agree_email: 'on',
+ },
+ redirect: 'manual',
+ });
+ }
+
+ it('should complete if the registration policy still allows it', async () => {
+ const username = `policy${utils.generateUUID().slice(0, 8)}`;
+ const jar = await parkRegistration(username);
+ const { response } = await completeRegistration(jar);
+
+ assert.strictEqual(response.status, 302);
+ assert(await user.getUidByUsername(username));
+ });
+
+ it('should reject a pending registration after registrations are disabled', async () => {
+ const username = `policy${utils.generateUUID().slice(0, 8)}`;
+ const jar = await parkRegistration(username);
+ meta.config.registrationType = 'disabled';
+
+ const { response } = await completeRegistration(jar);
+
+ assert.strictEqual(response.status, 403);
+ assert.strictEqual(await user.getUidByUsername(username), null);
+ });
+
+ it('should reject an uninvited pending registration after invite-only mode is enabled', async () => {
+ const username = `policy${utils.generateUUID().slice(0, 8)}`;
+ const jar = await parkRegistration(username);
+ meta.config.registrationType = 'invite-only';
+
+ const { response } = await completeRegistration(jar);
+
+ assert.strictEqual(response.status, 302);
+ const redirect = new URL(response.headers.location, nconf.get('url'));
+ assert.strictEqual(redirect.searchParams.get('register'), '[[register:invite.error-invite-only]]');
+ assert.strictEqual(await user.getUidByUsername(username), null);
+ });
+ });
+
it('should fail to register if username is falsy or too short', async () => {
const userData = [
{ username: '', password: 'somepassword' },