Skip to content

Commit 2419df3

Browse files
docs: add package import rule guide (#752)
* add match package import rule * update match package import rule * address pr comments * address pr comments #2 * minor fix with playground * Update match-package-import.md --------- Co-authored-by: Herrington Darkholme <[email protected]>
1 parent 34f0fb2 commit 2419df3

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
## Match package import in Golang
2+
3+
* [Playground Link](/playground.html#eyJtb2RlIjoiQ29uZmlnIiwibGFuZyI6ImdvIiwicXVlcnkiOiIiLCJyZXdyaXRlIjoiIiwic3RyaWN0bmVzcyI6InNtYXJ0Iiwic2VsZWN0b3IiOiIiLCJjb25maWciOiJpZDogbWF0Y2gtcGFja2FnZS1pbXBvcnRcbmxhbmd1YWdlOiBnb1xucnVsZTpcbiAga2luZDogaW1wb3J0X3NwZWNcbiAgaGFzOlxuICAgIHJlZ2V4OiBnaXRodWIuY29tL2dvbGFuZy1qd3Qvand0Iiwic291cmNlIjoicGFja2FnZSBtYWluXG5cbmltcG9ydCAoXG5cdFwiZm10XCJcblx0XCJnaXRodWIuY29tL2dvbGFuZy1qd3Qvand0XCIgIC8vIFRoaXMgbWF0Y2hlcyB0aGUgQVNUIHJ1bGVcbilcblxuZnVuYyBtYWluKCkge1xuXHQvLyBDcmVhdGUgYSBuZXcgdG9rZW5cblx0dG9rZW4gOj0gand0Lk5ldyhqd3QuU2lnbmluZ01ldGhvZEhTMjU2KVxuXHRcblx0Ly8gQWRkIHNvbWUgY2xhaW1zXG5cdHRva2VuLkNsYWltcyA9IGp3dC5NYXBDbGFpbXN7XG5cdFx0XCJ1c2VyXCI6IFwiYWxpY2VcIixcblx0XHRcInJvbGVcIjogXCJhZG1pblwiLFxuXHR9XG5cdFxuXHQvLyBTaWduIHRoZSB0b2tlblxuXHR0b2tlblN0cmluZywgZXJyIDo9IHRva2VuLlNpZ25lZFN0cmluZyhbXWJ5dGUoXCJteS1zZWNyZXRcIikpXG5cdGlmIGVyciAhPSBuaWwge1xuXHRcdGZtdC5QcmludGYoXCJFcnJvciBzaWduaW5nIHRva2VuOiAldlxcblwiLCBlcnIpXG5cdFx0cmV0dXJuXG5cdH1cblx0XG5cdGZtdC5QcmludGYoXCJHZW5lcmF0ZWQgdG9rZW46ICVzXFxuXCIsIHRva2VuU3RyaW5nKVxufSJ9)
4+
5+
### Description
6+
7+
A generic rule template for detecting imports of specific packages in Go source code. This rule can be customized to match any package by modifying the regex pattern, making it useful for security auditing, dependency management, and compliance checking.
8+
9+
This rule identifies Go import statements based on the configured regex pattern, including:
10+
11+
Direct imports: `import "package/name"`
12+
Versioned imports: `import "package/name/v4"`
13+
Subpackage imports: `import "package/name/subpkg"`
14+
Grouped imports within `import () blocks`
15+
16+
### YAML
17+
18+
```yaml
19+
id: match-package-import
20+
language: go
21+
rule:
22+
kind: import_spec
23+
has:
24+
regex: PACKAGE_PATTERN_HERE
25+
```
26+
27+
### Example
28+
29+
JWT Library Detection
30+
31+
```go{5}
32+
package main
33+
34+
import (
35+
"fmt"
36+
"github.com/golang-jwt/jwt" // This matches the AST rule
37+
)
38+
39+
func main() {
40+
token := jwt.New(jwt.SigningMethodHS256) // Create a new token
41+
// Add some claims
42+
token.Claims = jwt.MapClaims{"user": "alice", "role": "admin"}
43+
tokenString, err := token.SignedString([]byte("my-secret")) // Sign the token
44+
if err != nil {
45+
fmt.Printf("Error signing token: %v\n", err)
46+
return
47+
}
48+
fmt.Printf("Generated token: %s\n", tokenString)
49+
}
50+
```
51+
52+
### Contributed by
53+
54+
[Sudesh Gutta](https://github.com/sudeshgutta)

0 commit comments

Comments
 (0)