# 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:
```cmd
pip install flask-allowedhosts
```
## Getting Started
To limit access to your routes using `flask-allowedhosts` follow these simple steps:
1. Import the `limit_hosts` decorator from flask_allowedhosts.
2. Define the list of allowed hosts.
3. Apply the `@limit_hosts` decorator to the desired endpoint(s).
```python
from flask import Flask, request, jsonify
from flask_allowedhosts import limit_hosts
app = Flask(__name__)
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
@app.route("/api/greet")
@limit_hosts(allowed_hosts=ALLOWED_HOSTS)
def greet_endpoint():
name = request.args.get("name", "Friend")
greeting = {"greeting": f"Hello There {name}!"}
return jsonify(greeting), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
```
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.
_The default value for `allowed_hosts` is an empty list, which means requests from all hosts are allowed._
## 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.
Raw data
{
"_id": null,
"home_page": "https://github.com/riad-azz/flask-allowedhosts",
"name": "flask-allowedhosts",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "flask,allowed hosts,web development,security",
"author": "Riadh Azzoun",
"author_email": "riadh.azzoun@hotmail.com",
"download_url": "https://files.pythonhosted.org/packages/3b/65/83d36874ea6925675f6220aeb16a5e0f0dfee3a18881032a2abcfe1523bd/flask-allowedhosts-1.2.0.tar.gz",
"platform": null,
"description": "# Flask Allowed Hosts\r\n\r\nFlask 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.\r\n\r\n## Installation\r\n\r\nInstall the package using pip:\r\n\r\n```cmd\r\npip install flask-allowedhosts\r\n```\r\n\r\n## Getting Started\r\n\r\nTo limit access to your routes using `flask-allowedhosts` follow these simple steps:\r\n\r\n1. Import the `limit_hosts` decorator from flask_allowedhosts.\r\n2. Define the list of allowed hosts.\r\n3. Apply the `@limit_hosts` decorator to the desired endpoint(s).\r\n\r\n```python\r\nfrom flask import Flask, request, jsonify\r\nfrom flask_allowedhosts import limit_hosts\r\n\r\napp = Flask(__name__)\r\n\r\nALLOWED_HOSTS = ['127.0.0.1', 'localhost']\r\n\r\n\r\n@app.route(\"/api/greet\")\r\n@limit_hosts(allowed_hosts=ALLOWED_HOSTS)\r\ndef greet_endpoint():\r\n name = request.args.get(\"name\", \"Friend\")\r\n greeting = {\"greeting\": f\"Hello There {name}!\"}\r\n return jsonify(greeting), 200\r\n\r\n\r\nif __name__ == '__main__':\r\n app.run(host='0.0.0.0', port=5000)\r\n```\r\n\r\nNow only the allowed hosts set in `ALLOWED_HOSTS` can access the protected endpoint(s). Requests from other hosts will receive a 403 Forbidden error.\r\n\r\n_The default value for `allowed_hosts` is an empty list, which means requests from all hosts are allowed._\r\n\r\n## Contributing\r\n\r\nContributions are welcome! If you have any suggestions, bug reports, or feature requests, please open an issue or submit a pull request.\r\n\r\n## License\r\n\r\nThis project is licensed under the [MIT] License - see the LICENSE.md file for details.\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A Flask extension to limit access to your routes by using allowed hosts.",
"version": "1.2.0",
"project_urls": {
"Homepage": "https://github.com/riad-azz/flask-allowedhosts"
},
"split_keywords": [
"flask",
"allowed hosts",
"web development",
"security"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "6593b3f34abb3db6c5fdf64067820af5deb535d172cf289ed5d573ce852aae68",
"md5": "99d60bc5679aa09321c401560e57e806",
"sha256": "07abfbe94cdc0799019261c4abfcba78afc2fbd19088701cc252085958bb285b"
},
"downloads": -1,
"filename": "flask_allowedhosts-1.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "99d60bc5679aa09321c401560e57e806",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 4372,
"upload_time": "2023-07-19T16:25:14",
"upload_time_iso_8601": "2023-07-19T16:25:14.033640Z",
"url": "https://files.pythonhosted.org/packages/65/93/b3f34abb3db6c5fdf64067820af5deb535d172cf289ed5d573ce852aae68/flask_allowedhosts-1.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3b6583d36874ea6925675f6220aeb16a5e0f0dfee3a18881032a2abcfe1523bd",
"md5": "4e7bc9f9381722b4e20e19cc2a4d5fba",
"sha256": "05a823c81a5c24168bad743f2c509e41547cd0fac555a88372dbb055558f8bc4"
},
"downloads": -1,
"filename": "flask-allowedhosts-1.2.0.tar.gz",
"has_sig": false,
"md5_digest": "4e7bc9f9381722b4e20e19cc2a4d5fba",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 3859,
"upload_time": "2023-07-19T16:25:15",
"upload_time_iso_8601": "2023-07-19T16:25:15.638053Z",
"url": "https://files.pythonhosted.org/packages/3b/65/83d36874ea6925675f6220aeb16a5e0f0dfee3a18881032a2abcfe1523bd/flask-allowedhosts-1.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-07-19 16:25:15",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "riad-azz",
"github_project": "flask-allowedhosts",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "flask-allowedhosts"
}