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

normalized input of parseStringOrThrowError method #830

Merged
merged 18 commits into from
Oct 15, 2023
7 changes: 6 additions & 1 deletion src/main/java/io/supertokens/webserver/InputParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,12 @@ public static String parseStringOrThrowError(JsonObject element, String fieldNam
if (!stringified.contains("\"")) {
throw new Exception();
}
return ((JsonObject) element).get(fieldName).getAsString();
Copy link
Contributor

Choose a reason for hiding this comment

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

This function is called for non GET request. We might want to add the trim and toLowerCase (in case of email), to the GET request equivalent as well. The functions are getQueryParamOrThrowError and getCommaSeparatedStringArrayQueryParamOrThrowError

Copy link
Author

Choose a reason for hiding this comment

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

Done

String s = element.get(fieldName).getAsString().trim();

if (s.matches("^(?=.{1,64}@)[A-Za-z0-9_-]+(\\.[A-Za-z0-9_-]+)*@[^-][A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$")) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We use the following regex in our backend SDK: https://github.com/supertokens/supertokens-node/blob/master/lib/ts/recipe/emailpassword/utils.ts#L242.

Might want to make this consistent with that.

Copy link
Author

Choose a reason for hiding this comment

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

Done

s = s.toLowerCase();
}
return s;
} catch (Exception e) {
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
throw new ServletException(
new WebserverAPI.BadRequestException("Field name '" + fieldName + "' is invalid in JSON input"));
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/io/supertokens/test/InputParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,21 @@ public void testParseStringOrJSONNullOrThrowError() throws Exception {
process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}

@Test
public void testParseStringOrThrowError() throws Exception {
JsonObject json = new JsonObject();
json.addProperty("untrimed mixedcase email", "[email protected] ");
json.addProperty("email", "[email protected]");
json.addProperty("untrimed mixedcase text", " TexT ");
json.addProperty("mixedcase text", "TeXt");
json.add("null", null);

assertEquals(InputParser.parseStringOrThrowError(json, "untrimed mixedcase email", false), "[email protected]");
assertEquals(InputParser.parseStringOrThrowError(json, "email", false), "[email protected]");
assertEquals(InputParser.parseStringOrThrowError(json, "untrimed mixedcase text", false), "TexT");
assertEquals(InputParser.parseStringOrThrowError(json, "mixedcase text", false), "TeXt");
assertNull(InputParser.parseStringOrThrowError(json, "null", true));
assertNull(InputParser.parseStringOrThrowError(json, "undefined", true));
}
}