Skip to content
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
2 changes: 1 addition & 1 deletion pkg/utils/jsluice.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !(386 || windows)
//go:build jsluice && !(386 || windows)

package utils

Expand Down
86 changes: 86 additions & 0 deletions pkg/utils/jsluice_stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//go:build !jsluice || 386 || windows

package utils

import (
"regexp"
"strings"
)

var (
// URL patterns for extracting endpoints from JavaScript
urlPattern = regexp.MustCompile(`(?i)(?:"|'|` + "`" + `)((?:https?:)?//[^\s"'` + "`" + `<>]+|/[^\s"'` + "`" + `<>]+)(?:"|'|` + "`" + `)`)

// API endpoint patterns
apiPattern = regexp.MustCompile(`(?i)(?:api|endpoint|url|path|route)[\s]*[:=][\s]*(?:"|'|` + "`" + `)([^"'` + "`" + `]+)(?:"|'|` + "`" + `)`)
)

type JSLuiceEndpoint struct {
Endpoint string
Type string
}

// ExtractJsluiceEndpoints extracts endpoints from JavaScript using pure Go regex.
// This is a fallback implementation when jsluice (which requires CGO) is not available.
//
// Note: This implementation uses regex patterns and may not be as accurate as jsluice,
// but it eliminates the CGO dependency for cross-platform compilation.
func ExtractJsluiceEndpoints(data string) []JSLuiceEndpoint {
var endpoints []JSLuiceEndpoint
seen := make(map[string]bool)

// Extract URLs using URL pattern
urlMatches := urlPattern.FindAllStringSubmatch(data, -1)
for _, match := range urlMatches {
if len(match) > 1 {
url := match[1]
if !seen[url] && isValidEndpoint(url) {
seen[url] = true
endpoints = append(endpoints, JSLuiceEndpoint{
Endpoint: url,
Type: "url",
})
}
}
}

// Extract API endpoints
apiMatches := apiPattern.FindAllStringSubmatch(data, -1)
for _, match := range apiMatches {
if len(match) > 1 {
endpoint := match[1]
if !seen[endpoint] && isValidEndpoint(endpoint) {
seen[endpoint] = true
endpoints = append(endpoints, JSLuiceEndpoint{
Endpoint: endpoint,
Type: "api",
})
}
}
}

return endpoints
}
Comment on lines +28 to +63
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Type classification differs from CGO version semantics.

The stub returns only "url" or "api", while the jsluice library returns specific types like "linkHref", "fetch", "xhr", etc. Downstream code constructs attributes via fmt.Sprintf("jsluice-%s", item.Type) (see parser_generic.go), so endpoints will be labeled differently:

  • CGO: jsluice-linkHref, jsluice-fetch, etc.
  • Stub: jsluice-url, jsluice-api

This may affect filtering, output formatting, or any logic that depends on specific type values. If this is intentional fallback behavior, consider documenting it in the function's comments.

📝 Suggested documentation addition
 // ExtractJsluiceEndpoints extracts endpoints from JavaScript using pure Go regex.
 // This is a fallback implementation when jsluice (which requires CGO) is not available.
 //
 // Note: This implementation uses regex patterns and may not be as accurate as jsluice,
 // but it eliminates the CGO dependency for cross-platform compilation.
+//
+// Type classification: This stub returns generic types ("url" or "api") rather than
+// the specific types returned by jsluice (e.g., "linkHref", "fetch", "xhr").
 func ExtractJsluiceEndpoints(data string) []JSLuiceEndpoint {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/utils/jsluice_stub.go` around lines 28 - 63, The ExtractJsluiceEndpoints
stub currently returns generic types "url" and "api" which differ from the real
jsluice CGO semantics (e.g., "linkHref", "fetch", "xhr") and breaks downstream
formatting that builds attributes with fmt.Sprintf("jsluice-%s", item.Type);
update ExtractJsluiceEndpoints (and JSLuiceEndpoint.Type values it creates) to
map URL and API regex matches to the concrete jsluice type strings used by the
real library (e.g., map link href matches to "linkHref", fetch calls to "fetch",
XHR to "xhr", etc.), or if you intentionally want a fallback behavior, add a
clear comment in the function declaring that it returns coarse-grained
"url"/"api" types and list the expected CGO-specific type names so callers know
the difference; ensure references in parser_generic.go that format "jsluice-%s"
will receive the expected type strings.


// isValidEndpoint checks if the extracted string is a valid endpoint
func isValidEndpoint(s string) bool {
// Filter out common false positives
if len(s) < 2 {
return false
}

// Normalize to lowercase for case-insensitive comparison
lower := strings.ToLower(s)

// Skip data URIs and javascript URIs
if strings.HasPrefix(lower, "data:") || strings.HasPrefix(lower, "javascript:") {
return false
}

// Skip very long strings (likely not URLs)
if len(s) > 2048 {
return false
}

return true
}
112 changes: 112 additions & 0 deletions pkg/utils/jsluice_stub_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//go:build !jsluice || 386 || windows

package utils

import (
"testing"
)

func TestExtractJsluiceEndpoints_Stub(t *testing.T) {
tests := []struct {
name string
input string
wantURLs []string
}{
{
name: "simple URL in quotes",
input: `var url = "https://example.com/api/users";`,
wantURLs: []string{
"https://example.com/api/users",
},
},
{
name: "relative path",
input: `fetch("/api/data")`,
wantURLs: []string{
"/api/data",
},
},
{
name: "multiple URLs",
input: `const api1 = "https://api.example.com/v1"; const api2 = "/api/v2/users";`,
wantURLs: []string{
"https://api.example.com/v1",
"/api/v2/users",
},
},
{
name: "no URLs",
input: `var x = 123; console.log("hello");`,
wantURLs: []string{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
endpoints := ExtractJsluiceEndpoints(tt.input)

if len(tt.wantURLs) == 0 && len(endpoints) == 0 {
return
}

if len(endpoints) != len(tt.wantURLs) {
t.Errorf("ExtractJsluiceEndpoints() got %d endpoints, want %d", len(endpoints), len(tt.wantURLs))
return
}

// Check if all expected URLs are found
found := make(map[string]bool)
for _, ep := range endpoints {
found[ep.Endpoint] = true
}

for _, wantURL := range tt.wantURLs {
if !found[wantURL] {
t.Errorf("ExtractJsluiceEndpoints() missing expected URL: %s", wantURL)
}
}
})
}
}

func TestIsValidEndpoint(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
{
name: "valid URL",
input: "https://example.com/api",
want: true,
},
{
name: "valid path",
input: "/api/users",
want: true,
},
{
name: "too short",
input: "/",
want: false,
},
{
name: "data URI",
input: "data:image/png;base64,iVBORw0KGgo...",
want: false,
},
{
name: "javascript URI",
input: "javascript:void(0)",
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isValidEndpoint(tt.input); got != tt.want {
t.Errorf("isValidEndpoint() = %v, want %v", got, tt.want)
}
})
}
}