Skip to content
Open
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
23 changes: 15 additions & 8 deletions src/org/labkey/test/tests/list/ListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1654,7 +1654,6 @@ public void testAutoIncrementKeyEncoded()
// setup a list with an auto-increment key that we need to make sure is encoded in the form input
String encodedListName = "autoIncrementEncodeList";
String keyName = "'><script>alert(\":(\")</script>'";
String encodedKeyFieldName = EscapeUtil.getFormFieldName(keyName);
_listHelper.createList(PROJECT_VERIFY, encodedListName, keyName, col("Name", ColumnType.String));
_listHelper.goToList(encodedListName);

Expand All @@ -1666,7 +1665,7 @@ public void testAutoIncrementKeyEncoded()

// insert a new row and verify the key field is not present
table.clickInsertNewRow();
checker().withScreenshot().verifyEquals("List fields on insert form.", List.of("quf_Name"), getQueryFormFieldNames());
checker().withScreenshot().verifyEquals("List fields on insert form.", List.of("Name"), getQueryFormFieldNamesDecoded());
String nameValue = "test";
setFormElement(Locator.name(EscapeUtil.getFormFieldName("Name")), nameValue);
clickButton("Submit");
Expand All @@ -1678,7 +1677,7 @@ public void testAutoIncrementKeyEncoded()

// verify name value can be updated
table.clickEditRow(0);
checker().withScreenshot().verifyEquals("List fields on update form.", List.of("quf_Name", encodedKeyFieldName), getQueryFormFieldNames());
checker().withScreenshot().verifyEquals("List fields on update form.", List.of("Name", keyName), getQueryFormFieldNamesDecoded());
nameValue = "test updated";
setFormElement(Locator.name(EscapeUtil.getFormFieldName("Name")), nameValue);
clickButton("Submit");
Expand Down Expand Up @@ -1728,12 +1727,20 @@ public void testMultiChoiceValues()
_listHelper.deleteList();
}

private List<String> getQueryFormFieldNames()
private List<String> getQueryFormFieldNamesDecoded()
{
return Locator.tag("input").attributeStartsWith("name", "quf_")
.findElements(getDriver()).stream()
.map(el -> el.getDomAttribute("name"))
.toList();
ArrayList<String> ret = new ArrayList<>();
Locator.tag("input").attributeStartsWith("name", "quf_")
.findElements(getDriver()).stream()
.map(el -> el.getDomAttribute("name"))
.map(s -> s.substring(4))
.forEach(name -> ret.add(name));
Locator.tag("input").attributeStartsWith("name", "%_quf_")
.findElements(getDriver()).stream()
.map(el -> el.getDomAttribute("name"))
.map(name -> EscapeUtil.decode(name.substring(6)))
.forEach(name -> ret.add(name));
return ret;
}

private void viewRawTableMetadata(String listName)
Expand Down
24 changes: 15 additions & 9 deletions src/org/labkey/test/util/EscapeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.labkey.test.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.apache.poi.ss.util.WorkbookUtil;
import org.eclipse.jetty.util.URIUtil;
Expand Down Expand Up @@ -271,20 +272,25 @@ public static String getFormFieldName(String columnName, boolean multiValue)
return getFormFieldName(columnName, (multiValue ? "[]" : "") + FORM_FIELD_PREFIX);
}



static final String FIELD_ENCODED_PREFIX = "%_";

/**
* Escapes special characters in a column name to be used as a form field name.
* See associated {@link org.labkey.api.query.QueryUpdateForm#getFormFieldName}
* See associated {@link org.labkey.api.util.PageFlowUtil#encodeFormName}
*/
public static String getFormFieldName(String columnName, @Nullable String prefix)
{
StringBuilder fieldName = new StringBuilder();
for (char c : columnName.toCharArray())
{
if (SPECIAL_CHARS.indexOf(c) >= 0)
fieldName.append(BACKSLASH);
fieldName.append(c);
}
String name = Objects.toString(prefix,"") + columnName;

return prefix == null ? fieldName.toString() : prefix + fieldName;
final String escapeChar = "%";
final String problemChars = "\\\"";
final String unclean = escapeChar + problemChars;
if (!StringUtils.containsAny(name, unclean))
return name;
var ret = FIELD_ENCODED_PREFIX + encode(name);
assert !StringUtils.containsAny(ret, problemChars);
return ret;
}
}
6 changes: 3 additions & 3 deletions src/org/labkey/test/util/TestDataGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -670,16 +670,16 @@ public static String randomFieldName(@NotNull String part, @Nullable Integer num
+ WIDE_PLACEHOLDER + REPEAT_PLACEHOLDER + ALL_CHARS_PLACEHOLDER;

int currentTries = 0;
RandomName randomFieldName = randomName(part, getNumChars(numStartChars, 5), getNumChars(numEndChars, 50), chars, exclusion);
RandomName randomFieldName = randomName(part, getNumChars(numStartChars, 5), getNumChars(numEndChars, 48), chars, exclusion);
while ((maxLength != null && randomFieldName.name().length() > maxLength) || isDomainAndFieldNameInvalid(_domainKind, null, randomFieldName))
{
randomFieldName = randomName(part, getNumChars(numStartChars, 5), getNumChars(numEndChars, 50), chars, exclusion);
randomFieldName = randomName(part, getNumChars(numStartChars, 5), getNumChars(numEndChars, 48), chars, exclusion);
if (++currentTries >= MAX_RANDOM_TRIES)
throw new IllegalStateException("Failed to generate a valid field name after " + MAX_RANDOM_TRIES + " tries. Last generated name: " + randomFieldName);
}

TestLogger.log("Generated random field name for domainKind " + _domainKind + ": " + randomFieldName);
return randomFieldName.name();
return randomFieldName.name() + "\"\'";
}

private static boolean isDomainAndFieldNameInvalid(DomainKind domainKind, @Nullable RandomName domainName, @Nullable RandomName fieldName)
Expand Down
Loading