Skip to content
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

Anonimization content validation #18

Open
wants to merge 3 commits into
base: f24
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added node_modules/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions node_modules/nodebb-theme-aawaa
Submodule nodebb-theme-aawaa added at 71a76e
28 changes: 24 additions & 4 deletions public/src/client/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,38 @@
const password_confirm_notify = $('#password-confirm-notify');
password_notify.text('');
password_confirm_notify.text('');

Check failure on line 152 in public/src/client/register.js

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed
try {
// Check password against strength requirements
if (!/[a-z]/.test(password)) {
throw new Error('[user:password-no-lowercase]');
}
if (!/[A-Z]/.test(password)) {
throw new Error('[user:password-no-uppercase]');
}
if (!/[0-9]/.test(password)) {
throw new Error('[user:password-no-number]');
}
if (!/[\W_]/.test(password)) {
throw new Error('[user:password-no-special-character]');
}
if (password.length < 8) {
throw new Error('[user:password-too-short]');
}

Check failure on line 170 in public/src/client/register.js

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed
utils.assertPasswordValidity(password, zxcvbn);

Check failure on line 172 in public/src/client/register.js

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed
if (password === $('#username').val()) {
throw new Error('[[user:password-same-as-username]]');
throw new Error('[user:password-same-as-username]');
}


Check failure on line 176 in public/src/client/register.js

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed
// If all validations pass
showSuccess(passwordInput, password_notify, successIcon);
} catch (err) {
showError(passwordInput, password_notify, err.message);
}


Check failure on line 182 in public/src/client/register.js

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed
// Confirm password match
if (password !== password_confirm && password_confirm !== '') {
showError(passwordInput, password_confirm_notify, '[[user:change-password-error-match]]');
}
Expand Down
6 changes: 6 additions & 0 deletions public/src/modules/quickreply.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,17 @@
}

const replyMsg = components.get('topic/quickreply/text').val();
const anonCheckbox = document.getElementById('anonymousCheckbox').checked ? 'true' : 'false';

Check failure on line 62 in public/src/modules/quickreply.js

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed
const replyData = {
tid: ajaxify.data.tid,
handle: undefined,
content: replyMsg,
anon: anonCheckbox,
};

console.log('Reply data being sent:', replyData);

const replyLen = replyMsg.length;
if (replyLen < parseInt(config.minimumPostLength, 10)) {
return alerts.error('[[error:content-too-short, ' + config.minimumPostLength + ']]');
Expand Down
34 changes: 33 additions & 1 deletion src/api/topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,43 @@ topicsAPI.create = async function (caller, data) {
return result.topicData;
};

const MIN_POST_LENGTH = 20; // Minimum content length
const MAX_POST_LENGTH = 500; // Maximum content length
const SENSITIVE_WORDS = ['badword1', 'badword2', 'offensive']; // Replace with actual sensitive words

function isValidContent(content) {
// Check if content length is within the allowed range
if (content.length < MIN_POST_LENGTH || content.length > MAX_POST_LENGTH) {
throw new Error(`Post length must be between ${MIN_POST_LENGTH} and ${MAX_POST_LENGTH} characters.`);
}

// Check for sensitive words
const containsSensitiveWord = SENSITIVE_WORDS.some(word => content.includes(word));
if (containsSensitiveWord) {
throw new Error('Post contains sensitive content.');
}

// If both checks pass, return true
return true;
}

// Integrating the content verification into your reply function:
topicsAPI.reply = async function (caller, data) {
if (!data || !data.tid || (meta.config.minimumPostLength !== 0 && !data.content)) {
throw new Error('[[error:invalid-data]]');
}

// Validate post content length and sensitive words
isValidContent(data.content);

const payload = { ...data };

if (data.anon === 'true') {
payload.username = 'Anonymous User';
payload.userslug = null;
console.log('Anonymous post detected, making it anonymous');
}

apiHelpers.setDefaultPostData(caller, payload);

await meta.blacklist.test(caller.ip);
Expand All @@ -96,7 +128,7 @@ topicsAPI.reply = async function (caller, data) {
return await posts.addToQueue(payload);
}

const postData = await topics.reply(payload); // postData seems to be a subset of postObj, refactor?
const postData = await topics.reply(payload);
const postObj = await posts.getPostSummaryByPids([postData.pid], caller.uid, {});

const result = {
Expand Down
11 changes: 11 additions & 0 deletions src/controllers/write/topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@
Topics.reply = async (req, res) => {
const id = await lockPosting(req, '[[error:already-posting]]');
try {
const isAnonymous = req.body.anon;
console.log("Anonymous flag received:", isAnonymous);

Check failure on line 35 in src/controllers/write/topics.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 8 spaces

Check failure on line 35 in src/controllers/write/topics.js

View workflow job for this annotation

GitHub Actions / test

Strings must use singlequote
let replyData = { ...req.body, tid: req.params.tid };

Check failure on line 36 in src/controllers/write/topics.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 8 spaces

Check failure on line 36 in src/controllers/write/topics.js

View workflow job for this annotation

GitHub Actions / test

'replyData' is never reassigned. Use 'const' instead
if (isAnonymous) {
console.log("Post is anonymous. Modifying the username and userslug.");
replyData.username = 'Anonymous User';
replyData.userslug = null;
} else {
console.log("Post is not anonymous.");
}
console.log("Final reply data being sent:", replyData);
const payload = await api.topics.reply(req, { ...req.body, tid: req.params.tid });
helpers.formatApiResponse(200, res, payload);
} finally {
Expand Down
Loading