Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't add exempt for Blueprints #134

Open
marban opened this issue Oct 17, 2022 · 3 comments
Open

Can't add exempt for Blueprints #134

marban opened this issue Oct 17, 2022 · 3 comments

Comments

@marban
Copy link

marban commented Oct 17, 2022

Anyone got a hint how to add a Blueprint's route to the _exempt_views set?
Something like csrf._exempt_views.add("bp_user.someroute")
Importing the root @csrf.exempt decorator in the Blueprint doesn't seem to work either

@psycle-dblakemore
Copy link

Not sure whether you're still having an issue with this, but if you are, please provide a code sample. Generally speaking, you should be able to wrap a Blueprint's route function with @csrf.exempt like this:

from flask import Flask
from flask import Blueprint
from flask_seasurf import SeaSurf

app = Flask(__name__)
app.secret_key = "super secret key"
csrf = SeaSurf(app)

custom_blueprint = Blueprint('custom', __name__, url_prefix='/custom')

@csrf.exempt
@custom_blueprint.route('/exempt', methods=['POST'])
def exempt_route():
    return "This route is exempt from CSRF"

@custom_blueprint.route('/nonexempt', methods=['POST'])
def non_exempt_route():
    return "This route is not exempt from CSRF"

app.register_blueprint(custom_blueprint)

However depending on how you have you file structure set up, you might need to do something different. If you can provide some details on your specific problem (code examples), then I might be able to help further.

@eheaton
Copy link

eheaton commented Feb 6, 2024

I actually am running into the same issue, where I can't use the @csrf.exempt decorator in a blueprint. Here is a basic structure of what I'm trying to do:

# my_module/blueprint.py

from flask import Blueprint

bp = Blueprint('my_blueprint', __name__)

@bp.post("/test-blueprint-route")
@bp.csrf.exempt # <-- This throws an error
def post_test_blueprint_route():
    return { "success": True }
# app.py
import Flask
from flask_seasurf import SeaSurf
from my_module.blueprint import bp

app = Flask(__name__)
app.secret_key = "super-secret-key"
app.csrf = SeaSurf(app)
app.register_blueprint(bp, url_prefix="/api")

@app.post("/test-route")
@app.csrf.exempt # <-- This does NOT throw an error
def post_test_route():
   return { "success": True }

The error I get is:

AttributeError: 'Blueprint' object has no attribute 'csrf'

Based on your above example it works because the csrf decorator is defined in the same file as the Blueprint, but that's generally not the case.

1 similar comment
@eheaton
Copy link

eheaton commented Feb 6, 2024

I actually am running into the same issue, where I can't use the @csrf.exempt decorator in a blueprint. Here is a basic structure of what I'm trying to do:

# my_module/blueprint.py

from flask import Blueprint

bp = Blueprint('my_blueprint', __name__)

@bp.post("/test-blueprint-route")
@bp.csrf.exempt # <-- This throws an error
def post_test_blueprint_route():
    return { "success": True }
# app.py
import Flask
from flask_seasurf import SeaSurf
from my_module.blueprint import bp

app = Flask(__name__)
app.secret_key = "super-secret-key"
app.csrf = SeaSurf(app)
app.register_blueprint(bp, url_prefix="/api")

@app.post("/test-route")
@app.csrf.exempt # <-- This does NOT throw an error
def post_test_route():
   return { "success": True }

The error I get is:

AttributeError: 'Blueprint' object has no attribute 'csrf'

Based on your above example it works because the csrf decorator is defined in the same file as the Blueprint, but that's generally not the case.

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

No branches or pull requests

3 participants