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

Bug: Passing relative Path to StaticFilesConfig fails to join correctly but passes validation. #1256

Closed
Maxyme opened this issue Mar 1, 2023 · 2 comments · Fixed by #1257
Assignees
Labels
Bug 🐛 This is something that is not working as expected

Comments

@Maxyme
Copy link

Maxyme commented Mar 1, 2023

Passing a relative path, as str or Path object, generates a NotFoundException when resolving paths using the StaticFilesConfig class when it should be resolved correctly.

This is interesting, as the path will first be validated when instantiating the class by Pydantic with the DirectoryPath type (and a Path object created), but later when joining the directory and file_path it fails to resolve it correctly:
in base.py/get_fs_info [line 57]:

joined_path = join(directory, file_path)  # noqa: PL118

How to reproduce:

This returns a NotFoundException: "no file or directory match the path was found"

static_path = Path("../static")
static = StaticFilesConfig(path="/ui", directories=[static_path])

While this works fine:

static_path = Path("../static").resolve() # or .absolute()
static = StaticFilesConfig(path="/ui", directories=[static_path])

A quick solution for this problem is to resolve() all given directories in StaticFiles:

class StaticFiles:
    """ASGI App that handles file sending."""
    def __init__(
        self,
        directories: Sequence["PathType"],
    ) -> None:
        """Initialize the Application.
        self.directories = [d.resolve() for d in directories]  # <-- here

I think there may be a better solution using Pathlib joining instead of os.join that might involve more refactoring.

@Maxyme Maxyme added Bug 🐛 This is something that is not working as expected Triage Required 🏥 This requires triage labels Mar 1, 2023
@Maxyme Maxyme changed the title Bug: Passing relative Path to Bug: Passing relative Path to StaticFilesConfig fails to join correctly but passes validation. Mar 1, 2023
@Goldziher
Copy link
Contributor

Hi, thanks for the report. Can you check the main branch? We updated the configs there.

@Maxyme
Copy link
Author

Maxyme commented Mar 1, 2023

@Goldziher thanks for the reply. I just checked on main and it now works when passing a relative Path object, but not a str. (ie. Path("../static") works but not "../static")

@Goldziher Goldziher self-assigned this Mar 1, 2023
@Goldziher Goldziher removed the Triage Required 🏥 This requires triage label Mar 1, 2023
@Goldziher Goldziher linked a pull request Mar 1, 2023 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🐛 This is something that is not working as expected
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants