Sanitize CSV on import (#101)

* Sanitize CSV on import

* update readme
This commit is contained in:
Aine 2024-10-25 11:52:26 +03:00 committed by GitHub
parent 791ae2c869
commit 44e8b82412
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 13 additions and 1 deletions

View file

@ -84,6 +84,7 @@ The following changes are already implemented:
* [Add user profile to the top menu](https://github.com/etkecc/synapse-admin/pull/80) * [Add user profile to the top menu](https://github.com/etkecc/synapse-admin/pull/80)
* [Enable visual customization](https://github.com/etkecc/synapse-admin/pull/81) * [Enable visual customization](https://github.com/etkecc/synapse-admin/pull/81)
* [Fix room state events display](https://github.com/etkecc/synapse-admin/pull/100) * [Fix room state events display](https://github.com/etkecc/synapse-admin/pull/100)
* [Sanitize CSV on import](https://github.com/etkecc/synapse-admin/pull/101)
_the list will be updated as new changes are added_ _the list will be updated as new changes are added_

View file

@ -121,6 +121,7 @@ const FilePicker = () => {
const verifyCsv = ({ data, meta, errors }: ParseResult<ImportLine>, { setValues, setStats, setError }) => { const verifyCsv = ({ data, meta, errors }: ParseResult<ImportLine>, { setValues, setStats, setError }) => {
/* First, verify the presence of required fields */ /* First, verify the presence of required fields */
meta.fields = meta.fields?.map(f => f.trim().toLowerCase());
const missingFields = expectedFields.filter(eF => !meta.fields?.find(mF => eF === mF)); const missingFields = expectedFields.filter(eF => !meta.fields?.find(mF => eF === mF));
if (missingFields.length > 0) { if (missingFields.length > 0) {
@ -147,6 +148,15 @@ const FilePicker = () => {
}; };
const errorMessages = errors.map(e => e.message); const errorMessages = errors.map(e => e.message);
// sanitize the data first
data = data.map(line => {
const newLine = {} as ImportLine;
for (const [key, value] of Object.entries(line)) {
newLine[key.trim().toLowerCase()] = value;
}
return newLine;
});
// process the data
data.forEach((line, idx) => { data.forEach((line, idx) => {
if (line.user_type === undefined || line.user_type === "") { if (line.user_type === undefined || line.user_type === "") {
stats.user_types.default++; stats.user_types.default++;
@ -173,6 +183,7 @@ const FilePicker = () => {
line[f] = true; // we need true booleans instead of strings line[f] = true; // we need true booleans instead of strings
} else { } else {
if (line[f] !== "false" && line[f] !== "") { if (line[f] !== "false" && line[f] !== "") {
console.log("invalid value", line[f], "for field " + f + " in row " + idx);
errorMessages.push( errorMessages.push(
translate("import_users.error.invalid_value", { translate("import_users.error.invalid_value", {
field: f, field: f,

View file

@ -13,7 +13,7 @@ const LoginFormBox = styled(Box)(({ theme }) => ({
backgroundSize: "cover", backgroundSize: "cover",
[`& .card`]: { [`& .card`]: {
maxWidth: "30rem", width: "30rem",
marginTop: "6rem", marginTop: "6rem",
marginBottom: "6rem", marginBottom: "6rem",
}, },