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

fix: enhance background URL processing to support Windows file paths #1604

Closed
wants to merge 1 commit into from

Conversation

SamimAB
Copy link

@SamimAB SamimAB commented Dec 23, 2024

In processBackgroundUrls, made two changes -

  1. replace backslashes with "\\", so css-tree reads it properly
  2. added a check for windows like file paths /^[a-zA-Z]:\\/.test(originalUrl)

I tried a few other fixes but this one works the best with least changes to the codebase. Hopefully this is helpful.

@CLAassistant
Copy link

CLAassistant commented Dec 23, 2024

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

coderabbitai bot commented Dec 23, 2024

Walkthrough

The pull request introduces modifications to the processBackgroundUrls function in the frontend/app/app-bg.tsx file, specifically focusing on URL handling within CSS background properties. The changes enhance the function's capability to process URLs by adding improved regex replacement for backslash escaping and expanding URL validation to support Windows-style absolute paths. The modification allows the function to handle a broader range of URL formats while maintaining its existing error handling and validation mechanisms. The core logic of the function remains consistent, with the primary goal of safely processing and encoding background URLs in the application's frontend component.


🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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. (Beta)
  • @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: 0

🧹 Nitpick comments (1)
frontend/app/app-bg.tsx (1)

57-57: Consider adding an additional check for UNC paths or other edge cases.
While matching /^[a-zA-Z]:\\/.test(originalUrl) correctly identifies drive-letter-based paths, some Windows paths use UNC syntax (e.g., “\server\share”). If such cases need to be supported, consider updating this regex. Otherwise, it might be useful to document the reason for excluding UNC paths.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 53a64fc and d52ae8c.

📒 Files selected for processing (1)
  • frontend/app/app-bg.tsx (2 hunks)
🔇 Additional comments (1)
frontend/app/app-bg.tsx (1)

29-29: Confirm we are not over-escaping backslashes.
Replacing single backslashes with double backslashes ensures the CSS parser handles Windows paths correctly. However, if upstream code might already escape these backslashes, this replacement could lead to unintended quadruple backslashes. Please verify (via tests or by auditing upstream path handling) that we do not unintentionally produce over-escaped paths.

✅ Verification successful

Let me gather information about how the cssText is handled before reaching this point.


Let me search for how cssText is processed before reaching this line to understand if there's any prior backslash escaping.


Let me check how the bgAttr value is set and if there's any prior processing of backslashes before it reaches processBackgroundUrls.


Let me check how the CSS parsing is handled and if there are any tests related to backslash handling.


Let me check the complete implementation of the background URL processing and where the CSS text originates from.


Based on the code analysis, I can now provide a definitive response about the backslash escaping:

The backslash escaping is correctly implemented and not causing over-escaping.

The code shows that:

  1. The bgAttr value comes directly from the tab's metadata without any prior escaping
  2. The processBackgroundUrls function is the only place where backslash escaping occurs
  3. The CSS parser (parseCSS) requires backslashes to be escaped to handle Windows paths correctly
  4. The escaping is done only once with cssText.replace(/\\/g, '\\\\') before parsing
  5. The CSS parser then correctly interprets these escaped backslashes when processing URLs

The escaping is necessary because:

  • Windows paths contain single backslashes (e.g., C:\path\to\file.jpg)
  • CSS requires backslashes in URLs to be escaped
  • The CSS parser needs to see \\ to interpret it as a single \ in the final URL
🏁 Scripts executed

The following scripts were executed for the analysis:

Error: No shell scripts found


Script:

#!/bin/bash
# Search for assignments or declarations of cssText
rg "cssText" -B 3 -A 3

# Look for any path handling or backslash escaping logic
rg "\\\\|replace.*\\\\" -B 3 -A 3

Length of output: 67064


Script:

#!/bin/bash
# Look for the function definition and usage of processBackgroundUrls
ast-grep --pattern 'function processBackgroundUrls($$$) {
  $$$
}'

# Look for where cssText is assigned/modified before reaching processBackgroundUrls
rg -B 5 -A 5 "processBackgroundUrls"

Length of output: 1550


Script:

#!/bin/bash
# Look for where bg meta is set/modified
rg "meta.*bg.*=" -B 5 -A 5

# Look for any backslash processing in the codebase related to background or meta
rg -B 3 -A 3 "bg.*replace.*\\\\" 
rg -B 3 -A 3 "meta.*replace.*\\\\"

Length of output: 1762


Script:

#!/bin/bash
# Look for CSS parsing tests and backslash handling
rg -B 3 -A 3 "parseCSS.*test" 

# Check the CSS parser implementation
ast-grep --pattern 'parseCSS($$$) {
  $$$
}'

# Look for any test cases involving backslashes in background URLs
rg -B 3 -A 3 "background.*\\\\.*test"

Length of output: 116


Script:

#!/bin/bash
# Get the full implementation of app-bg.tsx
cat frontend/app/app-bg.tsx

# Look for where background URLs are set in the codebase
rg -B 5 -A 5 "url\(['\"]?[^'\")]+['\"]?\)"

Length of output: 69011

@esimkowitz esimkowitz linked an issue Dec 23, 2024 that may be closed by this pull request
3 tasks
@SamimAB
Copy link
Author

SamimAB commented Dec 24, 2024

@esimkowitz the testdriver ai onboarding failed. does it require any actions from my side?

@esimkowitz
Copy link
Member

@ianjennings @chottuthejimmy could one of you take a look? Doesn’t seem like the job is getting queued

@chottuthejimmy
Copy link
Contributor

@ianjennings @chottuthejimmy could one of you take a look? Doesn’t seem like the job is getting queued

Sure give a moment

@chottuthejimmy
Copy link
Contributor

chottuthejimmy commented Dec 24, 2024

@esimkowitz yes it wasn't triggered, I see nothing at our backend.
While I look into it could u re-run the job for me please

@chottuthejimmy
Copy link
Contributor

It is working fine on the main branch

@esimkowitz
Copy link
Member

@chottuthejimmy okay queued to rerun. Yeah I've noticed that the command to queue the job to your service fails intermittently. It seems most of the recent failures have been because of this.

@chottuthejimmy
Copy link
Contributor

Yeah, I mean I am trying to figure it out.
One, the main branch has been scheduled to run everyday at 9 pm, so that gives us a benchmark to evaluate and make sure everything's fine, which atm seems ok.
Two, the tests will automatically fail if you merge / close the PR before the test is finished, ya ik you know it, just wanted to make a second point 😅

That said will get something to you before tmr.
Merry Christmas and thanks for your service 🫡

@ianjennings
Copy link
Contributor

@ianjennings @chottuthejimmy could one of you take a look? Doesn’t seem like the job is getting queued

image

It seems like the GitHub secret key is not being set during the run.

According to this post it depends who triggers the run. @SamimAB may not have permissions to use GitHub secrets. Try adding them to the org?

We can definitely make our errors more apparent, will make a note of that.

@esimkowitz
Copy link
Member

Let's move this discussion to #1621

@sawka
Copy link
Member

sawka commented Dec 27, 2024

hi, really appreciate that you brought this issue to our attention! i ended up just making change to the wsh setbg code to properly escape windows filepaths at the time when we set the metadata.

#1636

so now you should be able to do something like this in powershell (note if using bash or a unix shell you'd also need to escape the backslashes in the shell as well):

wsh setbg c:\foo\bar.jpg

the problem with blanket replacing \ => \ everywhere in the CSS is it could have unintended consequences. it would also affect urls that were already properly encoded with backslashes.

@sawka sawka closed this Dec 27, 2024
@SamimAB
Copy link
Author

SamimAB commented Dec 27, 2024

@sawka makes sense. appreciate it. will check it out.

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]: setbg is not working for images in Windows 10
6 participants