-
Notifications
You must be signed in to change notification settings - Fork 979
Fix cgo dependency 1367 #1564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Wesley7711
wants to merge
5
commits into
projectdiscovery:dev
Choose a base branch
from
Wesley7711:fix-cgo-dependency-1367
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+199
−1
Open
Fix cgo dependency 1367 #1564
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
|
|
||
| // 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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| }) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 viafmt.Sprintf("jsluice-%s", item.Type)(seeparser_generic.go), so endpoints will be labeled differently:jsluice-linkHref,jsluice-fetch, etc.jsluice-url,jsluice-apiThis 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
🤖 Prompt for AI Agents