Skip to content

Commit

Permalink
Merge pull request #233 from noir-cr/improve/add-param-in-spring-anal…
Browse files Browse the repository at this point in the history
…yzer

👔 Add identification logic of parameters in Spring #232
  • Loading branch information
ksg97031 authored Mar 5, 2024
2 parents f855d6a + 49c0bad commit b6d90c8
Show file tree
Hide file tree
Showing 20 changed files with 1,533 additions and 39 deletions.
1 change: 1 addition & 0 deletions spec/functional_test/fixtures/java_spring/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.test;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

@GetMapping("/greet")
public String greet(HttpServletRequest request) {
String name = request.getParameter("name");
if (name == null || name.isEmpty()) {
name = "World";
}

String header = request.getHeader("header");
if (header == null || header.isEmpty()) {
header = "!";
}
return "Hello, " + name + header;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.test;
import org.springframework.web.bind.annotation.*;
import a.b.c.bind.annotation.*;
import org.springframework.c.d.e.*;

@RestController
@RequestMapping("/items")
public class ItemController {

@GetMapping("/{id}")
public Item getItem(@PathVariable Long id) {
public Item getItem(@PathVariable Long id) throws ItemNotFoundException {
}

@PostMapping
Expand All @@ -23,4 +26,25 @@ public void deleteItem(@PathVariable Long id) {
@GetMapping("/json/{id}", produces = [MediaType.APPLICATION_JSON_VALUE])
public void getItemJson(){
}
}

class Item {
int id;
String name;

public void setId(int _id) {
id = _id;
}

public int getId() {
return id;
}

public void setName(String _name) {
name = _name;
}

public String getName() {
return name;
}
}
13 changes: 13 additions & 0 deletions spec/functional_test/fixtures/java_spring/src/RequestParam.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.test;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

@GetMapping("/greet2")
public String greet2(@RequestParam("myname") String a, @RequestParam("b") int b, String name) {
return "Hello, " + a + b"!";
}
}
1 change: 1 addition & 0 deletions spec/functional_test/fixtures/kotlin_spring/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.gradle
15 changes: 12 additions & 3 deletions spec/functional_test/testers/java_spring_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,21 @@ extected_endpoints = [
# ItemController.java
Endpoint.new("/items/{id}", "GET"),
Endpoint.new("/items/json/{id}", "GET"),
Endpoint.new("/items", "POST"),
Endpoint.new("/items/update/{id}", "PUT"),
Endpoint.new("/items", "POST", [Param.new("id", "", "form"), Param.new("name", "", "form")]),
Endpoint.new("/items/update/{id}", "PUT", [Param.new("id", "", "json"), Param.new("name", "", "json")]),
Endpoint.new("/items/delete/{id}", "DELETE"),
Endpoint.new("/greet", "GET", [
Param.new("name", "", "query"),
Param.new("header", "", "header"),
]),
Endpoint.new("/greet2", "GET", [
Param.new("myname", "", "query"),
Param.new("b", "", "query"),
Param.new("name", "", "query"),
]),
]

FunctionalTester.new("fixtures/java_spring/", {
:techs => 1,
:endpoints => 15,
:endpoints => 17,
}, extected_endpoints).test_all
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require "../../../src/analyzer/analyzers/analyzer_spring.cr"
require "../../../src/analyzer/analyzers/analyzer_kotlin_spring.cr"
require "../../../src/options"

describe "mapping_to_path" do
options = default_options()
instance = AnalyzerSpring.new(options)
instance = AnalyzerKotlinSpring.new(options)

it "mapping_to_path - GET" do
instance.mapping_to_path("@GetMapping(\"/abcd\")").should eq(["/abcd"])
Expand Down Expand Up @@ -72,7 +72,7 @@ end

describe "utils func" do
options = default_options()
instance = AnalyzerSpring.new(options)
instance = AnalyzerKotlinSpring.new(options)

it "is_bracket - true" do
instance.is_bracket("{abcd=1234}").should eq(true)
Expand Down
10 changes: 10 additions & 0 deletions spec/unit_test/detector/detect_kotlin_spring_spe_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "../../../src/detector/detectors/*"

describe "Detect Java Spring" do
options = default_options()
instance = DetectorKotlinSpring.new options

it "build.gradle.kts" do
instance.detect("build.gradle.kts", "'org.springframework.boot' version '2.6.2'").should eq(true)
end
end
8 changes: 2 additions & 6 deletions src/analyzer/analyzer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def initialize_analyzers(logger : NoirLogger)
analyzers["go_gin"] = ->analyzer_go_gin(Hash(Symbol, String))
analyzers["java_armeria"] = ->analyzer_armeria(Hash(Symbol, String))
analyzers["java_jsp"] = ->analyzer_jsp(Hash(Symbol, String))
analyzers["java_spring"] = ->analyzer_spring(Hash(Symbol, String))
analyzers["java_spring"] = ->analyzer_java_spring(Hash(Symbol, String))
analyzers["js_express"] = ->analyzer_express(Hash(Symbol, String))
analyzers["kotlin_spring"] = ->analyzer_spring(Hash(Symbol, String))
analyzers["kotlin_spring"] = ->analyzer_kotlin_spring(Hash(Symbol, String))
analyzers["oas2"] = ->analyzer_oas2(Hash(Symbol, String))
analyzers["oas3"] = ->analyzer_oas3(Hash(Symbol, String))
analyzers["php_pure"] = ->analyzer_php_pure(Hash(Symbol, String))
Expand Down Expand Up @@ -51,10 +51,6 @@ def analysis_endpoints(options : Hash(Symbol, String), techs, logger : NoirLogge
logger.system "Analysis Started"
logger.info_sub "Code Analyzer: #{techs.size} in use"

if (techs.includes? "java_spring") && (techs.includes? "kotlin_spring")
techs.delete("kotlin_spring")
end

techs.each do |tech|
if analyzer.has_key?(tech)
if NoirTechs.similar_to_tech(options[:exclude_techs]).includes?(tech)
Expand Down
Loading

0 comments on commit b6d90c8

Please sign in to comment.