Skip to content

Fix: Validate country field against numeric input (Issue #1301) - #1435

Open
sahare77 wants to merge 1 commit into
Canopus-Labs:mainfrom
sahare77:fix/1301-country-validation
Open

Fix: Validate country field against numeric input (Issue #1301)#1435
sahare77 wants to merge 1 commit into
Canopus-Labs:mainfrom
sahare77:fix/1301-country-validation

Conversation

@sahare77

@sahare77 sahare77 commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Summary of What Has Been Done

The profile form contains a "Country" input field that lacked strict validation on the backend. Users could submit arbitrary numeric data or special characters, which bypassed the schema and corrupted the database.

Changes Made

  • Modified �ackend/controllers/authController.js in the updateUserProfile function.
  • Added strict regex validation /^[a-zA-Z\s-]+$/ to the country field to ensure only valid alphabetical strings (and hyphens) are accepted.

Impact it Made

  • Greatly improves database data integrity.
  • Prevents errors when calling third-party APIs that rely on strict country codes.

Closes #1301

Updates updateUserProfile to reject invalid country values before database operations. The validation accepts alphabetic characters, spaces, and hyphens. Numeric and special-character values return HTTP 400.

@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

updateUserProfile now validates non-empty country values before database lookup or updates. Invalid values return HTTP 400 responses.

Changes

Profile validation

Layer / File(s) Summary
Country input validation
backend/controllers/authController.js
Non-empty country values now accept only letters, spaces, and hyphens. Invalid values return HTTP 400 before database operations.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Possibly related issues

Possibly related PRs

Suggested reviewers: karanunique, suhaniiz, prishajain64

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The change rejects numeric input but does not enforce the supported two-letter country codes required by issue #1301. Validate country against the supported two-letter country-code list and return an error for unsupported values.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the country-field validation change and references linked issue #1301.
Out of Scope Changes check ✅ Passed The validation change concerns the country field and is related to the linked issue, with no unrelated changes shown.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@backend/controllers/authController.js`:
- Around line 431-434: Update the country validation in the relevant auth
controller flow to normalize the input and require membership in the supported
two-letter country-code allowlist, rather than accepting arbitrary alphabetic
names or punctuation. Perform this validation before assigning user.country, and
preserve the existing 400 response for unsupported values.
- Around line 431-434: Update the country validation in the auth controller to
require that country is a string, trim it, and reject the normalized value when
empty or when it fails the existing countryRegex validation. Assign the
normalized trimmed string to user.country rather than the original input, while
preserving the current invalid-request response.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 986d77f7-eca4-4b67-9583-4681df851370

📥 Commits

Reviewing files that changed from the base of the PR and between 8acb5b8 and 8aad334.

📒 Files selected for processing (1)
  • backend/controllers/authController.js

Comment on lines +431 to +434
if (country !== undefined && country !== "") {
const countryRegex = /^[a-zA-Z\s\-]+$/;
if (!countryRegex.test(country)) {
return res.status(400).json({ success: false, message: "Invalid country name provided" });

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Validate supported two-letter country codes, not arbitrary names.

The regex accepts values such as "United States", "US-", and "ZZ". The PR objective states that downstream APIs expect supported two-letter country codes. This validation can still persist values that cause invalid API calls. Normalize the value and validate it against the supported country-code allowlist before assigning user.country at Line 447.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@backend/controllers/authController.js` around lines 431 - 434, Update the
country validation in the relevant auth controller flow to normalize the input
and require membership in the supported two-letter country-code allowlist,
rather than accepting arbitrary alphabetic names or punctuation. Perform this
validation before assigning user.country, and preserve the existing 400 response
for unsupported values.

🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/usr/bin/env bash
set -euo pipefail

node <<'NODE'
const countryRegex = /^[a-zA-Z\s\-]+$/;

for (const value of [false, ["US"], "   ", "US"]) {
  console.log(JSON.stringify(value), countryRegex.test(value));
}
NODE

Repository: Canopus-Labs/PrepPilot

Length of output: 204


🏁 Script executed:

#!/usr/bin/env bash
set -euo pipefail

file="backend/controllers/authController.js"
printf '%s\n' '--- relevant controller code ---'
sed -n '400,470p' "$file"

printf '%s\n' '--- country references in the controller ---'
rg -n -C 3 '\bcountry\b' "$file"

Repository: Canopus-Labs/PrepPilot

Length of output: 3964


Require a string and reject blank whitespace.

countryRegex.test(country) coerces non-string values, and the controller assigns country directly to user.country. Require a string, trim it, reject an empty normalized value, validate it, and assign the normalized string.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@backend/controllers/authController.js` around lines 431 - 434, Update the
country validation in the auth controller to require that country is a string,
trim it, and reject the normalized value when empty or when it fails the
existing countryRegex validation. Assign the normalized trimmed string to
user.country rather than the original input, while preserving the current
invalid-request response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Country field accepts numeric input instead of validating names

1 participant