-
Notifications
You must be signed in to change notification settings - Fork 547
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
Changes from 4 commits
fecd7b6
7285ac6
16fab74
9475fd8
ee5c404
01b5d40
777adca
57d4374
62e3117
73a0f95
ae7d7ce
602b5ec
b8c5469
6c62c9e
9d86429
1f0b804
50c1cf2
47c0f05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -137,7 +137,12 @@ public static String parseStringOrThrowError(JsonObject element, String fieldNam | |
if (!stringified.contains("\"")) { | ||
throw new Exception(); | ||
} | ||
return ((JsonObject) element).get(fieldName).getAsString(); | ||
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,})$")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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")); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)); | ||
} | ||
} |
There was a problem hiding this comment.
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
andgetCommaSeparatedStringArrayQueryParamOrThrowError
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done