Skip to main content

Flask Allowed Hosts

Flask Allowed Hosts is a Flask extension that provides host validation for API endpoints. It allows you to enforce that requests are only accepted from specific hosts, providing an additional layer of security for your Flask application.

Installation

Install the package using pip:

pip install flask-allowed-hosts

Getting Started

To limit access to your routes using flask-allowed-hosts:

from flask import Flask, request, jsonify
from flask_allowed_hosts import AllowedHosts

ALLOWED_HOSTS = ["123.123.123.123", "321.321.321.321"]


# Returns a json response if the request IP is not in the allowed hosts
def on_denied():
  error = {"error": "Oops! looks like you are not allowed to access this page!"}
  return jsonify(error), 403


app = Flask(__name__)
allowed_hosts = AllowedHosts(app, allowed_hosts=ALLOWED_HOSTS, on_denied=on_denied)


@app.route("/", methods=["GET"])
def home_page():
  return "Hello World!"


@app.route("/api/greet", methods=["GET"])
@allowed_hosts.limit()
def greet_endpoint():
  name = request.args.get("name", "Friend")
  greeting = {"greeting": f"Hello There {name}!"}
  return jsonify(greeting), 200


@app.route("/api/greet/override", methods=["GET"])
@allowed_hosts.limit(allowed_hosts=["127.0.0.1", "localhost"])
def greet_override_endpoint():
  name = request.args.get("name", "Friend")
  greeting = {"greeting override": f"Hello There {name}!"}
  return jsonify(greeting), 200


if __name__ == '__main__':
  app.run(host='0.0.0.0', port=5000, debug=True)

Now only the allowed hosts set in ALLOWED_HOSTS can access the protected endpoint(s). Requests from other hosts will receive a 403 Forbidden error.

You can check out more examples in the examples directory.

Arguments

  • allowed_hosts: [List[str], str] : Modify this list to include the allowed hosts. The default value is an empty list [], which means requests from all hosts are allowed.

  • on_denied: Callable: Modify this function to customize the behavior when a request is denied. The default is None, which means a 403 Forbidden error is returned.

Contributing

Contributions are welcome! If you have any suggestions, bug reports, or feature requests, please open an issue or submit a pull request.

License

This project is licensed under the [MIT] License - see the LICENSE.md file for details.

Metadata

Release files for flask-allowed-hosts 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-allowed-hosts 1.0.2
File Size Uploaded
flask_allowed_hosts-1.0.2.tar.gz 4.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flask-allowed-hosts 1.0.2
File Interpreter ABI Platform
flask_allowed_hosts-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 9.2 kB

Release files / flask_allowed_hosts-1.0.2.tar.gz

Download URL flask_allowed_hosts-1.0.2.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
c55315de3d7c5871364357e3c8e1167fe8479f0c97b6f9674340472613573da3
BLAKE2b-256 checksum
How to use checksums
42934e41b69572c67f07e529e494cb41f2b132ab07d16f3f1ab30bdf80679c41
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.2

Release files / flask_allowed_hosts-1.0.2-py3-none-any.whl

Download URL flask_allowed_hosts-1.0.2-py3-none-any.whl
Size 4.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
395e69738aadb15f59edd1c6361348bb7ee46386f85500929a166f1838a0b101
BLAKE2b-256 checksum
How to use checksums
6443afd534626788e6b1969ac7327686abe96c7ccf0da849f6ec3a7c78f87e0d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.2

Release history Release notifications | RSS feed

1.2.0

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.4

2 release files

1.0.3

2 release files

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page