-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add new mutator for big list of naughty strings
- Loading branch information
Showing
2 changed files
with
825 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
...ain/java/com/endava/cats/fuzzer/special/mutators/impl/BigListOfNaughtyStringsMutator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.endava.cats.fuzzer.special.mutators.impl; | ||
|
||
import com.endava.cats.fuzzer.special.mutators.api.BodyMutator; | ||
import com.endava.cats.util.CatsUtil; | ||
import com.endava.cats.util.JsonUtils; | ||
import jakarta.inject.Singleton; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Sends random naughty strings in json fields. | ||
*/ | ||
@Singleton | ||
public class BigListOfNaughtyStringsMutator implements BodyMutator { | ||
private static final List<String> NAUGHTY_STRINGS; | ||
|
||
static { | ||
try (InputStream inputStream = BigListOfNaughtyStringsMutator.class.getClassLoader().getResourceAsStream("blns.txt"); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(Objects.requireNonNull(inputStream), StandardCharsets.UTF_8))) { | ||
|
||
NAUGHTY_STRINGS = reader.lines() | ||
.filter(StringUtils::isNotBlank) | ||
.filter(Predicate.not(line -> line.startsWith("#"))) | ||
.collect(Collectors.toList()); | ||
|
||
} catch (Exception e) { | ||
throw new RuntimeException("Failed to read BLNS resource file", e); | ||
} | ||
} | ||
|
||
@Override | ||
public String mutate(String inputJson, String selectedField) { | ||
String randomNaughtyString = CatsUtil.selectRandom(NAUGHTY_STRINGS); | ||
if (CatsUtil.random().nextBoolean()) { | ||
randomNaughtyString = randomNaughtyString + JsonUtils.getVariableFromJson(inputJson, selectedField); | ||
} | ||
return CatsUtil.justReplaceField(inputJson, selectedField, randomNaughtyString).json(); | ||
} | ||
|
||
@Override | ||
public String description() { | ||
return "replace field with random naughty strings"; | ||
} | ||
} |
Oops, something went wrong.