Browse Source

BUGFIX: Fixed text length not being verified on todo update

master
parent
commit
5c7f0fbf18
  1. 10
      pages/category.html
  2. 20
      src/server/endpoints.go
  3. 4
      src/server/validation.go

10
pages/category.html

@ -57,7 +57,6 @@
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div> </div>
<div class="modal-body"> <div class="modal-body">
<p id="modalToDoErrorMessage"></p>
<div> <div>
<strong>{{index .Translation "category modal todo text"}}</strong> <strong>{{index .Translation "category modal todo text"}}</strong>
<span id="modalTodoTextDisplay"></span> <span id="modalTodoTextDisplay"></span>
@ -86,6 +85,7 @@
{{ index .Translation "category file download button"}} {{ index .Translation "category file download button"}}
</button> </button>
</div> </div>
<p id="modalToDoErrorMessage" class="text-danger fw-bold"></p>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal" onclick="toggleEditMode(false);">{{index .Translation "category modal close button"}}</button> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal" onclick="toggleEditMode(false);">{{index .Translation "category modal close button"}}</button>
@ -126,7 +126,6 @@
<div class="row g-3 align-items-center"> <div class="row g-3 align-items-center">
<div class="col-md"> <div class="col-md">
<label for="newTodoText" class="form-label">{{index .Translation "category todo text"}}</label> <label for="newTodoText" class="form-label">{{index .Translation "category todo text"}}</label>
<!-- <input type="text" class="form-control" id="newTodoText" placeholder='{{index .Translation "category enter todo text"}}' required> -->
<textarea class="form-control" id="newTodoText" placeholder='{{index .Translation "category enter todo text"}}' required></textarea> <textarea class="form-control" id="newTodoText" placeholder='{{index .Translation "category enter todo text"}}' required></textarea>
</div> </div>
<div class="col-md"> <div class="col-md">
@ -362,7 +361,6 @@ async function saveEditedTodo() {
document.getElementById('modalTodoDueDisplay').innerText = updatedDue; document.getElementById('modalTodoDueDisplay').innerText = updatedDue;
const updatedDueUnix = Date.parse(updatedDue) / 1000; const updatedDueUnix = Date.parse(updatedDue) / 1000;
let response = await updateTodo(viewedTodoID, {"text":updatedText, "dueUnix":updatedDueUnix, "isDone":false}); let response = await updateTodo(viewedTodoID, {"text":updatedText, "dueUnix":updatedDueUnix, "isDone":false});
if (!response.ok) { if (!response.ok) {
document.getElementById("modalToDoErrorMessage").innerText = await response.text(); document.getElementById("modalToDoErrorMessage").innerText = await response.text();
@ -371,7 +369,6 @@ async function saveEditedTodo() {
let result = await uploadAttachedFile(viewedTodoID); let result = await uploadAttachedFile(viewedTodoID);
if (!result) { if (!result) {
alert("Failed to upload attachment file");
return; return;
} }
@ -410,10 +407,11 @@ async function downloadAttachedFile() {
async function uploadAttachedFile(todoID) { async function uploadAttachedFile(todoID) {
let todoFileInput = document.getElementById("modalFileInput"); let todoFileInput = document.getElementById("modalFileInput");
if (todoFileInput.files.length === 0 ) { if (todoFileInput.files.length === 0 ) {
return false; return true;
} }
if (todoFileInput.files.item(0).size > 3145728) { if (todoFileInput.files.item(0).size > 3145728) {
todoFileInput.setCustomValidity("File size exceeded 3MB"); // todoFileInput.setCustomValidity("File size exceeded 3MB");
document.getElementById("modalToDoErrorMessage").innerText = "File size exceeded 3MB";
return false; return false;
} }

20
src/server/endpoints.go

@ -561,6 +561,18 @@ func (s *Server) EndpointTodoUpdate(w http.ResponseWriter, req *http.Request) {
return return
} }
// Validate
if uint(len([]rune(updatedTodo.Text))) > MaxTodoTextLength {
http.Error(
w,
fmt.Sprintf("Text is too big! Text must be less than %d characters long!", MaxTodoTextLength),
http.StatusBadRequest,
)
return
}
updatedTodo.File = nil
updatedTodo.ID = todoID
// Update // Update
err = s.db.UpdateTodoSoft(todoID, updatedTodo) err = s.db.UpdateTodoSoft(todoID, updatedTodo)
if err != nil { if err != nil {
@ -694,8 +706,12 @@ func (s *Server) EndpointTodoCreate(w http.ResponseWriter, req *http.Request) {
} }
// Check if text is too long or not // Check if text is too long or not
if uint(len(newTodo.Text)) > MaxTodoTextLength { if uint(len([]rune(newTodo.Text))) > MaxTodoTextLength {
http.Error(w, "Text is too big!", http.StatusBadRequest) http.Error(
w,
fmt.Sprintf("Text is too big! Text must be less than %d characters long!", MaxTodoTextLength),
http.StatusBadRequest,
)
return return
} }

4
src/server/validation.go

@ -31,8 +31,8 @@ import (
const ( const (
MinimalEmailLength uint = 3 MinimalEmailLength uint = 3
MinimalPasswordLength uint = 5 MinimalPasswordLength uint = 5
MaxEmailLength uint = 60 MaxEmailLength uint = 50
MaxPasswordLength uint = 250 MaxPasswordLength uint = 50
MaxTodoTextLength uint = 250 MaxTodoTextLength uint = 250
MaxTodoFileSizeBytes uint = 3145728 // 3MB MaxTodoFileSizeBytes uint = 3145728 // 3MB
) )

Loading…
Cancel
Save