From 5c0c18eba037d38c4e90155982dc3cd161977420 Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Mon, 14 Feb 2022 19:59:36 -0800 Subject: [PATCH] Add email validation on collaborators. --- wise-webapp/pom.xml | 16 +++++----- .../wisemapping/rest/MindmapController.java | 30 +++++++++++++++++-- .../webapp/WEB-INF/wisemapping-servlet.xml | 6 ---- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/wise-webapp/pom.xml b/wise-webapp/pom.xml index c64e66c9..4377a8b4 100644 --- a/wise-webapp/pom.xml +++ b/wise-webapp/pom.xml @@ -213,12 +213,6 @@ commons-dbcp2 2.9.0 - - commons-fileupload - commons-fileupload - 1.2.1 - runtime - org.hibernate hibernate-ehcache @@ -240,6 +234,12 @@ 1.2.17 compile + + + commons-validator + commons-validator + 1.7 + org.hsqldb @@ -250,7 +250,7 @@ com.fasterxml.jackson.core jackson-databind - 2.11.3 + 2.13.1 javax.servlet @@ -277,7 +277,7 @@ org.apache.httpcomponents fluent-hc - 4.5.5 + 4.5.13 junit diff --git a/wise-webapp/src/main/java/com/wisemapping/rest/MindmapController.java b/wise-webapp/src/main/java/com/wisemapping/rest/MindmapController.java index 65e7a05e..dd918e93 100644 --- a/wise-webapp/src/main/java/com/wisemapping/rest/MindmapController.java +++ b/wise-webapp/src/main/java/com/wisemapping/rest/MindmapController.java @@ -27,6 +27,7 @@ import com.wisemapping.rest.model.*; import com.wisemapping.security.Utils; import com.wisemapping.service.*; import com.wisemapping.validator.MapInfoValidator; +import org.apache.commons.validator.routines.EmailValidator; import org.apache.log4j.Logger; import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Autowired; @@ -296,7 +297,14 @@ public class MindmapController extends BaseController { // Compare one by one if some of the elements has been changed .... final Set collabsToRemove = new HashSet<>(mindMap.getCollaborations()); for (RestCollaboration restCollab : restCollabs.getCollaborations()) { - final Collaboration collaboration = mindMap.findCollaboration(restCollab.getEmail()); + final String email = restCollab.getEmail(); + + // Is a valid email address ? + if (!EmailValidator.getInstance().isValid(email)) { + throw new IllegalArgumentException(email + " is not valid email address"); + } + + final Collaboration collaboration = mindMap.findCollaboration(email); // Validate role format ... String roleStr = restCollab.getRole(); if (roleStr == null) { @@ -333,6 +341,17 @@ public class MindmapController extends BaseController { throw new IllegalArgumentException("No enough permissions"); } + // Is valid email address ? + final EmailValidator emailValidator = EmailValidator.getInstance(); + restCollabs + .getCollaborations() + .forEach(collab -> { + // Is a valid email address ? + if (!emailValidator.isValid(collab.getEmail())) { + throw new IllegalArgumentException(collab.getEmail() + " is not valid email address"); + } + }); + // Has any role changed ?. Just removed it. final Map mapsByEmail = mindMap .getCollaborations() @@ -432,6 +451,12 @@ public class MindmapController extends BaseController { public void deleteCollabByEmail(@PathVariable int id, @RequestParam(required = false) String email) throws IOException, WiseMappingException { logger.debug("Deleting permission for email:" + email); + // Is a valid email address ? + final EmailValidator emailValidator = EmailValidator.getInstance(); + if (!emailValidator.isValid(email)) { + throw new IllegalArgumentException(email + " is not valid email address"); + } + final Mindmap mindmap = findMindmapById(id); final User user = Utils.getUser(); @@ -495,6 +520,7 @@ public class MindmapController extends BaseController { mindmapService.removeMindmap(mindmap, user); } } + @RequestMapping(method = RequestMethod.POST, value = "/maps", consumes = {"application/xml", "application/json"}) @ResponseStatus(value = HttpStatus.CREATED) public void createMap(@RequestBody(required = false) String mapXml, @NotNull HttpServletResponse response, @RequestParam(required = false) String title, @RequestParam(required = false) String description) throws IOException, WiseMappingException { @@ -506,7 +532,7 @@ public class MindmapController extends BaseController { if (description != null && !description.isEmpty()) { mindmap.setDescription(description); - }else { + } else { mindmap.setDescription("description"); } diff --git a/wise-webapp/src/main/webapp/WEB-INF/wisemapping-servlet.xml b/wise-webapp/src/main/webapp/WEB-INF/wisemapping-servlet.xml index 073c65bb..62da4794 100644 --- a/wise-webapp/src/main/webapp/WEB-INF/wisemapping-servlet.xml +++ b/wise-webapp/src/main/webapp/WEB-INF/wisemapping-servlet.xml @@ -64,12 +64,6 @@ class="org.springframework.web.servlet.i18n.SessionLocaleResolver"> - - - - -