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

Removed comments + added line highlight at position of keyword #268

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

Expand All @@ -30,14 +32,14 @@ public void execute(Context ctx, ResultBuilder result) {
}

String code;

try {
code = Files.readString(Path.of(solutionFile.get(0).getPath()));
} catch (IOException e) {
result.genericFailure(this, e);
return;
}

code = removeComments(code);
if (!checkPackageName(code, result)) return;

if (!checkForKeywords(code, result)) return;
Expand All @@ -46,7 +48,7 @@ public void execute(Context ctx, ResultBuilder result) {
private boolean checkPackageName(String code, ResultBuilder result) {
Pattern pattern = Pattern.compile("^\\s*package\\s+delft\\s*;.*", Pattern.DOTALL);
if (!pattern.matcher(code).find()) {
result.compilationSecurityFail("The package name of your solution must be \"delft\"");
result.compilationSecurityFail("The package name of your solution must be \"delft\"", null);
return false;
}

Expand Down Expand Up @@ -90,14 +92,32 @@ private boolean checkForKeywords(String code, ResultBuilder result) {
);
for (String keyword : keywords.keySet()) {
if (code.contains(keyword)) {
result.compilationSecurityFail(keywords.get(keyword));
return false;
String[] lines = code.split("\\n");
// Split lines and search every line for keyword
for (int lineNumber = 0; lineNumber < lines.length; lineNumber++)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we have a test showing what this should do? :)

if (lines[lineNumber].contains(keyword)) {
// Adding 1 to lineNumber to convert from zero-based index to one-based index
result.compilationSecurityFail(keywords.get(keyword), Optional.of(lineNumber+1));
return false;
}
}
}

return true;
}

public String removeComments(String code) {
alexcojocaru2002 marked this conversation as resolved.
Show resolved Hide resolved
// Remove single-line comments (//)
code = code.replaceAll("//.*", "");

// Remove multi-line comments (/* */)
Pattern pattern = Pattern.compile("/\\*.*?\\*/", Pattern.DOTALL);
Matcher matcher = pattern.matcher(code);
code = matcher.replaceAll("");

return code;
}

@Override
public boolean equals(Object other) {
return other instanceof SourceCodeSecurityCheckStep;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ public void compilationFail(List<Diagnostic<? extends JavaFileObject>> errors) {
this.compilation = CompilationResult.compilationFail(compilationErrors);
}

public void compilationSecurityFail(String message) {
public void compilationSecurityFail(String message, Optional<Integer> lineNumber) {
alexcojocaru2002 marked this conversation as resolved.
Show resolved Hide resolved
this.compilation = CompilationResult.compilationFail(List.of(
new CompilationErrorInfo("Solution.java", 1, message)
new CompilationErrorInfo("Solution.java", lineNumber.orElse(1), message) // Highlight first line if the line number does not exist
));
}

Expand Down
Loading