Giter VIP home page Giter VIP logo

Comments (5)

vytas7 avatar vytas7 commented on May 22, 2024 1

It does not necessarily mean you, but you are of course very welcome to tackle this one 😈

from falcon.

vytas7 avatar vytas7 commented on May 22, 2024

Hi @mattwarrick!
I agree this is a bug as TestClient ought to either correctly encode the RAW_URI CGI variable or omit it altogether.

We'll get this fixed, but for the time being you can work around the problem by manually providing the correct rendition of RAW_URI via the extras parameter when simulating requests, e.g.:

import falcon
import falcon.testing
import falcon.uri


class RawPathComponent:
    def process_request(self, req, resp):
        raw_uri = req.env.get('RAW_URI') or req.env.get('REQUEST_URI')

        # NOTE: Reconstruct the percent-encoded path from the raw URI.
        if raw_uri:
            req.path, _, _ = raw_uri.partition('?')


class URLResource:
    def on_get(self, req, resp, url):
        # NOTE: url here is potentially percent-encoded.
        url = falcon.uri.decode(url)

        resp.media = {'url': url}

    def on_get_status(self, req, resp, url):
        # NOTE: url here is potentially percent-encoded.
        url = falcon.uri.decode(url)

        resp.media = {'cached': True}


app = falcon.App(middleware=[RawPathComponent()])
app.add_route('/cache/{url}', URLResource())
app.add_route('/cache/{url}/status', URLResource(), suffix='status')


def test_raw_uri():
    client = falcon.testing.TestClient(app)

    resp1 = client.get(
        '/cache/http%3A%2F%2Ffalconframework.org',
        extras={'RAW_URI': '/cache/http%3A%2F%2Ffalconframework.org'},
    )
    assert resp1.json == {'url': 'http://falconframework.org'}

    resp2 = client.get(
        '/cache/http%3A%2F%2Ffalconframework.org/status',
        extras={'RAW_URI': '/cache/http%3A%2F%2Ffalconframework.org/status'},
    )
    assert resp2.json == {'cached': True}

If you have a large testing suite, and it is impractical to apply this treatment to every invocation, you could probably mitigate the impact by first checking the raw path for '%2F' in the middleware itself (and/or alternatively restrict the effect by other parts of the path such as prefix):

        # NOTE: Reconstruct the percent-encoded path from the raw URI.
        if raw_uri and '%2F' in raw_uri:
            req.path, _, _ = raw_uri.partition('?')

from falcon.

liborjelinek avatar liborjelinek commented on May 22, 2024

I understand what adding "good first issue" and "needs contributor" tags means to me :-) I'll try to prepare PR fixit it.

from falcon.

vytas7 avatar vytas7 commented on May 22, 2024

Resolved fixed in #2159

from falcon.

liborjelinek avatar liborjelinek commented on May 22, 2024

A follow-up for others that might stumble upon this issue in future: I have collected together steps required for creating URL-encoded URLs in Falcon in a blog post.

from falcon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.