Skip to content

Commit 791a81d

Browse files
committed
Add X-CSRFToken header to fetch API call
1 parent 98eba6e commit 791a81d

4 files changed

Lines changed: 10 additions & 5 deletions

File tree

src/controllers/FibonacciController.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ async def fibonacci() -> ResponseReturnValue:
5757
await flash("Please provide a numeric value 'N' for the fibonacci number!", "danger")
5858
else:
5959
logging.warning(f"Invalid request method: {request.method}!")
60-
response = await Respond("fibonacci.html", title="Welcome to Python Flask Fibonacci calculator", fibonacci=fibonacci)
61-
return response
60+
return await Respond("fibonacci.html", title="Welcome to Python Flask Fibonacci calculator", fibonacci=fibonacci)
6261

6362
def _fib(n):
6463
# n = input(n)

src/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def create_app() -> Quart:
5555
app = cors(app, allow_credentials=True, allow_origin="https://localhost")
5656
configure_uploads(app, images)
5757
# https://quart-wtf.readthedocs.io/en/stable/how_to_guides/configuration.html
58-
#csrf = CSRFProtect(app)
58+
CSRFProtect(app)
5959
bcrypt.init_app(app)
6060
db.init_app(app)
6161
# https://hypercorn.readthedocs.io/en/stable/how_to_guides/http_https_redirect.html

src/view/templates/base.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
<div class="content" style="margin-top: 10px">
7676
{% block content %}
7777
{% endblock %}
78-
<div class="container" style="margin-top: 10px">
78+
<div id="notification" class="container" style="margin-top: 10px">
7979
{% with messages = get_flashed_messages(with_categories=true) %}
8080
{% if messages %}
8181
<hr>

src/view/templates/chat.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{% extends "base.html" %}
22
{% block content %}
33
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/chat.css') }}">
4+
<meta name="csrf-token" content="{{ csrf_token() }}">
45
<div class="container" style="margin-top: 10px">
56
<div id="chat-room-widget" style="margin-top: 10px">
67
<div id="messages">
78
</div>
89
<form id="chatForm" name="chatForm" action="{{url_for('chat.invoke')}}" method="post" enctype="multipart/form-data">
10+
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
911
<div id="message-box">
1012
<div class="col-md-8">
1113
<textarea id="prompt" name="message" placeholder="How can I help?" class="form-control" rows="1" required onkeyup="stoppedTyping()"></textarea>
@@ -39,6 +41,7 @@
3941
// Select your input type file and store it in a variable
4042
document.querySelector("#chatForm").addEventListener("submit", async (e) => {
4143
e.preventDefault();
44+
var csrf_token = $('meta[name=csrf-token]').attr('content');
4245
const prompt = document.querySelector("#prompt").value;
4346
const image = document.querySelector("#image");
4447
const receipt = document.querySelector("#checkReceipt").checked;
@@ -48,7 +51,7 @@
4851
form.append("prompt", prompt);
4952
form.append("receipt", receipt);
5053
if (image && image.files.length && image.files[0]) {
51-
console.log(`Image name: ${image.files[0].name}, size: ${image.files[0].size}, type: ${image.files[0].type}`);
54+
//console.log(`Image name: ${image.files[0].name}, size: ${image.files[0].size}, type: ${image.files[0].type}`);
5255
form.append("image", image.files[0]);
5356
}// else
5457
//console.log("No file selected!");
@@ -77,6 +80,9 @@
7780
const response = await fetch('invoke', { /* Without forward-slash prefix! */
7881
method: 'POST',
7982
//headers: { 'Content-Type': 'multipart/form-data' }, Do NOT declare Content-Type: multipart/form-data in request header
83+
headers: {
84+
"X-CSRFToken": csrf_token,
85+
},
8086
body: form
8187
});
8288
const data = await response.json();

0 commit comments

Comments
 (0)