Skip to content

Commit

Permalink
Fix use of scope in non shared limit decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
alisaifee committed Dec 27, 2022
1 parent 59fb36b commit 1870a8d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
12 changes: 10 additions & 2 deletions flask_limiter/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ def __init__(
limit_provider=limit,
key_function=self._key_func,
scope="global",
shared=True,
)
for limit in application_limits
]
Expand Down Expand Up @@ -352,6 +353,7 @@ def init_app(self, app: flask.Flask) -> None:
limit_provider=app_limits,
key_function=self._key_func,
scope="global",
shared=True,
cost=self._application_limits_cost,
)
]
Expand Down Expand Up @@ -924,7 +926,10 @@ def __evaluate_limits(self, endpoint: str, limits: List[Limit]) -> None:
limit_for_header: Optional[RequestLimit] = None
view_limits: List[RequestLimit] = []
for lim in sorted(limits, key=lambda x: x.limit):
limit_scope = lim.scope or endpoint
if not lim.shared and lim.scope:
limit_scope = f"{endpoint}:{lim.scope}"
else:
limit_scope = lim.scope or endpoint

if lim.is_exempt or lim.method_exempt:
continue
Expand Down Expand Up @@ -1060,7 +1065,7 @@ def __init__(
self.limiter: weakref.ProxyType[Limiter] = weakref.proxy(limiter)
self.limit_value = limit_value
self.key_func = key_func or self.limiter._key_func
self.scope = scope if shared else None
self.scope = scope
self.per_method = per_method
self.methods = tuple(methods) if methods else None
self.error_message = error_message
Expand All @@ -1070,6 +1075,7 @@ def __init__(
self.on_breach = on_breach
self.cost = cost
self.is_static = not callable(self.limit_value)
self.shared = shared

@property
def dynamic_limit(self) -> Optional[LimitGroup]:
Expand All @@ -1085,6 +1091,7 @@ def dynamic_limit(self) -> Optional[LimitGroup]:
deduct_when=self.deduct_when,
on_breach=self.on_breach,
cost=self.cost,
shared=self.shared,
)

@property
Expand All @@ -1102,6 +1109,7 @@ def static_limits(self) -> List[Limit]:
deduct_when=self.deduct_when,
on_breach=self.on_breach,
cost=self.cost,
shared=self.shared,
)
)

Expand Down
3 changes: 3 additions & 0 deletions flask_limiter/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class Limit:
deduct_when: Optional[Callable[[Response], bool]] = None
on_breach: Optional[Callable[[RequestLimit], Optional[Response]]] = None
_cost: Union[Callable[[], int], int] = 1
shared: bool = False

def __post_init__(self) -> None:
if self.methods:
Expand Down Expand Up @@ -144,6 +145,7 @@ class LimitGroup:
on_breach: Optional[Callable[[RequestLimit], Optional[Response]]] = None
per_method: bool = False
cost: Optional[Union[Callable[[], int], int]] = None
shared: bool = False

def __iter__(self) -> Iterator[Limit]:
limit_items = parse_many(
Expand All @@ -165,4 +167,5 @@ def __iter__(self) -> Iterator[Limit]:
self.deduct_when,
self.on_breach,
self.cost or 1,
self.shared,
)

0 comments on commit 1870a8d

Please sign in to comment.