Skip to content

[KA-CHOW] fix(vesper-api-gateway): Missing rate limiting - #1

Closed
HarshXAI wants to merge 1 commit into
mainfrom
kachow/heal-Missing-rate-limiting-1771727131651
Closed

HarshXAI wants to merge 1 commit into
mainfrom
kachow/heal-Missing-rate-limiting-1771727131651

Conversation

@HarshXAI

Copy link
Copy Markdown
Owner

🔧 KA-CHOW Self-Healing Patch

Service: vesper-api-gateway
Issue type: Missing rate limiting

What changed

The code introduces a rate limiting decorator that tracks the number of requests from each client IP within a 60-second window. It maintains a dictionary to store timestamps of requests for each IP. When a request is made, it filters out timestamps older than 60 seconds and checks if the number of requests exceeds the limit of 100. If it does, it returns a 429 error. Otherwise, it appends the current timestamp and processes the request. This change prevents abuse by limiting excessive requests while preserving existing functionality.

Patch

from flask import Flask, request, jsonify
from functools import wraps
from time import time

app = Flask(__name__)

RATE_LIMIT = 100
TIME_WINDOW = 60
client_requests = {}


def rate_limit(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        client_ip = request.remote_addr
        current_time = time()
        if client_ip not in client_requests:
            client_requests[client_ip] = []
        # Filter out requests that are outside the time window
        client_requests[client_ip] = [timestamp for timestamp in client_requests[client_ip] if current_time - timestamp < TIME_WINDOW]
        if len(client_requests[client_ip]) >= RATE_LIMIT:
            return jsonify({'error': 'Too many requests'}), 429
        client_requests[client_ip].append(current_time)
        return func(*args, **kwargs)
    return wrapper

@app.route('/public-endpoint')
@rate_limit
def public_endpoint():
    return jsonify({'message': 'This is a public endpoint'})

if __name__ == '__main__':
    app.run()

Auto-generated by KA-CHOW — review before merging

@github-advanced-security

Copy link
Copy Markdown

This pull request sets up GitHub code scanning for this repository. Once the scans have completed and the checks have passed, the analysis results for this pull request branch will appear on this overview. Once you merge this pull request, the 'Security' tab will show more code scanning analysis results (for example, for the default branch). Depending on your configuration and choice of analysis tool, future pull requests will be annotated with code scanning analysis results. For more information about GitHub code scanning, check out the documentation.

@HarshXAI HarshXAI closed this Feb 22, 2026
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.

2 participants