Opened 9 years ago

Closed 9 months ago

Last modified 9 months ago

#25595 closed Bug (fixed)

Invalid regexp in URLValidator can't handle file:// schemes

Reported by: Marcin Nowak Owned by: Adam Zapletal
Component: Core (Other) Version: 1.8
Severity: Normal Keywords:
Cc: Adam Zapletal Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Regexp does not allow to use file scheme in URLValidator.

Steps to reproduce

from django.core.validators import URLValidator
URLValidator(schemes=['file'])('file:///tmp/somefile')

Expected result

No exception should be raised.

Current result

ValidationError: [u'Enter a valid URL.']

Change History (9)

comment:1 by Tim Graham, 9 years ago

Component: UncategorizedCore (Other)
Triage Stage: UnreviewedAccepted

Can this be fixed without special casing the file scheme in the validation?

comment:2 by Claude Paroz, 9 years ago

It's URLValidator not URIValidator, I think you might need your own custom validator for this. The current regex is already bloated, I'm not sure we should continue to complexify it...

comment:3 by Tim Graham, 9 years ago

I had a similar skepticism. If we don't make a change, then let's clarify the documentation to prevent further tickets about this.

comment:4 by Johannes Maron, 9 years ago

I thinks this is both a documentation and code problem. The URLValidator should raise a value error, if a unsupported schema is set. In the documentation it should be explained which schemas are supported. Currently one gets the impression that all are.

Last edited 9 years ago by Johannes Maron (previous) (diff)

comment:5 by Johannes Maron, 9 years ago

Interestingly enough, if you include the optional host parameter in RFC1738 it works: file://localhost/tmp/file/name

comment:6 by Adam Zapletal, 9 months ago

Cc: Adam Zapletal added
Has patch: set
Owner: changed from nobody to Adam Zapletal
Status: newassigned

I opened a PR adding a warning about this to the documentation. I'm happy to handle it in a different way if that'd be better.

For what it's worth, if someone out there wants to validate local file URIs in addition to URLs, here is an idea to start with:

@deconstructible
class CustomURLValidator(URLValidator):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.schemes.append('file')

    def __call__(self, value):
        if value.startswith('file:///'):
            value = value.replace('file:///', 'file://localhost/', 1)

        return super().__call__(value)
Last edited 9 months ago by Adam Zapletal (previous) (diff)

comment:7 by Mariusz Felisiak, 9 months ago

Triage Stage: AcceptedReady for checkin

comment:8 by Mariusz Felisiak <felisiak.mariusz@…>, 9 months ago

Resolution: fixed
Status: assignedclosed

In 7326513a:

Fixed #25595 -- Doc'd that URLValidator rejects file:// URIs without a host.

comment:9 by Mariusz Felisiak <felisiak.mariusz@…>, 9 months ago

In 710ca576:

[5.0.x] Fixed #25595 -- Doc'd that URLValidator rejects file:// URIs without a host.

Backport of 7326513a8f5d4d4e0aeec28540f9451b939b1dda from main

Note: See TracTickets for help on using tickets.
Back to Top