-
Notifications
You must be signed in to change notification settings - Fork 76
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
Add validation for threat intel source config #1393
Changes from all commits
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 |
---|---|---|
|
@@ -5,17 +5,15 @@ | |
|
||
package org.opensearch.securityanalytics.threatIntel.common; | ||
|
||
import org.opensearch.securityanalytics.commons.model.IOC; | ||
import org.opensearch.securityanalytics.commons.model.IOCType; | ||
import org.opensearch.securityanalytics.threatIntel.model.IocUploadSource; | ||
import org.opensearch.securityanalytics.threatIntel.model.S3Source; | ||
import org.opensearch.securityanalytics.threatIntel.model.SATIFSourceConfigDto; | ||
import org.opensearch.securityanalytics.threatIntel.model.UrlDownloadSource; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Source config dto validator | ||
|
@@ -24,7 +22,34 @@ public class SourceConfigDtoValidator { | |
public List<String> validateSourceConfigDto(SATIFSourceConfigDto sourceConfigDto) { | ||
List<String> errorMsgs = new ArrayList<>(); | ||
|
||
if (sourceConfigDto.getIocTypes().isEmpty()) { | ||
String nameRegex = "^[a-zA-Z0-9 _-]{1,128}$"; | ||
Pattern namePattern = Pattern.compile(nameRegex); | ||
|
||
int MAX_RULE_DESCRIPTION_LENGTH = 65535; | ||
String descriptionRegex = "^.{0," + MAX_RULE_DESCRIPTION_LENGTH + "}$"; | ||
Pattern descriptionPattern = Pattern.compile(descriptionRegex); | ||
|
||
if (sourceConfigDto.getName() == null || sourceConfigDto.getName().isEmpty()) { | ||
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. @jowg-amazon could we add length restrictions? The frontend uses this validation for names. 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. added validation for names based on how frontend currently validates the name |
||
errorMsgs.add("Name must not be empty"); | ||
} else if (sourceConfigDto.getName() != null && namePattern.matcher(sourceConfigDto.getName()).matches() == false) { | ||
errorMsgs.add("Name must be less than 128 characters and only consist of upper and lowercase letters, numbers 0-9, hyphens, spaces, and underscores"); | ||
} | ||
|
||
if (sourceConfigDto.getFormat() == null || sourceConfigDto.getFormat().isEmpty()) { | ||
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 may need to add length validation for this as well. Unless this gets validated somewhere downstream, the config could be created via backend API with something like 300k characters; which could disrupt functionality. 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. Added length validation |
||
errorMsgs.add("Format must not be empty"); | ||
} else if (sourceConfigDto.getFormat() != null && sourceConfigDto.getFormat().length() > 50) { | ||
errorMsgs.add("Format must be 50 characters or less"); | ||
} | ||
|
||
if (sourceConfigDto.getDescription() != null && descriptionPattern.matcher(sourceConfigDto.getDescription()).matches() == false) { | ||
errorMsgs.add("Description must be " + MAX_RULE_DESCRIPTION_LENGTH + " characters or less"); | ||
} | ||
|
||
if (sourceConfigDto.getSource() == null) { | ||
errorMsgs.add("Source must not be empty"); | ||
} | ||
|
||
if (sourceConfigDto.getIocTypes() == null || sourceConfigDto.getIocTypes().isEmpty()) { | ||
errorMsgs.add("Must specify at least one IOC type"); | ||
} else { | ||
for (String s: sourceConfigDto.getIocTypes()) { | ||
|
@@ -34,34 +59,41 @@ public List<String> validateSourceConfigDto(SATIFSourceConfigDto sourceConfigDto | |
} | ||
} | ||
|
||
switch (sourceConfigDto.getType()) { | ||
case IOC_UPLOAD: | ||
if (sourceConfigDto.isEnabled()) { | ||
errorMsgs.add("Job Scheduler cannot be enabled for IOC_UPLOAD type"); | ||
} | ||
if (sourceConfigDto.getSchedule() != null) { | ||
errorMsgs.add("Cannot pass in schedule for IOC_UPLOAD type"); | ||
} | ||
if (sourceConfigDto.getSource() != null && sourceConfigDto.getSource() instanceof IocUploadSource == false) { | ||
errorMsgs.add("Source must be IOC_UPLOAD type"); | ||
} | ||
break; | ||
case S3_CUSTOM: | ||
if (sourceConfigDto.getSchedule() == null) { | ||
errorMsgs.add("Must pass in schedule for S3_CUSTOM type"); | ||
} | ||
if (sourceConfigDto.getSource() != null && sourceConfigDto.getSource() instanceof S3Source == false) { | ||
errorMsgs.add("Source must be S3_CUSTOM type"); | ||
} | ||
break; | ||
case URL_DOWNLOAD: | ||
if (sourceConfigDto.getSchedule() == null) { | ||
errorMsgs.add("Must pass in schedule for URL_DOWNLOAD source type"); | ||
} | ||
if (sourceConfigDto.getSource() != null && sourceConfigDto.getSource() instanceof UrlDownloadSource == false) { | ||
errorMsgs.add("Source must be URL_DOWNLOAD source type"); | ||
} | ||
break; | ||
if (sourceConfigDto.getType() == null) { | ||
errorMsgs.add("Type must not be empty"); | ||
} else { | ||
switch (sourceConfigDto.getType()) { | ||
case IOC_UPLOAD: | ||
if (sourceConfigDto.isEnabled()) { | ||
errorMsgs.add("Job Scheduler cannot be enabled for IOC_UPLOAD type"); | ||
} | ||
if (sourceConfigDto.getSchedule() != null) { | ||
errorMsgs.add("Cannot pass in schedule for IOC_UPLOAD type"); | ||
} | ||
if (sourceConfigDto.getSource() != null && sourceConfigDto.getSource() instanceof IocUploadSource == false) { | ||
errorMsgs.add("Source must be IOC_UPLOAD type"); | ||
} | ||
if (sourceConfigDto.getSource() instanceof IocUploadSource && ((IocUploadSource) sourceConfigDto.getSource()).getIocs() == null) { | ||
errorMsgs.add("Ioc list must include at least one ioc"); | ||
} | ||
break; | ||
case S3_CUSTOM: | ||
if (sourceConfigDto.getSchedule() == null) { | ||
errorMsgs.add("Must pass in schedule for S3_CUSTOM type"); | ||
} | ||
if (sourceConfigDto.getSource() != null && sourceConfigDto.getSource() instanceof S3Source == false) { | ||
errorMsgs.add("Source must be S3_CUSTOM type"); | ||
} | ||
break; | ||
case URL_DOWNLOAD: | ||
if (sourceConfigDto.getSchedule() == null) { | ||
errorMsgs.add("Must pass in schedule for URL_DOWNLOAD source type"); | ||
} | ||
if (sourceConfigDto.getSource() != null && sourceConfigDto.getSource() instanceof UrlDownloadSource == false) { | ||
errorMsgs.add("Source must be URL_DOWNLOAD source type"); | ||
} | ||
break; | ||
} | ||
} | ||
return errorMsgs; | ||
} | ||
|
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.
As a follow-up item, let's add validation for the refresh frequency.