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

Fixed #81. Properly extracted only the src attribute, improving the detection accuracy of the file matcher. #82

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -67,13 +67,19 @@ private List<String> findScriptUrl(String source) {
List<String> urls = new ArrayList<String>();

for(String line : tokens) {
if(line.contains("<script") || line.contains("<SCRIPT")) { //This precondition avoid applyig an RegEx on every line
Pattern p = Pattern.compile("<[sS][cC][rR][iI][pP][tT][^>]*" + //script tags
"[sS][rR][cC]=" + //src attribute
"[\"']([^>]*)[\"']"); //URL between quotes
if (line.toLowerCase().contains("<script")) { //This precondition avoid applyig an RegEx on every line
Pattern p = Pattern.compile(
"<script[^>]*" + //script tags
"src=" + //src attribute
"(?<src>\"[^\"]*?\"|'[^']*?'|\\S+)", //URL between quotes
Pattern.CASE_INSENSITIVE);

Matcher m = p.matcher(line);
if(m.find()) {
String urlScript = m.group(1);
if (m.find()) {
String src = m.group("src");
String urlScript = src.startsWith("\"") || src.startsWith("'")
? src.substring(1, src.length() - 1) // trim quotes
: src;
urls.add(urlScript);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,53 @@ public void noMatch() throws IOException {
verify(repo).findByHash(DUMMY_SCRIPT_SHA1);
verify(repo).findByFileContent(DUMMY_SCRIPT);
}

@Test
public void uriMatchToHtml() throws IOException {
VulnerabilitiesRepositoryLoader.syncWithOnlineRepository = false;

//Init. mock
VulnerabilitiesRepository repo = mock(VulnerabilitiesRepository.class);
when(repo.findByUri("/js/yolo.js")).thenReturn(ONE_RESULT);

//Call the scanner logic
ScannerFacade scanner = new ScannerFacade(repo);
List<JsLibraryResult> results = scanner.scanHtml((
"<sCrIpT sRc=\"/js/yolo.js\" type=\"text/javascript\"></sCrIpT>" + // double quote
"<sCrIpT sRc='/js/yolo.js' type='text/javascript'></sCrIpT>" + // single quote
"<sCrIpT sRc=/js/yolo.js type=text/javascript></sCrIpT>" // no quote
).getBytes(),0);

//Assertions
assertEquals(results.size(),3,"Expect one result.");
verify(repo,times(3)).findByUri("/js/yolo.js");
verify(repo,never()).findByFilename(anyString());
verify(repo,never()).findByHash(anyString());
verify(repo,never()).findByFileContent(anyString());
}

@Test
public void filenameMatchToHtml() throws IOException {
VulnerabilitiesRepositoryLoader.syncWithOnlineRepository = false;

//Init. mock
VulnerabilitiesRepository repo = mock(VulnerabilitiesRepository.class);
when(repo.findByUri("/js/yolo.js")).thenReturn(EMPTY_RESULT);
when(repo.findByFilename("yolo.js")).thenReturn(ONE_RESULT);

//Call the scanner logic
ScannerFacade scanner = new ScannerFacade(repo);
List<JsLibraryResult> results = scanner.scanHtml((
"<sCrIpT sRc=\"/js/yolo.js\" type=\"text/javascript\"></sCrIpT>" + // double quote
"<sCrIpT sRc='/js/yolo.js' type='text/javascript'></sCrIpT>" + // single quote
"<sCrIpT sRc=/js/yolo.js type=text/javascript></sCrIpT>" // no quote
).getBytes(),0);

//Assertions
assertEquals(results.size(),3,"Expect one result.");
verify(repo,times(3)).findByUri("/js/yolo.js");
verify(repo,times(3)).findByFilename("yolo.js");
verify(repo,never()).findByHash(anyString());
verify(repo,never()).findByFileContent(anyString());
}
}