-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
84 additions
and
7 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
arc-utils/src/main/java/fr/insee/arc/utils/security/GuiInputSecurity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package fr.insee.arc.utils.security; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class GuiInputSecurity { | ||
|
||
private GuiInputSecurity() { | ||
throw new IllegalStateException("Utility class"); | ||
} | ||
|
||
/** | ||
* format an input field as a database identifier | ||
* @param guiInput | ||
* @return | ||
*/ | ||
public static String formatAsDatabaseIdentifier(String guiInput) { | ||
|
||
if (guiInput==null) | ||
{ | ||
return null; | ||
} | ||
|
||
return guiInput.replaceAll("[^\\w$]", "") // remove all but world and $ symbol. worlds are 0-9a-zA-Z and _ | ||
.replaceFirst("^[_$]*", "") // remove begin trailings $ and _ | ||
.replaceFirst("[_$]*$", "") // remove end trailings $ and _ | ||
; | ||
} | ||
|
||
public static List<String> formatAsDatabaseIdentifier(List<String> guiInputs) { | ||
if (guiInputs==null) | ||
{ | ||
return null; | ||
} | ||
|
||
return guiInputs.stream().map(t->formatAsDatabaseIdentifier(t)).collect(Collectors.toList()); | ||
} | ||
|
||
|
||
|
||
|
||
} |
36 changes: 36 additions & 0 deletions
36
arc-utils/src/test/java/fr/insee/arc/utils/security/GuiInputSecurityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package fr.insee.arc.utils.security; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.junit.Test; | ||
|
||
public class GuiInputSecurityTest { | ||
|
||
@Test | ||
public void testFormatAsDatabaseIdentifier() { | ||
|
||
assertEquals("var_table_metier",GuiInputSecurity.formatAsDatabaseIdentifier("var_table_metier")); | ||
|
||
// remove space and ; and other chars | ||
assertEquals("var_tablemetier",GuiInputSecurity.formatAsDatabaseIdentifier("var_table metier")); | ||
assertEquals("dropdatabasetoto",GuiInputSecurity.formatAsDatabaseIdentifier("drop database toto;")); | ||
assertEquals("var_table_metier$10",GuiInputSecurity.formatAsDatabaseIdentifier("var_table_metier$10")); | ||
assertEquals("var_tableMETIER",GuiInputSecurity.formatAsDatabaseIdentifier("var_table METIER")); | ||
|
||
// remove trailing $ and _ | ||
assertEquals("var_table_metier",GuiInputSecurity.formatAsDatabaseIdentifier("__var_table_metier$$")); | ||
assertEquals("var_table_metier",GuiInputSecurity.formatAsDatabaseIdentifier("$var_table_metier_$")); | ||
assertEquals("var_table_metier",GuiInputSecurity.formatAsDatabaseIdentifier("$_$var_table_metier$__;")); | ||
|
||
|
||
List<String> guiInputs = Arrays.asList("__var_table_metier$$", "drop database toto;"); | ||
List<String> guiInputsReformat = GuiInputSecurity.formatAsDatabaseIdentifier(guiInputs); | ||
assertEquals("var_table_metier", guiInputsReformat.get(0)); | ||
assertEquals("dropdatabasetoto", guiInputsReformat.get(1)); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters