From 277c2ce2d2da7c632423c87a8b25c8662f1423bd Mon Sep 17 00:00:00 2001 From: Yun Xu Date: Mon, 15 Oct 2018 21:53:11 -0700 Subject: [PATCH 01/25] fix redirect with quoted param --- sanic/response.py | 2 +- tests/test_redirect.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/sanic/response.py b/sanic/response.py index cf9aaba2..c32ed9ce 100644 --- a/sanic/response.py +++ b/sanic/response.py @@ -421,7 +421,7 @@ def redirect( headers = headers or {} # URL Quote the URL before redirecting - safe_to = quote_plus(to, safe=":/#?&=@[]!$&'()*+,;") + safe_to = quote_plus(to, safe=":/%#?&=@[]!$&'()*+,;") # According to RFC 7231, a relative URI is now permitted. headers["Location"] = safe_to diff --git a/tests/test_redirect.py b/tests/test_redirect.py index f5efac60..8c7cc007 100644 --- a/tests/test_redirect.py +++ b/tests/test_redirect.py @@ -1,4 +1,6 @@ import pytest +import json +from urllib.parse import quote from sanic.response import text, redirect @@ -107,3 +109,25 @@ def test_redirect_with_header_injection(redirect_app): assert response.status == 302 assert "test-header" not in response.headers assert not response.text.startswith('test-body') + + +@pytest.mark.parametrize("test_str", ["sanic-test", "sanictest", "sanic test"]) +async def test_redirect_with_params(app, test_client, test_str): + + @app.route("/api/v1/test//") + async def init_handler(request, test): + assert test == test_str + return redirect("/api/v2/test/{}/".format(quote(test))) + + @app.route("/api/v2/test//") + async def target_handler(request, test): + assert test == test_str + return text("OK") + + test_cli = await test_client(app) + + response = await test_cli.get("/api/v1/test/{}/".format(quote(test_str))) + assert response.status == 200 + + txt = await response.text() + assert txt == "OK" From 20d1ab60c718869d86deed5410d5aef428042195 Mon Sep 17 00:00:00 2001 From: Yun Xu Date: Mon, 15 Oct 2018 22:13:42 -0700 Subject: [PATCH 02/25] remove unused json import --- tests/test_redirect.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_redirect.py b/tests/test_redirect.py index 8c7cc007..a6ccf5de 100644 --- a/tests/test_redirect.py +++ b/tests/test_redirect.py @@ -1,5 +1,4 @@ import pytest -import json from urllib.parse import quote from sanic.response import text, redirect From 8ef7bf8e7b610604a6aef4cda8d823e4c10a6c48 Mon Sep 17 00:00:00 2001 From: Yun Xu Date: Wed, 17 Oct 2018 21:20:16 -0700 Subject: [PATCH 03/25] integrate with isort --- sanic/__init__.py | 1 + sanic/__main__.py | 3 ++- sanic/app.py | 20 +++++++++++--------- sanic/blueprints.py | 1 + sanic/cookies.py | 1 + sanic/exceptions.py | 1 + sanic/handlers.py | 13 +++++++------ sanic/reloader_helpers.py | 5 +++-- sanic/request.py | 12 ++++++++---- sanic/response.py | 13 +++++++------ sanic/router.py | 4 +++- sanic/server.py | 28 ++++++++++++++++------------ sanic/static.py | 4 ++-- sanic/testing.py | 3 ++- sanic/views.py | 2 +- sanic/websocket.py | 7 ++++--- sanic/worker.py | 16 +++++++++------- setup.cfg | 13 +++++++++++++ tox.ini | 2 ++ 19 files changed, 94 insertions(+), 55 deletions(-) diff --git a/sanic/__init__.py b/sanic/__init__.py index 786b6647..a6e7ba72 100644 --- a/sanic/__init__.py +++ b/sanic/__init__.py @@ -1,6 +1,7 @@ from sanic.app import Sanic from sanic.blueprints import Blueprint + __version__ = "0.8.3" __all__ = ["Sanic", "Blueprint"] diff --git a/sanic/__main__.py b/sanic/__main__.py index ede743fd..9bef96fc 100644 --- a/sanic/__main__.py +++ b/sanic/__main__.py @@ -1,8 +1,9 @@ from argparse import ArgumentParser from importlib import import_module -from sanic.log import logger from sanic.app import Sanic +from sanic.log import logger + if __name__ == "__main__": parser = ArgumentParser(prog="sanic") diff --git a/sanic/app.py b/sanic/app.py index 9fa2e4ce..9ef65260 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -1,29 +1,31 @@ -import os import logging import logging.config +import os import re import warnings -from asyncio import get_event_loop, ensure_future, CancelledError -from collections import deque, defaultdict + +from asyncio import CancelledError, ensure_future, get_event_loop +from collections import defaultdict, deque from functools import partial from inspect import getmodulename, isawaitable, signature, stack +from ssl import Purpose, create_default_context from traceback import format_exc from urllib.parse import urlencode, urlunparse -from ssl import create_default_context, Purpose + +import sanic.reloader_helpers as reloader_helpers from sanic.config import Config from sanic.constants import HTTP_METHODS -from sanic.exceptions import ServerError, URLBuildError, SanicException +from sanic.exceptions import SanicException, ServerError, URLBuildError from sanic.handlers import ErrorHandler -from sanic.log import logger, error_logger, LOGGING_CONFIG_DEFAULTS +from sanic.log import LOGGING_CONFIG_DEFAULTS, error_logger, logger from sanic.response import HTTPResponse, StreamingHTTPResponse from sanic.router import Router -from sanic.server import serve, serve_multiple, HttpProtocol, Signal +from sanic.server import HttpProtocol, Signal, serve, serve_multiple from sanic.static import register as static_register from sanic.testing import SanicTestClient from sanic.views import CompositionView -from sanic.websocket import WebSocketProtocol, ConnectionClosed -import sanic.reloader_helpers as reloader_helpers +from sanic.websocket import ConnectionClosed, WebSocketProtocol class Sanic: diff --git a/sanic/blueprints.py b/sanic/blueprints.py index c148a952..c837a2a9 100644 --- a/sanic/blueprints.py +++ b/sanic/blueprints.py @@ -3,6 +3,7 @@ from collections import defaultdict, namedtuple from sanic.constants import HTTP_METHODS from sanic.views import CompositionView + FutureRoute = namedtuple( "Route", [ diff --git a/sanic/cookies.py b/sanic/cookies.py index 7d323529..fc49ecfd 100644 --- a/sanic/cookies.py +++ b/sanic/cookies.py @@ -1,6 +1,7 @@ import re import string + # ------------------------------------------------------------ # # SimpleCookie # ------------------------------------------------------------ # diff --git a/sanic/exceptions.py b/sanic/exceptions.py index 6e9323a9..35318ac9 100644 --- a/sanic/exceptions.py +++ b/sanic/exceptions.py @@ -1,5 +1,6 @@ from sanic.helpers import STATUS_CODES + TRACEBACK_STYLE = """