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

Allow relative URLs in the redirect location header. #229

Merged
merged 5 commits into from
Dec 4, 2023

Conversation

isoos
Copy link
Collaborator

@isoos isoos commented Nov 10, 2023

Fixes #228.

@isoos isoos requested review from jonasfj and sigurdm November 10, 2023 10:07
@@ -159,8 +159,10 @@ Future<bool> _safeUrlCheck(
if (response.isRedirect &&
response.headers[HttpHeaders.locationHeader]!.isNotEmpty &&
maxRedirects > 0) {
final loc = Uri.parse(response.headers[HttpHeaders.locationHeader]![0]);
final nextUri = loc.isAbsolute ? loc : url.resolveUri(loc);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this always work?

Suggested change
final nextUri = loc.isAbsolute ? loc : url.resolveUri(loc);
final nextUri = url.resolveUri(loc);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also how about adding a test case.
If we're really lazy we can probably use https://httpbin.org/redirect-to?url=status%2F200 to test it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've prepared a server + redirect logic for a test, but the test logic checks for local / private IPs and it always fails for such tests. Any preference how to suppress the IP checks for tests?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, isn't it tempting to be lazy and just use: https://httpbin.org/redirect-to?url=status%2F200

Ignoring such local / private IP tests is kind of a bad idea, since the whole point is to avoid hitting private networks :D

I suppose one could do a one-off using a constant like bool.fromEnvironment() but that doesn't make testing easier, since you'll have to do -D .. something.

Let's either not test it, or use httpbin to not make our problems bigger than they have to be.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, isn't it tempting to be lazy and just use: https://httpbin.org/redirect-to?url=status%2F200

Already added that: https://github.com/google/dart-neats/pull/229/files#diff-d7c4d80be7934367cf516c2c7d1c65dd444fc2a10786ea20790381d3c361dea1R30

Ignoring such local / private IP tests is kind of a bad idea, since the whole point is to avoid hitting private networks :D

I think it is not unreasonable to turn off some parts of the library for local testing - it feels that more coverage is better in this case. I'll check if I can implement something that is not exposed to the end user in any other way.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the PR with refactor + override for local testing. I can remove the commit if needed, but I think it is better to have it this way.

@isoos isoos force-pushed the relative-redirect branch from af56554 to 1a87ca1 Compare November 27, 2023 15:37
safe_url_check/lib/src/safe_url_check.dart Outdated Show resolved Hide resolved
}
}

class SafeUrlChecker {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we creating a class that has no purpose?

This is just a convoluted way to make function, right?

Why not just keep the old _safeUrlCheck make it safeUrlCheck in the safe_url_check/lib/src/safe_url_check.dart file and add a parameter skipLocalNetworkCheck which defaults to false and has @visibleForTestingOnly annotation.

Or we could just accept that we don't have testing, or use httpbin.org for testing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or put the parameter on doSafeUrlCheck.

This introduces a class SafeUrlChecker which we only every construct inorder to call a single method.

It's just a confusing way to have a function that you can pass arguments to in two different ways.

Another, somewhat simpler and ugly option is to make a global:

@visibleForTestingOnly
bool skipLocalNetworkCheck = false;

Put this in an lib/src/... file and check it from within safeUrlCheck.
If no code actively sets this property, hopefully the compiler will optimize it away.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a convoluted way to make function, right?

I didn't like the recursive function with 6+ parameters. The class keeps the non-changing parts as fields.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, PTAL.

@jonasfj jonasfj merged commit e246c20 into google:master Dec 4, 2023
16 checks passed
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.

The updated RFC allows relative URLs in the location header, but the package rejects them.
3 participants