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

Update Input Validation #1644

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def exchange_create():
api_type_name = data.get("api")

if not re.match("^[A-Z0-9_]+$", bank):
abort(400, "Field `bank` must match /^[A-Z0-9_]$/")
abort(400, "Field `bank` must match /^[A-Z0-9_]+$/")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was a typo? Updated it because the message doesn't match regex actually being called?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, typo. We can also explain it in english if that's easier.


if not isinstance(api_json, dict):
abort(400, "Field `api_json` must be object")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{# Copyright (c) Meta Platforms, Inc. and affiliates. #}
<h3> Banks </h3>
<div class="w-50 my-1">
<form action="/ui/create_bank" method="post" enctype="multipart/form-data">
<form action="/ui/create_bank" method="post" enctype="multipart/form-data" id="banks_grid_form">
<div class="input-group mb-3">
<input type="text" class="form-control" name="bank_name" placeholder="Name" required>
<button type="submit" class="btn btn-outline-success">Create New Bank</button>
<input type="text" class="form-control" name="bank_name" placeholder="Name" id="create_bank_bank_name" required>
<button type="submit" class="btn btn-outline-success" id="create_bank_submit">Create New Bank</button>
</div>
<span id="bank-name-error" class="text-danger"></span>
</form>
</div>
<div class="row row-cols-1 row-cols-md-4 g-4">
Expand All @@ -21,4 +22,49 @@
</div>
</div>
{% endfor %}
</div>
</div>
<script>
document.getElementById('create_bank_bank_name').addEventListener('blur', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fancy!

const bankNameField = this;
// auto uppercase and trim input
bankNameField.value = bankNameField.value.trim().toUpperCase();
const submitButton = document.getElementById('create_bank_submit');
submitButton.disabled = true;
const errorSpan = document.getElementById('bank-name-error');
errorSpan.textContent = ''; // Clear previous error message
const regex = /^[A-Z0-9_]+$/;

// Validate the input
if (!regex.test(bankNameField.value)) {
errorSpan.textContent = 'Bank name must be all uppercase and snake cased';
bankNameField.classList.remove("is-valid");
bankNameField.classList.add("is-invalid");
} else {
submitButton.disabled = false;
bankNameField.classList.remove("is-invalid");
bankNameField.classList.add("is-valid");
}
});

document.getElementById("banks_grid_form").addEventListener("submit", async event => {
event.preventDefault();

fetch(`/c/banks`, {
method: 'POST',
body: JSON.stringify({name: document.getElementById('create_bank_bank_name').value}),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
})
.then(async response => {
if (response.ok) {
window.location.reload();
} else {
json = await response.json()
alert("Create Bank failed! " + response.statusText + " : " + (json['message'] ?? '<no message>)'))
}
});
})

</script>
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,69 @@
<label for="exchange_create_form_bank" class="form-label">New bank name</label>
<input type="text" class="form-control" name="bank" id="exchange_create_form_bank"
placeholder="BANK_NAME" required>
<span id="bank-name-error" class="text-danger"></span>
</div>
<div class="mb-3">
<label for="exchange_create_form_json" class="form-label">Type-specific JSON</label>
<textarea type="text" class="form-control" name="api_json" id="exchange_create_form_json" rows=5
placeholder="{}" onblur="validatePrettyFormJSON(this)"></textarea>
<span id="json-error" class="text-danger"></span>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Create</button>
<button type="submit" class="btn btn-primary" id="exchange_create_form_submit">Create</button>
</div>
</form>
</div>
</div>
</div>
<script>
function validatePrettyFormJSON(elem) {
const errorSpan = document.getElementById('json-error');
errorSpan.textContent = ''; // Clear previous error message
try {
const obj = JSON.parse(elem.value || "{}");
const pretty = JSON.stringify(obj, undefined, 4);
elem.value = pretty;
elem.classList.remove("is-invalid");
elem.classList.add("is-valid");
submitButton.disabled = false;
} catch (e) {
errorSpan.textContent = 'Invalid JSON';
elem.classList.remove("is-valid");
elem.classList.add("is-invalid");
submitButton.disabled = true;
}
}

document.getElementById('exchange_create_form_bank').addEventListener('blur', function() {
const bankNameField = this;
// auto uppercase and trim input
bankNameField.value = bankNameField.value.trim().toUpperCase();
const submitButton = document.getElementById('exchange_create_form_submit');
submitButton.disabled = true;
const errorSpan = document.getElementById('bank-name-error');
errorSpan.textContent = ''; // Clear previous error message

const regex = /^[A-Z0-9_]+$/;

// Validate the input
if (bankNameField.value.trim() === '') {
errorSpan.textContent = 'This field is required!';
bankNameField.classList.remove("is-valid");
bankNameField.classList.add("is-invalid");
} else if (!regex.test(bankNameField.value)) {
errorSpan.textContent = 'Bank name must be all uppercase and snake cased';
bankNameField.classList.remove("is-valid");
bankNameField.classList.add("is-invalid");
} else {
submitButton.disabled = false;
bankNameField.classList.remove("is-invalid");
bankNameField.classList.add("is-valid");
}
});

document.getElementById("exchange_create_form").addEventListener("submit", async event => {
event.preventDefault();
const data = new FormData(event.target);
Expand Down
Loading