Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
A Flask extension to limit access to your routes by using allowed hostnames and IP addresses.
This extension provides a way to restrict access to your Flask application based on the incoming request's hostname or IP address or IP address range (network).
Install the package using pip:
pip install flask-allowed-hosts
AllowedHosts
class.@allowed_hosts.limit()
decorator (optional).from flask import Flask, jsonify, abort
from flask_allowed_hosts import AllowedHosts
app = Flask(__name__)
ALLOWED_HOSTS = ["93.184.215.14", "api.example.com"]
def custom_on_denied():
error = {"error": "Oops! Looks like you are not allowed to access this page!"}
return jsonify(error), 403
allowed_hosts = AllowedHosts(app, allowed_hosts=ALLOWED_HOSTS, on_denied=custom_on_denied)
# Allows all incoming requests
@app.route("/api/public", methods=["GET"])
def public_endpoint():
data = {"message": "This is public!"}
return jsonify(data), 200
# Only allows incoming requests from "93.184.215.14" and "api.example.com"
@app.route("/api/private", methods=["GET"])
@allowed_hosts.limit()
def private_endpoint():
data = {"message": "This is private!"}
return jsonify(data), 200
# We can override the allowed_hosts list and the on_denied function for each route
@app.route("/api/private/secret", methods=["GET"])
@allowed_hosts.limit(allowed_hosts=["127.0.0.1"], on_denied=lambda: abort(404))
def secret_private_endpoint():
data = {"message": "This is very private!"}
return jsonify(data), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
Warning: This approach might cause unexpected behavior when combined with the class-based usage.
@limit_hosts
decorator.from flask import Flask, jsonify
from flask_allowed_hosts import limit_hosts
app = Flask(__name__)
ALLOWED_HOSTS = ["93.184.215.14", "api.example.com"]
def custom_on_denied():
error = {"error": "Custom Denied Response"}
return jsonify(error), 403
# Allows all incoming requests
@app.route("/api/public", methods=["GET"])
def public_endpoint():
data = {"message": "This is public!"}
return jsonify(data), 200
# Only allows incoming requests from "93.184.215.14" and "api.example.com"
@app.route("/api/private", methods=["GET"])
@limit_hosts(allowed_hosts=ALLOWED_HOSTS, on_denied=custom_on_denied)
def private_endpoint():
return jsonify({"message": "This is private!"}), 200
You can find more examples in the examples directory.
app
: The Flask application instance (optional).allowed_hosts
: List of allowed hosts (optional, defaults to None
which allows all hosts).on_denied
: Function for denied access behavior (optional).The extension respects these configurations:
ALLOWED_HOSTS
: List of allowed hosts in Flask config.ALLOWED_HOSTS_ON_DENIED
: Function for denied access behavior in Flask config.Precedence: Values provided during initialization override Flask config values.
You can enable debug mode by setting the ALLOWED_HOSTS_DEBUG
environment variable to True
:
export ALLOWED_HOSTS_DEBUG="True"
This will print helpful debug messages to the console.
Contributions are welcome! Please feel free to submit a Pull Request.
If you have any questions or feedback, please feel free to open an issue or a pull request.
This project is licensed under the [MIT] License - see the LICENSE.md file for details.
FAQs
A Flask extension to limit access to your routes by using allowed hostnames and IP addresses.
We found that flask-allowed-hosts demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.