+
Skip to content

Sanitize partner websites #2599

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

Merged
merged 1 commit into from
Jul 1, 2025
Merged

Sanitize partner websites #2599

merged 1 commit into from
Jul 1, 2025

Conversation

TWilson023
Copy link
Collaborator

@TWilson023 TWilson023 commented Jul 1, 2025

Builds on the logic + script of #2585 to sanitize websites as well

Summary by CodeRabbit

  • New Features

    • Website URLs entered or pasted in partner forms are now automatically sanitized for consistency and security.
    • A script is available to clean up existing partner website URLs in the database.
  • Bug Fixes

    • Pasted website links are now normalized, removing unnecessary query parameters and ensuring proper formatting.

@TWilson023 TWilson023 requested a review from steven-tey July 1, 2025 21:17
Copy link

vercel bot commented Jul 1, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
dub ✅ Ready (Inspect) Visit Preview Jul 1, 2025 9:43pm

Copy link
Contributor

coderabbitai bot commented Jul 1, 2025

Walkthrough

This change introduces website URL sanitization for partner records. It adds a sanitizeWebsite utility, applies this sanitization in the online presence update schema and form, and provides a script to retroactively sanitize existing partner website URLs in the database.

Changes

File(s) Change Summary
apps/web/lib/social-utils.ts Added sanitizeWebsite function to normalize and clean website URLs.
apps/web/lib/actions/partners/update-online-presence.ts Applied sanitizeWebsite as a transform on the website field in the update schema.
apps/web/ui/partners/online-presence-form.tsx Added paste handler to sanitize website URLs on user input using sanitizeWebsite.
apps/web/scripts/partners/sanitize-websites.ts New script to sanitize and update partner website URLs in the database using sanitizeWebsite.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant OnlinePresenceForm
    participant sanitizeWebsite
    participant Database

    User->>OnlinePresenceForm: Paste website URL
    OnlinePresenceForm->>sanitizeWebsite: Sanitize pasted URL
    sanitizeWebsite-->>OnlinePresenceForm: Sanitized URL
    OnlinePresenceForm->>Database: Save sanitized website URL
Loading
sequenceDiagram
    participant Script
    participant Database
    participant sanitizeWebsite

    Script->>Database: Query partners with unsanitized websites
    loop For each partner
        Script->>sanitizeWebsite: Sanitize website URL
        sanitizeWebsite-->>Script: Sanitized URL
        Script->>Database: Update partner record if changed
    end
Loading

Possibly related PRs

Suggested reviewers

  • devkiran
  • steven-tey

Poem

A rabbit hopped through fields of code,
Sanitizing websites as it strode.
URLs trimmed and made pristine,
No stray queries left unseen.
With every hop, the data’s bright—
Now partner links are clean and right!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
apps/web/scripts/partners/sanitize-websites.ts (1)

6-58: Consider adding error handling for database operations.

The script performs database updates in a loop without error handling. Consider wrapping the update operation in try-catch to handle potential database errors gracefully:

 for (const { id, website } of partners) {
   let updatedWebsite = sanitizeWebsite(website);
   const needsUpdate = updatedWebsite !== website;
 
   if (needsUpdate) {
     updatedPartners.push({
       id,
       website: updatedWebsite,
     });
 
+    try {
       await prisma.partner.update({
         where: { id },
         data: { website: updatedWebsite },
       });
+    } catch (error) {
+      console.error(`Failed to update partner ${id}:`, error);
+      // Remove from updatedPartners array on failure
+      updatedPartners.pop();
+    }
   }
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 897844c and d35bfb4.

📒 Files selected for processing (4)
  • apps/web/lib/actions/partners/update-online-presence.ts (2 hunks)
  • apps/web/lib/social-utils.ts (1 hunks)
  • apps/web/scripts/partners/sanitize-websites.ts (1 hunks)
  • apps/web/ui/partners/online-presence-form.tsx (3 hunks)
🧰 Additional context used
🧠 Learnings (1)
apps/web/scripts/partners/sanitize-websites.ts (1)
Learnt from: devkiran
PR: dubinc/dub#2177
File: apps/web/lib/api/links/bulk-create-links.ts:66-84
Timestamp: 2025-06-06T07:59:03.120Z
Learning: In apps/web/lib/api/links/bulk-create-links.ts, the team accepts the risk of potential undefined results from links.find() operations when building invalidLinks arrays, because existing links are fetched from the database based on the input links, so matches are expected to always exist.
🧬 Code Graph Analysis (2)
apps/web/scripts/partners/sanitize-websites.ts (1)
apps/web/lib/social-utils.ts (1)
  • sanitizeWebsite (48-64)
apps/web/lib/actions/partners/update-online-presence.ts (1)
apps/web/lib/social-utils.ts (1)
  • sanitizeWebsite (48-64)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
🔇 Additional comments (6)
apps/web/lib/social-utils.ts (1)

48-64: LGTM! Well-implemented URL sanitization function.

The function correctly handles edge cases with proper null/undefined checks, adds HTTPS protocol when missing, removes query parameters, and gracefully handles invalid URLs. The implementation is robust and follows good practices.

apps/web/lib/actions/partners/update-online-presence.ts (2)

7-7: LGTM! Proper import of sanitization function.


18-18: LGTM! Correct integration of website sanitization in schema validation.

The sanitizeWebsite transform is properly applied to ensure consistent URL normalization during validation.

apps/web/ui/partners/online-presence-form.tsx (3)

5-9: LGTM! Proper import of sanitization utilities.


131-142: LGTM! Well-implemented paste handler for website sanitization.

The callback correctly sanitizes pasted URLs and only prevents default behavior when sanitization succeeds. The useCallback hook is properly used with the correct dependency.


188-188: LGTM! Proper integration of paste handler.

The onPasteWebsite handler is correctly applied to provide immediate URL sanitization on paste events.

Comment on lines +31 to +50
for (const { id, website } of partners) {
let updatedWebsite = sanitizeWebsite(website);

const needsUpdate = updatedWebsite !== website;

if (needsUpdate) {
updatedPartners.push({
id,
website: updatedWebsite,
});

await prisma.partner.update({
where: {
id,
},
data: {
website: updatedWebsite,
},
});
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Handle the case where sanitization returns null for existing websites.

If sanitizeWebsite returns null for an existing website (e.g., invalid URL), the script will update the database to set the website field to null. This might not be the intended behavior.

Consider skipping the update or logging these cases separately:

 for (const { id, website } of partners) {
   let updatedWebsite = sanitizeWebsite(website);
 
+  // Skip if sanitization failed (returned null for existing website)
+  if (website && !updatedWebsite) {
+    console.log(`Skipping invalid website for partner ${id}: ${website}`);
+    continue;
+  }
+
   const needsUpdate = updatedWebsite !== website;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
for (const { id, website } of partners) {
let updatedWebsite = sanitizeWebsite(website);
const needsUpdate = updatedWebsite !== website;
if (needsUpdate) {
updatedPartners.push({
id,
website: updatedWebsite,
});
await prisma.partner.update({
where: {
id,
},
data: {
website: updatedWebsite,
},
});
}
for (const { id, website } of partners) {
let updatedWebsite = sanitizeWebsite(website);
// Skip if sanitization failed (returned null for existing website)
if (website && !updatedWebsite) {
console.log(`Skipping invalid website for partner ${id}: ${website}`);
continue;
}
const needsUpdate = updatedWebsite !== website;
if (needsUpdate) {
updatedPartners.push({
id,
website: updatedWebsite,
});
await prisma.partner.update({
where: {
id,
},
data: {
website: updatedWebsite,
},
});
}
🤖 Prompt for AI Agents
In apps/web/scripts/partners/sanitize-websites.ts around lines 31 to 50, the
code updates the partner's website even if sanitizeWebsite returns null, which
may unintentionally set the website field to null in the database. Modify the
logic to check if updatedWebsite is null before proceeding with the update; if
it is null, skip the update or log the case separately to avoid overwriting
valid data with null.

@steven-tey steven-tey merged commit a03c95a into main Jul 1, 2025
9 checks passed
@steven-tey steven-tey deleted the sanitize-website branch July 1, 2025 22:42
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
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载