IDEMPIERE-5348 : Validation of filenames (#1398)

This commit is contained in:
Nicolas Micoud 2022-07-18 15:26:34 +02:00 committed by GitHub
parent 090893e051
commit e8b2a94b3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -471,11 +471,14 @@ public class FileUtil
throw new IllegalArgumentException("Prefix string \"" + prefix +
"\" too short: length must be at least 3");
}
prefix = Util.setFilenameCorrect(prefix);
if (suffix == null)
suffix = ".tmp";
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String dt = sdf.format(cal.getTime());
String tmpdirname = (directory != null) ? directory.getCanonicalPath() : System.getProperty("java.io.tmpdir");
tmpdirname += System.getProperty("file.separator") + "rpttmp_" + dt + "_" + Env.getContext(Env.getCtx(), Env.AD_SESSION_ID) + System.getProperty("file.separator");

View File

@ -25,6 +25,7 @@ import java.sql.Timestamp;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
import java.text.Normalizer;
import java.text.Normalizer.Form;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
@ -748,4 +749,21 @@ public class Util
}
}
}
/**
* Make the filename correct (updating all unauthorized characters to safe ones)
* @param the filename to check
* @returns the correct filename
*/
public static String setFilenameCorrect(String input) {
String output = Normalizer.normalize(input, Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
output = output.replaceAll("/" , "-");
output = output.replaceAll(":" , "-");
output = output.replaceAll("\\*" , "-");
output = output.replaceAll("<" , "-");
output = output.replaceAll(">" , "-");
output = output.replaceAll("%" , "-");
return output.trim();
}
} // Util