From 28bf445cbcd46f4cd9df5ea0bb532ebb26da9bb2 Mon Sep 17 00:00:00 2001 From: hahwul Date: Sat, 7 Oct 2023 00:56:54 +0900 Subject: [PATCH] Fixed bug in js express analyzer (#126) --- .../fixtures/js_express/app.js | 10 ++ .../testers/js_express_spec.cr | 11 ++ src/analyzer/analyzers/analyzer_express.cr | 102 +++++++++--------- 3 files changed, 72 insertions(+), 51 deletions(-) create mode 100644 spec/functional_test/fixtures/js_express/app.js create mode 100644 spec/functional_test/testers/js_express_spec.cr diff --git a/spec/functional_test/fixtures/js_express/app.js b/spec/functional_test/fixtures/js_express/app.js new file mode 100644 index 00000000..d3cc2cd4 --- /dev/null +++ b/spec/functional_test/fixtures/js_express/app.js @@ -0,0 +1,10 @@ +require('express') + +module.exports = function(app) { + app.get('/',function(req,res){ + res.render('index'); + }); + app.post('/upload',function(req,res){ + res.render('index'); + }); +} \ No newline at end of file diff --git a/spec/functional_test/testers/js_express_spec.cr b/spec/functional_test/testers/js_express_spec.cr new file mode 100644 index 00000000..e9bcc1ab --- /dev/null +++ b/spec/functional_test/testers/js_express_spec.cr @@ -0,0 +1,11 @@ +require "../func_spec.cr" + +extected_endpoints = [ + Endpoint.new("/", "GET"), + Endpoint.new("/upload", "POST"), +] + +FunctionalTester.new("fixtures/js_express/", { + :techs => 1, + :endpoints => 2, +}, extected_endpoints).test_all diff --git a/src/analyzer/analyzers/analyzer_express.cr b/src/analyzer/analyzers/analyzer_express.cr index 9549a1d4..d6f98228 100644 --- a/src/analyzer/analyzers/analyzer_express.cr +++ b/src/analyzer/analyzers/analyzer_express.cr @@ -2,70 +2,70 @@ require "../../models/analyzer" class AnalyzerExpress < Analyzer def analyze - # TODO - end -end - -def analyzer_express(options : Hash(Symbol, String)) - result = [] of Endpoint - base_path = options[:base] - url = options[:url] - _ = url - - # Source Analysis - begin - Dir.glob("#{base_path}/**/*") do |path| - next if File.directory?(path) - if File.exists?(path) - File.open(path, "r", encoding: "utf-8", invalid: :skip) do |file| - file.each_line do |line| - if line.includes? ".get('/" - api_path = express_get_endpoint(line) - if api_path != "" - result << Endpoint.new(api_path, "GET") + # Source Analysis + begin + Dir.glob("#{base_path}/**/*") do |path| + next if File.directory?(path) + if File.exists?(path) + File.open(path, "r", encoding: "utf-8", invalid: :skip) do |file| + file.each_line do |line| + if line.includes? ".get('/" + api_path = express_get_endpoint(line) + if api_path != "" + endpoint = (url + api_path).gsub(/\/\//, "/") + result << Endpoint.new(endpoint, "GET") + end end - end - if line.includes? ".post('/" - api_path = express_get_endpoint(line) - if api_path != "" - result << Endpoint.new(api_path, "POST") + if line.includes? ".post('/" + api_path = express_get_endpoint(line) + if api_path != "" + endpoint = (url + api_path).gsub(/\/\//, "/") + result << Endpoint.new(endpoint, "POST") + end end - end - if line.includes? ".put('/" - api_path = express_get_endpoint(line) - if api_path != "" - result << Endpoint.new(api_path, "PUT") + if line.includes? ".put('/" + api_path = express_get_endpoint(line) + if api_path != "" + result << Endpoint.new(url + api_path, "PUT") + end end - end - if line.includes? ".delete('/" - api_path = express_get_endpoint(line) - if api_path != "" - result << Endpoint.new(api_path, "DELETE") + if line.includes? ".delete('/" + api_path = express_get_endpoint(line) + if api_path != "" + endpoint = (url + api_path).gsub(/\/\//, "/") + result << Endpoint.new(endpoint, "DELETE") + end end - end - if line.includes? ".patch('/" - api_path = express_get_endpoint(line) - if api_path != "" - result << Endpoint.new(api_path, "PATCH") + if line.includes? ".patch('/" + api_path = express_get_endpoint(line) + if api_path != "" + endpoint = (url + api_path).gsub(/\/\//, "/") + result << Endpoint.new(endpoint, "PATCH") + end end end end end end + rescue e + # TODO end - rescue e - # TODO + + result end - result -end + def express_get_endpoint(line : String) + api_path = "" + splited = line.split("(") + if splited.size > 0 + api_path = splited[1].split(",")[0].gsub(/['"]/, "") + end -def express_get_endpoint(line : String) - api_path = "" - splited = line.split("(") - if splited.size > 0 - api_path = splited[1].split(",")[0].gsub(/['"]/, "") + api_path end +end - api_path +def analyzer_express(options : Hash(Symbol, String)) + instance = AnalyzerExpress.new(options) + instance.analyze end