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

Request path in req.uri does not match full path of request for nested servers. #832

Open
dcow opened this issue May 19, 2021 · 10 comments
Open
Labels
bug Something isn't working

Comments

@dcow
Copy link

dcow commented May 19, 2021

I understand this may be by design due to the nature of nesting, etc. If so, then the issue is that for an application that implements something like signed requests, the full path of the incoming request needs to be known by the middleware so that it can generate the right digest. I tried querying req.param("--tide-path-rest") but it returns the same data as req.uri().path() even though there's an entry in the map that shows more of the path.

@dcow
Copy link
Author

dcow commented May 21, 2021

This is due to using nest instead of all. However, when using all tide doesn't want to resolve any of my routes anymore.

@jasontheiler
Copy link

I'd also really like to know if this is by design or not. And if so, how do you get the full request path then? It's really annoying when you try to do some URL generation using req.url().join("...") inside a nested middleware or endpoint.

@Fishrock123
Copy link
Member

This sounds like a bug but I think it may be tricky to fix.

@Fishrock123 Fishrock123 added the bug Something isn't working label Jun 9, 2021
@dcow
Copy link
Author

dcow commented Aug 31, 2021

@Fishrock123 I may have some time to look into fixing this. What is tricky about the issue, in your opinion? And what should the correct behavior be? Should nested middleware req.uri contain the full path? Or should a public param be added that contains the original req uri? Or is all broken?

@Fishrock123
Copy link
Member

@jbr would routefinder help this? I think so, right?

@jbr
Copy link
Member

jbr commented Sep 4, 2021

I'm not entirely sure, but I don't think so. I think this is a tide design issue, where it's intended that the request url is rewritten inside of nested apps, but there isn't an alternative api for the inner app to find out the actual full url. In my opinion, there should be two distinct apis: One is "what's my relative path from my mount point, which I might not know as a nested app author" and the other is "what is the full request path, untouched"

@Fishrock123
Copy link
Member

I agree with that, yeah.

@sorcix
Copy link
Contributor

sorcix commented Oct 10, 2021

I'm working around this problem by using the Route::at method.

let mut server = tide::new();
server.at("/foo").get(foo_handler);

// "nested" server
let mut api = server.at("/api");
api.with(auth).with(cors).with(JsonApi);

api.at("/endpoint1").get(something);
api.at("/endpoint2").get(somethingelse);

It seems to behave as I expected a nested server to behave.

@dcow
Copy link
Author

dcow commented Oct 10, 2021

When I do this the application crashes for reasons I can’t discern. I’ll give it another shot and report back.

@sorcix
Copy link
Contributor

sorcix commented Oct 10, 2021

I've found a single difference between nested servers and this workaround with routes since I've started using it: the moment at which middleware is executed.

Middleware on a Server seems to execute before the routing, so my auth middleware returns 403 for unauthenticated requests even when the requested route does not exist. When using this workaround middleware only executes for routes that do exist, as they are set on the routes themselves.

In the example for my previous post:

let mut api = server.at("/api");
api.with(auth).with(cors).with(JsonApi);

api.at("/endpoint1").get(something);

This means that /api/endpoint returns 403 but /api/bar or /api/endpoint/foo returns 404. When using a nested server they would all return 403.

But it works for now. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants