Skip to content

Commit

Permalink
chore: fix secure alert about set_cookie (#376)
Browse files Browse the repository at this point in the history
Signed-off-by: Keming <[email protected]>
  • Loading branch information
kemingy authored Oct 3, 2024
1 parent 8967d2f commit 89893e4
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ line-length = 88
[tool.ruff.lint]
select = ["E", "F", "B", "G", "I", "SIM", "TID", "PL", "RUF"]
ignore = ["E501", "PLR2004", "RUF012"]
[tool.ruff.pylint]
[tool.ruff.lint.pylint]
max-args = 12
max-branches = 15

Expand Down
4 changes: 1 addition & 3 deletions spectree/plugins/starlette_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,7 @@ def parse_route(app, prefix=""):
return routes

def bypass(self, func, method):
if method in ["HEAD", "OPTIONS"]:
return True
return False
return method in ["HEAD", "OPTIONS"]

def parse_func(self, route):
for method in route.methods or ["GET"]:
Expand Down
12 changes: 9 additions & 3 deletions tests/flask_imports/dry_plugin_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

@pytest.mark.parametrize("response_format", ["json", "xml"])
def test_flask_skip_validation(client, response_format: str):
client.set_cookie(key="pub", value="abcdefg")
client.set_cookie(
key="pub", value="abcdefg", secure=True, httponly=True, samesite="Strict"
)
assert response_format in ("json", "xml")
resp = client.post(
f"/api/user_skip/flask?order=1&response_format={response_format}",
Expand All @@ -31,7 +33,9 @@ def test_flask_skip_validation(client, response_format: str):


def test_flask_return_model(client):
client.set_cookie(key="pub", value="abcdefg")
client.set_cookie(
key="pub", value="abcdefg", secure=True, httponly=True, samesite="Strict"
)

resp = client.post(
"/api/user_model/flask?order=1",
Expand Down Expand Up @@ -134,7 +138,9 @@ def test_flask_validate_basic(client):
],
)
def test_flask_validate_post_data(client, fragment):
client.set_cookie(key="pub", value="abcdefg")
client.set_cookie(
key="pub", value="abcdefg", secure=True, httponly=True, samesite="Strict"
)
resp = client.post(
f"/api/{fragment}/flask?order=1",
json=dict(name="flask", limit=10),
Expand Down
12 changes: 9 additions & 3 deletions tests/quart_imports/dry_plugin_quart.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

@pytest.mark.parametrize("response_format", ["json", "xml"])
def test_quart_skip_validation(client, response_format: str):
client.set_cookie("quart", "pub", "abcdefg")
client.set_cookie(
"quart", "pub", "abcdefg", secure=True, httponly=True, samesite="Strict"
)

resp = asyncio.run(
client.post(
Expand All @@ -32,7 +34,9 @@ def test_quart_skip_validation(client, response_format: str):


def test_quart_return_model(client):
client.set_cookie("quart", "pub", "abcdefg")
client.set_cookie(
"quart", "pub", "abcdefg", secure=True, httponly=True, samesite="Strict"
)

resp = asyncio.run(
client.post(
Expand Down Expand Up @@ -129,7 +133,9 @@ def test_quart_validate(client):
assert resp.status_code == 422
assert resp.headers.get("X-Error") == "Validation Error"

client.set_cookie("quart", "pub", "abcdefg")
client.set_cookie(
"quart", "pub", "abcdefg", secure=True, httponly=True, samesite="Strict"
)
for fragment in ("user", "user_annotated"):
resp = asyncio.run(
client.post(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_config_contact():
Configuration(contact={"name": "John", "url": "url"})


@pytest.mark.skipif(EmailFieldType == str, reason="email-validator is not installled")
@pytest.mark.skipif(EmailFieldType is str, reason="email-validator is not installled")
def test_config_contact_invalid_email():
with pytest.raises(ValidationError):
Configuration(contact={"name": "John", "email": "hello"})
Expand Down
2 changes: 1 addition & 1 deletion tests/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_init_response():
expect_400_model = gen_list_model(JSON)
assert resp.has_model()
assert resp.find_model(200) is None
assert type(resp.find_model(400)) == type(expect_400_model) and get_type_hints(
assert type(resp.find_model(400)) is type(expect_400_model) and get_type_hints(
resp.find_model(400)
) == get_type_hints(expect_400_model)
assert resp.find_model(401) == DemoModel
Expand Down

0 comments on commit 89893e4

Please sign in to comment.