Skip to content

Commit

Permalink
Merge pull request #486 from owasp-noir/issue-481
Browse files Browse the repository at this point in the history
Enhanced detection logic for Express
  • Loading branch information
hahwul authored Jan 15, 2025
2 parents 76a6b37 + c002d4f commit 71ed4aa
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
51 changes: 50 additions & 1 deletion spec/unit_test/detector/javascript/express_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,56 @@ describe "Detect JS Express" do
it "require_single_quot" do
instance.detect("index.js", "require('express')").should eq(true)
end

it "require_double_quot" do
instance.detect("index.js", "require(\"express\")").should eq(true)
end
end

it "import_single_quot" do
instance.detect("index.js", "import express from 'express'").should eq(true)
end

it "import_double_quot" do
instance.detect("index.js", "import express from \"express\"").should eq(true)
end

it "const_require_single_quot" do
instance.detect("index.js", "const express = require('express')").should eq(true)
end

it "const_require_double_quot" do
instance.detect("index.js", "const express = require(\"express\")").should eq(true)
end

it "let_require_single_quot" do
instance.detect("index.js", "let express = require('express')").should eq(true)
end

it "let_require_double_quot" do
instance.detect("index.js", "let express = require(\"express\")").should eq(true)
end

it "var_require_single_quot" do
instance.detect("index.js", "var express = require('express')").should eq(true)
end

it "var_require_double_quot" do
instance.detect("index.js", "var express = require(\"express\")").should eq(true)
end

it "import_router_single_quot" do
instance.detect("index.js", "import { Router } from 'express'").should eq(true)
end

it "import_router_double_quot" do
instance.detect("index.js", "import { Router } from \"express\"").should eq(true)
end

it "app_use_express_json" do
instance.detect("index.js", "app.use(express.json())").should eq(true)
end

it "app_use_express_urlencoded" do
instance.detect("index.js", "app.use(express.urlencoded({ extended: true }))").should eq(true)
end
end
11 changes: 7 additions & 4 deletions src/detector/detectors/javascript/express.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ require "../../../models/detector"
module Detector::Javascript
class Express < Detector
def detect(filename : String, file_contents : String) : Bool
if (filename.includes? ".js") && (file_contents.includes? "require('express')")
true
elsif (filename.includes? ".js") && (file_contents.includes? "require(\"express\")")
if (filename.includes?(".js") || filename.includes?(".mjs") || filename.includes?(".ts")) &&
(file_contents.match(/require\(['"]express['"]\)/) ||
file_contents.match(/import express from ['"]express['"]/) ||
file_contents.match(/import { Router } from ['"]express['"]/) ||
file_contents.match(/app\.use\(express\.json\(\)\)/) ||
file_contents.match(/app\.use\(express\.urlencoded\(\{ extended: true \}\)\)/))
true
else
false
Expand All @@ -16,4 +19,4 @@ module Detector::Javascript
@name = "js_express"
end
end
end
end
2 changes: 0 additions & 2 deletions src/detector/detectors/javascript/restify.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ module Detector::Javascript
true
elsif (filename.includes? ".js") && (file_contents.includes? "require(\"restify\")")
true
elsif (filename.includes? ".ts") && (file_contents.includes? "server")
true
elsif (filename.includes? ".ts") && (file_contents.includes? "require(\"restify\")")
true
else
Expand Down

0 comments on commit 71ed4aa

Please sign in to comment.