Skip to content

Commit ac7b07e

Browse files
committed
Adds comments to all exported members
1 parent 3150d47 commit ac7b07e

File tree

6 files changed

+109
-5
lines changed

6 files changed

+109
-5
lines changed

cmd/jsluice/testdata/escapes.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let str = 'Hello,\x20World!'

cmd/jsluice/testdata/object.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const config = {
2+
stage: false,
3+
server: "example.com",
4+
ttl: 3600,
5+
dns: ["1.1.1.1", "8.8.8.8"],
6+
paths: {
7+
"home": "/",
8+
"blog": "/blog"
9+
}
10+
}

secret-matchers.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import (
44
"strings"
55
)
66

7-
// A Secret represents any bit of secret or otherwise interesting
8-
// data found within a JavaScript file. E.g. an AWS access key and
9-
// secret.
7+
// A Secret represents any secret or otherwise interesting data
8+
// found within a JavaScript file. E.g. an AWS access key.
109
type Secret struct {
1110
Kind string `json:"kind"`
1211
Data any `json:"data"`
@@ -15,6 +14,7 @@ type Secret struct {
1514
Context map[string]string `json:"context"`
1615
}
1716

17+
// Severity indicates how serious a finding is
1818
type Severity string
1919

2020
const (

strings.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,7 @@ func (s *stringLexer) String() string {
180180
}
181181

182182
// DecodeString accepts a raw string as it might be found in some
183-
// JavaScript source code (without surrounding quotes), and converts
184-
// any escape sequences. E.g.
183+
// JavaScript source code, and converts any escape sequences. E.g:
185184
// foo\x3dbar -> foo=bar // Hex escapes
186185
// foo\u003Dbar -> foo=bar // Unicode escapes
187186
// foo\u{003D}bar -> foo=bar // Braced unicode escapes

tree.go

+73
Original file line numberDiff line numberDiff line change
@@ -9,59 +9,89 @@ import (
99
"github.com/smacker/go-tree-sitter/javascript"
1010
)
1111

12+
// ExpressionPlaceholder is the string used to replace any
13+
// expressions when string concatenations are collapsed. E.g:
14+
// "prefix" + someVar + "suffix"
15+
// Would become:
16+
// prefixEXPRsuffix
1217
var ExpressionPlaceholder = "EXPR"
1318

19+
// Node is a wrapper around a tree-sitter node. It serves as
20+
// an attachment point for convenience methods, and also to
21+
// store the raw JavaScript source that is a required argument
22+
// for many tree-sitter functions.
1423
type Node struct {
1524
node *sitter.Node
1625
source []byte
1726
}
1827

28+
// NewNode creates a new Node for the provided tree-sitter
29+
// node and a byte-slice containing the JavaScript source.
30+
// The source provided should be the complete source code
31+
// and not just the source for the node in question.
1932
func NewNode(n *sitter.Node, source []byte) *Node {
2033
return &Node{
2134
node: n,
2235
source: source,
2336
}
2437
}
2538

39+
// AsObject returns a Node as jsluice's internal object type,
40+
// to allow the fetching of keys etc
2641
func (n *Node) AsObject() object {
2742
return newObject(n, n.source)
2843
}
2944

45+
// Content returns the source code for a particular node.
3046
func (n *Node) Content() string {
3147
if n.node == nil {
3248
return ""
3349
}
3450
return n.node.Content(n.source)
3551
}
3652

53+
// Type returns the tree-sitter type string for a Node.
54+
// E.g. string, object, call_expression. If the node is
55+
// nil then an empty string is returned.
3756
func (n *Node) Type() string {
3857
if n.node == nil {
3958
return ""
4059
}
4160
return n.node.Type()
4261
}
4362

63+
// Fetches a child Node from a named field. For example,
64+
// the 'pair' node has two fields: key, and value.
4465
func (n *Node) ChildByFieldName(name string) *Node {
4566
if !n.IsValid() {
4667
return nil
4768
}
4869
return NewNode(n.node.ChildByFieldName(name), n.source)
4970
}
5071

72+
// NamedChild returns the 'named' child Node at the provided
73+
// index. Tree-sitter considers a child to be named if it has
74+
// a name in the syntax tree. Things like brackets are not named,
75+
// but things like variables and function calls are named.
76+
// See https://tree-sitter.github.io/tree-sitter/using-parsers#named-vs-anonymous-nodes
77+
// for more details.
5178
func (n *Node) NamedChild(index int) *Node {
5279
if !n.IsValid() {
5380
return nil
5481
}
5582
return NewNode(n.node.NamedChild(index), n.source)
5683
}
5784

85+
// NamedChildCount returns the number of named children a Node has.
5886
func (n *Node) NamedChildCount() int {
5987
if !n.IsValid() {
6088
return 0
6189
}
6290
return int(n.node.NamedChildCount())
6391
}
6492

93+
// NamedChildren returns a slice of *Node containg all
94+
// named children for a node.
6595
func (n *Node) NamedChildren() []*Node {
6696
count := n.NamedChildCount()
6797
out := make([]*Node, 0, count)
@@ -83,6 +113,7 @@ func (n *Node) NamedChildren() []*Node {
83113
//
84114
// ./upload.php?profile=EXPR&show=EXPR
85115
//
116+
// The value of ExpressionPlaceholder is used as a placeholder, defaulting to 'EXPR'
86117
func (n *Node) CollapsedString() string {
87118
if !n.IsValid() {
88119
return ""
@@ -101,18 +132,39 @@ func (n *Node) CollapsedString() string {
101132
}
102133
}
103134

135+
// IsValid returns true if the *Node and the underlying
136+
// tree-sitter node are both not nil.
104137
func (n *Node) IsValid() bool {
105138
return n != nil && n.node != nil
106139
}
107140

141+
// RawString returns the raw JavaScript representation
142+
// of a string (i.e. escape sequences are left undecoded)
143+
// but with the surrounding quotes removed.
108144
func (n *Node) RawString() string {
109145
return dequote(n.Content())
110146
}
111147

148+
// DecodedString returns a fully decoded version of a
149+
// JavaScript string. It is just a convenience wrapper
150+
// around the DecodeString function.
112151
func (n *Node) DecodedString() string {
113152
return DecodeString(n.Content())
114153
}
115154

155+
// AsGoType returns a representation of a Node as a native
156+
// Go type, defaulting to a string containing the JavaScript
157+
// source for the Node. Return types are:
158+
//
159+
// string => string
160+
// number => int, float64
161+
// object => map[string]any
162+
// array => []any
163+
// false => false
164+
// true => true
165+
// null => nil
166+
// other => string
167+
//
116168
func (n *Node) AsGoType() any {
117169
if n == nil {
118170
return nil
@@ -138,6 +190,7 @@ func (n *Node) AsGoType() any {
138190
}
139191
}
140192

193+
// AsMap returns a representation of the Node as a map[string]any
141194
func (n *Node) AsMap() map[string]any {
142195
if n.Type() != "object" {
143196
return map[string]any{}
@@ -160,6 +213,7 @@ func (n *Node) AsMap() map[string]any {
160213
return out
161214
}
162215

216+
// AsArray returns a representation of the Node as a []any
163217
func (n *Node) AsArray() []any {
164218
if n.Type() != "array" {
165219
return []any{}
@@ -176,6 +230,9 @@ func (n *Node) AsArray() []any {
176230
return out
177231
}
178232

233+
// AsNumber returns a representation of the Node as an int or float64.
234+
//
235+
// Note: hex, octal etc number formats are currently unsupported
179236
func (n *Node) AsNumber() any {
180237
if n.Type() != "number" {
181238
return 0
@@ -201,13 +258,20 @@ func (n *Node) AsNumber() any {
201258
return i
202259
}
203260

261+
// Parent returns the Parent Node for a Node
204262
func (n *Node) Parent() *Node {
205263
if !n.IsValid() {
206264
return nil
207265
}
208266
return NewNode(n.node.Parent(), n.source)
209267
}
210268

269+
// Query executes a tree-sitter query on a specific Node.
270+
// Nodes matching the query are passed one at a time to the
271+
// provided callback function.
272+
//
273+
// See https://tree-sitter.github.io/tree-sitter/using-parsers#pattern-matching-with-queries
274+
// for query syntax documentation.
211275
func (n *Node) Query(query string, fn func(*Node)) {
212276
if !n.IsValid() {
213277
return
@@ -237,6 +301,9 @@ func (n *Node) Query(query string, fn func(*Node)) {
237301
}
238302
}
239303

304+
// IsStringy returns true if a Node is a string
305+
// or is an expression starting with a string
306+
// (e.g. a string concatenation expression).
240307
func (n *Node) IsStringy() bool {
241308
if n.Type() == "string" {
242309
return true
@@ -255,17 +322,22 @@ func (n *Node) IsStringy() bool {
255322
}
256323
}
257324

325+
// dequote removes surround quotes from the provided string
258326
func dequote(in string) string {
259327
return strings.Trim(in, "'\"`")
260328
}
261329

330+
// content returns the source for the provided tree-sitter
331+
// node, checking if the node is nil first.
262332
func content(n *sitter.Node, source []byte) string {
263333
if n == nil {
264334
return ""
265335
}
266336
return n.Content(source)
267337
}
268338

339+
// PrintTree returns a string representation of the syntax tree
340+
// for the provided JavaScript source
269341
func PrintTree(source []byte) string {
270342
parser := sitter.NewParser()
271343
parser.SetLanguage(javascript.GetLanguage())
@@ -276,6 +348,7 @@ func PrintTree(source []byte) string {
276348
return getTree(root, source)
277349
}
278350

351+
// getTree does the actual heavy lifting and recursion for PrintTree
279352
// TODO: provide a way to print the tree as a JSON object?
280353
func getTree(n *sitter.Node, source []byte) string {
281354

user-patterns.go

+21
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import (
77
"regexp"
88
)
99

10+
// A UserPattern represents a pattern that was provided by a
11+
// when using the command-line tool. When using the package
12+
// directly, a SecretMatcher can be created directly instead
13+
// of creating a UserPattern
1014
type UserPattern struct {
1115
Name string `json:"name"`
1216
Key string `json:"key"`
@@ -19,6 +23,8 @@ type UserPattern struct {
1923
reValue *regexp.Regexp
2024
}
2125

26+
// ParseRegex parses all of the user-provided regular expressions
27+
// for a pattern into Go *regexp.Regexp types
2228
func (u *UserPattern) ParseRegex() error {
2329
if u.Value != "" {
2430
re, err := regexp.Compile(u.Value)
@@ -53,20 +59,26 @@ func (u *UserPattern) ParseRegex() error {
5359
return nil
5460
}
5561

62+
// MatchValue returns true if a pattern's value regex matches
63+
// the supplied value, or if there is no value regex.
5664
func (u *UserPattern) MatchValue(in string) bool {
5765
if u.reValue == nil {
5866
return true
5967
}
6068
return u.reValue.MatchString(in)
6169
}
6270

71+
// MatchKey returns true if a pattern's key regex matches
72+
// the supplied value, or if there is no key regex
6373
func (u *UserPattern) MatchKey(in string) bool {
6474
if u.reKey == nil {
6575
return true
6676
}
6777
return u.reKey.MatchString(in)
6878
}
6979

80+
// SecretMatcher returns a SecretMatcher based on the UserPattern,
81+
// for use with (*Analyzer).AddSecretMatcher()
7082
func (u *UserPattern) SecretMatcher() SecretMatcher {
7183
if len(u.Object) > 0 {
7284
return u.objectMatcher()
@@ -79,6 +91,7 @@ func (u *UserPattern) SecretMatcher() SecretMatcher {
7991
return u.stringMatcher()
8092
}
8193

94+
// objectMatcher returns a SecretMatcher for matching against objects
8295
func (u *UserPattern) objectMatcher() SecretMatcher {
8396
return SecretMatcher{"(object) @matches", func(n *Node) *Secret {
8497
pairs := n.NamedChildren()
@@ -110,6 +123,7 @@ func (u *UserPattern) objectMatcher() SecretMatcher {
110123
}}
111124
}
112125

126+
// pairMatcher returns a SecretMatcher for matching against key/value pairs
113127
func (u *UserPattern) pairMatcher() SecretMatcher {
114128
return SecretMatcher{"(pair) @matches", func(n *Node) *Secret {
115129

@@ -148,6 +162,7 @@ func (u *UserPattern) pairMatcher() SecretMatcher {
148162
}}
149163
}
150164

165+
// stringMatcher returns a SecretMatcher for matching against string literals
151166
func (u *UserPattern) stringMatcher() SecretMatcher {
152167
return SecretMatcher{"(string) @matches", func(n *Node) *Secret {
153168
in := n.RawString()
@@ -177,8 +192,11 @@ func (u *UserPattern) stringMatcher() SecretMatcher {
177192
}}
178193
}
179194

195+
// UserPatterns is an alias for a slice of *UserPattern
180196
type UserPatterns []*UserPattern
181197

198+
// SecretMatchers returns a slice of SecretMatcher for use with
199+
// (*Analyzer).AddSecretMatchers()
182200
func (u UserPatterns) SecretMatchers() []SecretMatcher {
183201
out := make([]SecretMatcher, 0)
184202

@@ -188,6 +206,9 @@ func (u UserPatterns) SecretMatchers() []SecretMatcher {
188206
return out
189207
}
190208

209+
// ParseUserPatterns accepts an io.Reader pointing to a JSON user-pattern
210+
// definition file, and returns a list of UserPatterns, and any error that
211+
// occurred.
191212
func ParseUserPatterns(r io.Reader) (UserPatterns, error) {
192213
out := make(UserPatterns, 0)
193214

0 commit comments

Comments
 (0)