From f46818363171a288c2ffe7b606f702de589b8952 Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Fri, 26 Jan 2024 14:30:14 +0000 Subject: [PATCH] Sanitize/Validate name field (#20) * escape name * add email pydantic validation (API) * format prettier * don't allow slash on email also * make regex const * use string literals * make get ticket a POST * email regex Co-authored-by: Vlad Stan --- models.py | 4 +- templates/events/display.html | 94 +++++++++----- templates/events/index.html | 227 ++++++++++++++++++++++++++-------- views_api.py | 9 +- 4 files changed, 252 insertions(+), 82 deletions(-) diff --git a/models.py b/models.py index 94869a5..2798c12 100644 --- a/models.py +++ b/models.py @@ -1,5 +1,5 @@ from fastapi import Query -from pydantic import BaseModel +from pydantic import BaseModel, EmailStr from typing import Optional @@ -17,7 +17,7 @@ class CreateEvent(BaseModel): class CreateTicket(BaseModel): name: str - email: str + email: EmailStr class Event(BaseModel): diff --git a/templates/events/display.html b/templates/events/display.html index 78f2225..60ec944 100644 --- a/templates/events/display.html +++ b/templates/events/display.html @@ -13,14 +13,33 @@

{{ event_name }}

Buy Ticket
- - + +
- Submit - Cancel + type="submit" + >Submit + Cancel
@@ -28,8 +47,15 @@
Buy Ticket
- Link to your - ticket! + Link to your ticket!

You'll be redirected in a few moments...

@@ -37,19 +63,27 @@
Buy Ticket
- +
- Copy invoice + Copy invoice Close
@@ -108,20 +142,27 @@
Buy Ticket
dismissMsg() clearInterval(paymentChecker) - setTimeout(function () { }, 10000) + setTimeout(function () {}, 10000) }, + nameValidation(val) { + const regex = /[`!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/g + return ( + !regex.test(val) || + 'Please enter valid name. No special character allowed.' + ) + }, + emailValidation(val) { + let regex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/g + return !regex.test(val) || 'Please enter valid email.' + }, + Invoice: function () { var self = this axios - - .get( - '/events/api/v1/tickets/' + - '{{ event_id }}' + - '/' + - self.formDialog.data.name + - '/' + - self.formDialog.data.email - ) + .post(`/events/api/v1/tickets/{{ event_id }}`, { + name: self.formDialog.data.name, + email: self.formDialog.data.email + }) .then(function (response) { self.paymentReq = response.data.payment_request self.paymentCheck = response.data.payment_hash @@ -140,9 +181,7 @@
Buy Ticket
paymentChecker = setInterval(function () { axios .post( - '/events/api/v1/tickets/' + - '{{ event_id }}/' + - self.paymentCheck, + `/events/api/v1/tickets/{{ event_id }}/${self.paymentCheck}`, { event: '{{ event_id }}', event_name: '{{ event_name }}', @@ -171,12 +210,11 @@
Buy Ticket
self.ticketLink = { show: true, data: { - link: '/events/ticket/' + res.data.ticket_id + link: `/events/ticket/${res.data.ticket_id}` } } setTimeout(function () { - window.location.href = - '/events/ticket/' + res.data.ticket_id + window.location.href = `/events/ticket/${res.data.ticket_id}` }, 5000) } }) @@ -192,4 +230,4 @@
Buy Ticket
} }) -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/templates/events/index.html b/templates/events/index.html index 9bd1e4c..cf3bbba 100644 --- a/templates/events/index.html +++ b/templates/events/index.html @@ -4,7 +4,9 @@
- New Event + New Event @@ -15,11 +17,19 @@
Events
- Export to CSV + Export to CSV
- + {% raw %}