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

Return empty response when include fails #2846

Merged
merged 1 commit into from
Jan 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion tests/unit/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from warehouse.views import (
SEARCH_BOOSTS, SEARCH_FIELDS, current_user_indicator, forbidden, health,
httpexception_view, index, robotstxt, opensearchxml, search, force_status,
flash_messages
flash_messages, forbidden_include
)

from ..common.db.accounts import UserFactory
Expand Down Expand Up @@ -147,6 +147,19 @@ def test_logged_out_redirects_login(self):
"/accounts/login/?next=/foo/bar/%3Fb%3Ds"


class TestForbiddenIncludeView:

def test_forbidden_include(self):
exc = pretend.stub()
request = pretend.stub()

resp = forbidden_include(exc, request)

assert resp.status_code == 200
Copy link
Member

Choose a reason for hiding this comment

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

Do we still want this to be a 403 response? I think the HTML include will work fine in either case, but it feels a little cleaner to keep the response code as 403 here? What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, that makes sense.

assert resp.content_type == 'text/html'
assert resp.content_length == 0


def test_robotstxt(pyramid_request):
assert robotstxt(pyramid_request) == {}
assert pyramid_request.response.content_type == "text/plain"
Expand Down
8 changes: 8 additions & 0 deletions warehouse/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ def forbidden(exc, request, redirect_to="accounts.login"):
return httpexception_view(exc, request)


@forbidden_view_config(path_info=r"^/_includes/")
@exception_view_config(PredicateMismatch, path_info=r"^/_includes/")
def forbidden_include(exc, request):
# If the forbidden error is for a client-side-include, just return an empty
# response instead of redirecting
return Response()


@view_config(
route_name="robots.txt",
renderer="robots.txt",
Expand Down