Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
fastapi-sqlmodel-security
Advanced tools
(This is forked from FastAPI Simple Security with an SQLModel adaption. Credits to the original author!)
API key based security package for FastAPI, focused on simplicity of use:
sqlite
backend, working with both header and query parametersFastAPI
and the python standard libraryThis module cannot be used for any kind of distributed deployment. It's goal is to help have some basic security features for simple one-server API deployments, mostly during development.
pip install fastapi_sqlmodel_security
The key is to configure an instance of data store, which can usually be done with SqlModelDataStore
as shown below.
from fastapi_sqlmodel_security import create_auth_router, ApiKeySecurity, DataStore, SqlModelDataStore
from fastapi import Depends, FastAPI
app = FastAPI()
data_store = SqlModelDataStore(conn_url="sqlite3:///keys.db")
app.include_router(create_auth_router(data_store), prefix="/auth", tags=["_auth"])
@app.get("/secure", dependencies=[Depends(ApiKeySecurity(data_store))])
async def secure_endpoint():
return {"message": "This is a secure endpoint"}
Both the auth router and your own routes would need dependencies to be configured with the aforementioned data store.
Resulting app is:
More can be found in the demo app.
Start your API and check the logs for the automatically generated secret key if you did not provide one through environment variables.
Go to /docs
on your API and inform this secret key in the Authorize/Secret header
box.
All the administrator endpoints only support header security to make sure the secret key is not inadvertently
shared when sharing an URL.
Then, you can use /auth/new
to generate a new API key.
And finally, you can use this API key to access the secure endpoint.
You can of course automate API key acquisition through python with requests
and directly querying the endpoints.
If you do so, you can hide the endpoints from your API documentation with the environment variable
FASTAPI_SQLMODEL_SECURITY_HIDE_DOCS
.
You may want to use a no-auth api key security filter NoAuthApiKeySecurity
as an alternative to the real filter.
It does not do any key verification and will never block any requests, as long as they carry an API key of any value.
Environment variables:
FASTAPI_SQLMODEL_SECURITY_SECRET
: Secret administrator key
FASTAPI_SQLMODEL_SECURITY_HIDE_DOCS
: Whether or not to hide the API key related endpoints from the documentation
FASTAPI_SQLMODEL_SECURITY_AUTOMATIC_EXPIRATION
: Duration, in days, until an API key is deemed expired
poetry install
poetry shell
pre-commit install
pytest
The attached docker image runs a test app on localhost:8080
with secret key TEST_SECRET
. Run it with:
docker-compose build && docker-compose up
FAQs
API key-based security for FastAPI using SQLModel
We found that fastapi-sqlmodel-security 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.
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.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.