-
Notifications
You must be signed in to change notification settings - Fork 3
/
parser.go
184 lines (147 loc) · 3.02 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package plugin_log4shell
import (
"fmt"
)
// Token types.
const (
Start = "START"
End = "END"
Content = "CONTENT"
Separator = "SEP"
)
// Node types.
const (
Expression = "EXP"
Text = "TXT"
Root = "ROOT"
)
// Nodes a set of nodes.
type Nodes []*Node
func (e Nodes) String() string {
var data string
for _, v := range e {
data += v.String()
}
return data
}
// Node a node.
type Node struct {
Type string
Text string
Key Nodes
Value Nodes
}
func (n Node) String() string {
switch n.Type {
case Expression, Root:
return n.Value.String()
case Text:
return n.Text
default:
panic(fmt.Sprintf("not supported node type: %s", n.Type))
}
}
// Token a syntax token.
type Token struct {
Type string `json:"type,omitempty"`
Pos int `json:"pos,omitempty"`
Value string `json:"value,omitempty"`
}
func (t Token) String() string {
return t.Value
}
// Parse naively parses Log4j expression.
// https://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution
func Parse(value string) *Node {
root := &Node{Type: Root}
buildTree(root, tokenize(value))
return root
}
func tokenize(value string) []*Token {
var tokens []*Token
var previous *Token
var open int
length := len(value)
for i := 0; i < length; i++ {
v := value[i]
t := &Token{Pos: i}
switch {
case v == '$' && length > i+1 && value[i+1] == '{':
t.Type = Start
t.Value = "${"
i++
open++
case v == '}' && open > 0:
t.Type = End
t.Value = "}"
open--
case v == ':' && open > 0:
t.Type = Separator
t.Value = ":"
if length > i+1 && value[i+1] == '-' {
t.Value = ":-"
i++
}
default:
if previous != nil && previous.Type == Content {
previous.Value += string(v)
continue
}
t.Type = Content
t.Value = string(v)
}
previous = t
tokens = append(tokens, t)
}
return tokens
}
func buildTree(root *Node, tokens []*Token) int {
var sep bool
for i := 0; i < len(tokens); i++ {
token := tokens[i]
switch token.Type {
case Start:
exp := &Node{Type: Expression}
switch root.Type {
case Root:
root.Value = append(root.Value, exp)
case Expression:
if sep {
root.Key = append(root.Value, root.Key...)
root.Value = []*Node{exp}
} else {
root.Key = append(root.Key, exp)
}
default:
panic(fmt.Sprintf("invalid start node: %T", root))
}
j := buildTree(exp, tokens[i+1:])
if j < 0 {
return i
}
i += j
case End:
return i + 1
case Content:
switch root.Type {
case Root:
root.Value = append(root.Value, &Node{Type: Text, Text: token.Value})
case Expression:
if sep {
root.Key = append(root.Value, root.Key...)
root.Value = []*Node{{Type: Text, Text: token.Value}}
} else {
root.Key = append(root.Key, &Node{Type: Text, Text: token.Value})
}
default:
panic(fmt.Sprintf("invalid content node: %T", root))
}
case Separator:
sep = true
continue
default:
panic(fmt.Sprintf("invalid token type: %s", token.Type))
}
}
return -1
}