Skip to content

add cloudflare turnstile protection to support and account deletion forms#164

Merged
vernu merged 2 commits intomainfrom
cloudflare-turnstile
Dec 7, 2025
Merged

add cloudflare turnstile protection to support and account deletion forms#164
vernu merged 2 commits intomainfrom
cloudflare-turnstile

Conversation

@vernu
Copy link
Copy Markdown
Owner

@vernu vernu commented Dec 7, 2025

No description provided.

@vercel
Copy link
Copy Markdown

vercel bot commented Dec 7, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
textbee Ready Ready Preview Comment Dec 7, 2025 5:49pm

Comment on lines +37 to +40
const recentRequestsCount = await this.supportMessageModel.countDocuments({
email: sanitizedDto.email,
createdAt: { $gte: twentyFourHoursAgo },
})

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query object depends on a
user-provided value
.

Copilot Autofix

AI 4 months ago

To fix the issue, we need to ensure that the email field used in the MongoDB query always receives a primitive string value, not a potentially crafted object. The best and safest approach is to explicitly use MongoDB's $eq operator, so that whatever is received from the user (string or object), MongoDB treats it only as a literal value, not as a query operator. This requires directly replacing the query construction on line 37 so that instead of:

email: sanitizedDto.email,

it uses:

email: { $eq: sanitizedDto.email },

No additional imports or methods are required for this change. The fix is strictly limited to the vulnerable line and preserves the semantics of filtering by exact email match, but ensures injection is prevented regardless of input type.

Suggested changeset 1
api/src/support/support.service.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/api/src/support/support.service.ts b/api/src/support/support.service.ts
--- a/api/src/support/support.service.ts
+++ b/api/src/support/support.service.ts
@@ -35,7 +35,7 @@
       // Check rate limit: max 3 requests per 24 hours
       const twentyFourHoursAgo = new Date(Date.now() - 24 * 60 * 60 * 1000)
       const recentRequestsCount = await this.supportMessageModel.countDocuments({
-        email: sanitizedDto.email,
+        email: { $eq: sanitizedDto.email },
         createdAt: { $gte: twentyFourHoursAgo },
       })
 
EOF
@@ -35,7 +35,7 @@
// Check rate limit: max 3 requests per 24 hours
const twentyFourHoursAgo = new Date(Date.now() - 24 * 60 * 60 * 1000)
const recentRequestsCount = await this.supportMessageModel.countDocuments({
email: sanitizedDto.email,
email: { $eq: sanitizedDto.email },
createdAt: { $gte: twentyFourHoursAgo },
})

Copilot is powered by AI and may make mistakes. Always verify output.
isValidObjectId(sanitizedDto.user)
) {
user = await this.userModel.findById(createSupportMessageDto.user)
user = await this.userModel.findById(sanitizedDto.user)

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query object depends on a
user-provided value
.

Copilot Autofix

AI 4 months ago

To mitigate the risk of NoSQL injection, we should ensure that the value used for the _id lookup in Mongoose’s findById or similar queries is interpreted strictly as a literal, and not as a query object. The best practice is to use the $eq operator, so that even if the value is a valid ObjectId string supplied by the user, it will be checked as a literal and not as a potentially malicious query object.

In file api/src/support/support.service.ts

  • Locate the line where user = await this.userModel.findById(sanitizedDto.user).
  • Change this to use .findOne({ _id: { $eq: sanitizedDto.user } }) instead.
  • No additional imports are needed, as this is standard mongoose usage.

Only change the highlighted query, keeping application logic otherwise unchanged.


Suggested changeset 1
api/src/support/support.service.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/api/src/support/support.service.ts b/api/src/support/support.service.ts
--- a/api/src/support/support.service.ts
+++ b/api/src/support/support.service.ts
@@ -56,7 +56,7 @@
         sanitizedDto.user &&
         isValidObjectId(sanitizedDto.user)
       ) {
-        user = await this.userModel.findById(sanitizedDto.user)
+        user = await this.userModel.findOne({ _id: { $eq: sanitizedDto.user } })
       }
 
       // Send confirmation email to user
EOF
@@ -56,7 +56,7 @@
sanitizedDto.user &&
isValidObjectId(sanitizedDto.user)
) {
user = await this.userModel.findById(sanitizedDto.user)
user = await this.userModel.findOne({ _id: { $eq: sanitizedDto.user } })
}

// Send confirmation email to user
Copilot is powered by AI and may make mistakes. Always verify output.
@vernu vernu merged commit d6d52e8 into main Dec 7, 2025
8 of 9 checks passed
@vernu vernu deleted the cloudflare-turnstile branch December 7, 2025 17:52
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.

2 participants