Skip to content

Commit

Permalink
Merge pull request #134 from hahwul/hahwul-dev
Browse files Browse the repository at this point in the history
Fixed bug in js express analyzer (#126)
  • Loading branch information
hahwul authored Oct 6, 2023
2 parents 15c4869 + 28bf445 commit 67af9c7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 51 deletions.
10 changes: 10 additions & 0 deletions spec/functional_test/fixtures/js_express/app.js
Original file line number Diff line number Diff line change
@@ -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');
});
}
11 changes: 11 additions & 0 deletions spec/functional_test/testers/js_express_spec.cr
Original file line number Diff line number Diff line change
@@ -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
102 changes: 51 additions & 51 deletions src/analyzer/analyzers/analyzer_express.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 67af9c7

Please sign in to comment.