@ant-yasa/uast-parser-java-js 0.1.15-beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/.babelrc +12 -0
  2. package/.eslintignore +1 -0
  3. package/.eslintrc.js +38 -0
  4. package/.mocharc-integration.js +1 -0
  5. package/.mocharc.js +4 -0
  6. package/.prettierignore +1 -0
  7. package/.prettierrc +4 -0
  8. package/LEGAL.md +7 -0
  9. package/README.md +35 -0
  10. package/antlr/Java.g4 +1008 -0
  11. package/antlr/JavaLexer.g4 +241 -0
  12. package/antlr/JavaLexer_origin.g4 +241 -0
  13. package/antlr/JavaParser.g4 +765 -0
  14. package/antlr/JavaParser_origin.g4 +766 -0
  15. package/dist/package.json +73 -0
  16. package/dist/src/frontend/java/ASTBuilder.d.ts +171 -0
  17. package/dist/src/frontend/java/ASTBuilder.js +2369 -0
  18. package/dist/src/frontend/java/ASTBuilder.js.map +1 -0
  19. package/dist/src/frontend/java/ErrorListener.d.ts +9 -0
  20. package/dist/src/frontend/java/ErrorListener.js +24 -0
  21. package/dist/src/frontend/java/ErrorListener.js.map +1 -0
  22. package/dist/src/frontend/java/antlr/JavaLexer.d.ts +154 -0
  23. package/dist/src/frontend/java/antlr/JavaLexer.js +812 -0
  24. package/dist/src/frontend/java/antlr/JavaLexer.js.map +1 -0
  25. package/dist/src/frontend/java/antlr/JavaListener.d.ts +1393 -0
  26. package/dist/src/frontend/java/antlr/JavaListener.js +4 -0
  27. package/dist/src/frontend/java/antlr/JavaListener.js.map +1 -0
  28. package/dist/src/frontend/java/antlr/JavaParser.d.ts +2065 -0
  29. package/dist/src/frontend/java/antlr/JavaParser.js +15279 -0
  30. package/dist/src/frontend/java/antlr/JavaParser.js.map +1 -0
  31. package/dist/src/frontend/java/antlr/JavaVisitor.d.ts +892 -0
  32. package/dist/src/frontend/java/antlr/JavaVisitor.js +4 -0
  33. package/dist/src/frontend/java/antlr/JavaVisitor.js.map +1 -0
  34. package/dist/src/frontend/java/parser.d.ts +20 -0
  35. package/dist/src/frontend/java/parser.js +93 -0
  36. package/dist/src/frontend/java/parser.js.map +1 -0
  37. package/dist/src/frontend/java/testBuilder.d.ts +146 -0
  38. package/dist/src/frontend/java/testBuilder.js +464 -0
  39. package/dist/src/frontend/java/testBuilder.js.map +1 -0
  40. package/dist/src/frontend/java/types.d.ts +29 -0
  41. package/dist/src/frontend/java/types.js +3 -0
  42. package/dist/src/frontend/java/types.js.map +1 -0
  43. package/dist/src/frontend/javascript/parser.d.ts +8 -0
  44. package/dist/src/frontend/javascript/parser.js +1121 -0
  45. package/dist/src/frontend/javascript/parser.js.map +1 -0
  46. package/dist/src/index.d.ts +14 -0
  47. package/dist/src/index.js +76 -0
  48. package/dist/src/index.js.map +1 -0
  49. package/output.txt +3820 -0
  50. package/package.json +73 -0
  51. package/tests/benchmark/base/class.js +32 -0
  52. package/tests/benchmark/base/class.js.json +1809 -0
  53. package/tests/benchmark/base/class_test.js +13 -0
  54. package/tests/benchmark/base/class_test.js.json +435 -0
  55. package/tests/benchmark/base/comment.js +21 -0
  56. package/tests/benchmark/base/comment.js.json +1443 -0
  57. package/tests/benchmark/base/demo.ts +1060 -0
  58. package/tests/benchmark/base/empty.js +4 -0
  59. package/tests/benchmark/base/empty.js.json +202 -0
  60. package/tests/benchmark/base/export.js +36 -0
  61. package/tests/benchmark/base/export.js.json +6345 -0
  62. package/tests/benchmark/base/export1.js +2 -0
  63. package/tests/benchmark/base/export1.js.json +251 -0
  64. package/tests/benchmark/base/export2.js +35 -0
  65. package/tests/benchmark/base/export2.js.json +3297 -0
  66. package/tests/benchmark/base/expressions.js +25 -0
  67. package/tests/benchmark/base/expressions.js.json +1654 -0
  68. package/tests/benchmark/base/for.js +7 -0
  69. package/tests/benchmark/base/for.js.json +738 -0
  70. package/tests/benchmark/base/function.js +15 -0
  71. package/tests/benchmark/base/function.js.json +1458 -0
  72. package/tests/benchmark/base/import.js +3 -0
  73. package/tests/benchmark/base/import.js.json +416 -0
  74. package/tests/benchmark/base/literal.js +27 -0
  75. package/tests/benchmark/base/literal.js.json +1234 -0
  76. package/tests/benchmark/base/param.js +4 -0
  77. package/tests/benchmark/base/param.js.json +410 -0
  78. package/tests/benchmark/base/parseerror.js +1 -0
  79. package/tests/benchmark/base/parseerror.js.json +191 -0
  80. package/tests/benchmark/base/patternlike.js +24 -0
  81. package/tests/benchmark/base/patternlike.js.json +3720 -0
  82. package/tests/benchmark/base/react.js +8 -0
  83. package/tests/benchmark/base/react.js.json +211 -0
  84. package/tests/benchmark/base/switch.js +11 -0
  85. package/tests/benchmark/base/switch.js.json +741 -0
  86. package/tests/benchmark/base/taint.ts +61 -0
  87. package/tests/benchmark/base/temp.js +311 -0
  88. package/tests/benchmark/base/temp.js.json +22170 -0
  89. package/tests/benchmark/base/tmp.js +134 -0
  90. package/tests/benchmark/base/tmp.js.json +15409 -0
  91. package/tests/benchmark/base/vardecl.js +9 -0
  92. package/tests/benchmark/base/vardecl.js.json +491 -0
  93. package/tests/index.js +43 -0
  94. package/tests/index.ts +66 -0
  95. package/tests/test.d.ts +0 -0
  96. package/tests/test.js +38 -0
  97. package/tests/test.ts +60 -0
  98. package/tsconfig.json +24 -0
  99. package/tsconfig.test.json +13 -0
  100. package/webpack.config.js +21 -0
@@ -0,0 +1,61 @@
1
+ import { Controller } from 'chair';
2
+ import { taintFlowOnDecorator } from '../authentication/taint_decorator';
3
+
4
+ class TaintController extends Controller {
5
+
6
+ @taintFlowOnDecorator
7
+ async t1({ userId }) {
8
+ const { ctx } = this;
9
+ const taint = ctx.request;
10
+ //## 【污点sink检出】
11
+ __chair_input_sink(taint);
12
+ //## 排除,预期没有检出
13
+ __chair_input_sink(_, taint);
14
+
15
+ const foo = {
16
+ a: 1,
17
+ taint: taint,
18
+ }
19
+
20
+ //## 【污点sink检出】
21
+ __chair_input_sink(foo);
22
+ //## 排除,预期没有检出
23
+ __chair_input_sink(foo.a);
24
+ //## 【污点sink检出】
25
+ __chair_input_sink(foo.taint);
26
+
27
+ // 污点sanitize
28
+ foo.taint = 1;
29
+ //## 排除,预期没有检出
30
+ __chair_input_sink(foo.taint);
31
+
32
+
33
+ let bar = 1;
34
+ if (ctx.request.input < 1) {
35
+ bar = taint;
36
+ //## 【污点sink检出】
37
+ __chair_input_sink(bar);
38
+ } else {
39
+ // false分支不受true分支赋值影响
40
+ //pass
41
+ __chair_input_sink(bar);
42
+ }
43
+
44
+ const output = {};
45
+ const ret = taint_flow(taint, output);
46
+
47
+ //## 【污点sink检出】
48
+ __chair_input_sink(ret);
49
+ //## 【污点sink检出】
50
+ __chair_input_sink(output);
51
+
52
+ await ctx.service.taint.taintService(taint);
53
+ }
54
+ }
55
+
56
+ function taint_flow(input, output) {
57
+ output.taint = input;
58
+ return input;
59
+ }
60
+
61
+ export default TaintController;
@@ -0,0 +1,311 @@
1
+ const util = require('util')
2
+ const _data = Symbol('data')
3
+ const _delete = Symbol('delete')
4
+ const _append = Symbol('append')
5
+
6
+ const sqBracketsMatcher = str => str.match(/(.+)\[([^\]]+)\]\.?(.*)$/)
7
+
8
+ // replaces any occurence of an empty-brackets (e.g: []) with a special
9
+ // Symbol(append) to represent it, this is going to be useful for the setter
10
+ // method that will push values to the end of the array when finding these
11
+ const replaceAppendSymbols = str => {
12
+ const matchEmptyBracket = str.match(/^(.*)\[\]\.?(.*)$/)
13
+
14
+ if (matchEmptyBracket) {
15
+ const [, pre, post] = matchEmptyBracket
16
+ return [...replaceAppendSymbols(pre), _append, post].filter(Boolean)
17
+ }
18
+
19
+ return [str]
20
+ }
21
+
22
+ const parseKeys = key => {
23
+ const sqBracketItems = new Set()
24
+ sqBracketItems.add(_append)
25
+ const parseSqBrackets = str => {
26
+ const index = sqBracketsMatcher(str)
27
+
28
+ // once we find square brackets, we recursively parse all these
29
+ if (index) {
30
+ const preSqBracketPortion = index[1]
31
+
32
+ // we want to have a `new String` wrapper here in order to differentiate
33
+ // between multiple occurences of the same string, e.g:
34
+ // foo.bar[foo.bar] should split into { foo: { bar: { 'foo.bar': {} } }
35
+ /* eslint-disable-next-line no-new-wrappers */
36
+ const foundKey = new String(index[2])
37
+ const postSqBracketPortion = index[3]
38
+
39
+ // we keep track of items found during this step to make sure
40
+ // we don't try to split-separate keys that were defined within
41
+ // square brackets, since the key name itself might contain dots
42
+ sqBracketItems.add(foundKey)
43
+
44
+ // returns an array that contains either dot-separate items (that will
45
+ // be splitted appart during the next step OR the fully parsed keys
46
+ // read from square brackets, e.g:
47
+ // foo.bar[1.0.0].a.b -> ['foo.bar', '1.0.0', 'a.b']
48
+ return [
49
+ ...parseSqBrackets(preSqBracketPortion),
50
+ foundKey,
51
+ ...(postSqBracketPortion ? parseSqBrackets(postSqBracketPortion) : []),
52
+ ]
53
+ }
54
+
55
+ // at the end of parsing, any usage of the special empty-bracket syntax
56
+ // (e.g: foo.array[]) has not yet been parsed, here we'll take care
57
+ // of parsing it and adding a special symbol to represent it in
58
+ // the resulting list of keys
59
+ return replaceAppendSymbols(str)
60
+ }
61
+
62
+ const res = []
63
+ // starts by parsing items defined as square brackets, those might be
64
+ // representing properties that have a dot in the name or just array
65
+ // indexes, e.g: foo[1.0.0] or list[0]
66
+ const sqBracketKeys = parseSqBrackets(key.trim())
67
+
68
+ for (const k of sqBracketKeys) {
69
+ // keys parsed from square brackets should just be added to list of
70
+ // resulting keys as they might have dots as part of the key
71
+ if (sqBracketItems.has(k)) {
72
+ res.push(k)
73
+ } else {
74
+ // splits the dot-sep property names and add them to the list of keys
75
+ /* eslint-disable-next-line no-new-wrappers */
76
+ for (const splitKey of k.split('.')) {
77
+ res.push(String(splitKey))
78
+ }
79
+ }
80
+ }
81
+
82
+ // returns an ordered list of strings in which each entry
83
+ // represents a key in an object defined by the previous entry
84
+ return res
85
+ }
86
+
87
+ const getter = ({ data, key }) => {
88
+ // keys are a list in which each entry represents the name of
89
+ // a property that should be walked through the object in order to
90
+ // return the final found value
91
+ const keys = parseKeys(key)
92
+ let _data = data
93
+ let label = ''
94
+
95
+ for (const k of keys) {
96
+ // empty-bracket-shortcut-syntax is not supported on getter
97
+ if (k === _append) {
98
+ throw Object.assign(new Error('Empty brackets are not valid syntax for retrieving values.'), {
99
+ code: 'EINVALIDSYNTAX',
100
+ })
101
+ }
102
+
103
+ // extra logic to take into account printing array, along with its
104
+ // special syntax in which using a dot-sep property name after an
105
+ // arry will expand it's results, e.g:
106
+ // arr.name -> arr[0].name=value, arr[1].name=value, ...
107
+ const maybeIndex = Number(k)
108
+ if (Array.isArray(_data) && !Number.isInteger(maybeIndex)) {
109
+ _data = _data.reduce((acc, i, index) => {
110
+ acc[`${label}[${index}].${k}`] = i[k]
111
+ return acc
112
+ }, {})
113
+ return _data
114
+ } else {
115
+ // if can't find any more values, it means it's just over
116
+ // and there's nothing to return
117
+ if (!_data[k]) {
118
+ return undefined
119
+ }
120
+
121
+ // otherwise sets the next value
122
+ _data = _data[k]
123
+ }
124
+
125
+ label += k
126
+ }
127
+
128
+ // these are some legacy expectations from
129
+ // the old API consumed by lib/view.js
130
+ if (Array.isArray(_data) && _data.length <= 1) {
131
+ _data = _data[0]
132
+ }
133
+
134
+ return {
135
+ [key]: _data,
136
+ }
137
+ }
138
+
139
+ const setter = ({ data, key, value, force }) => {
140
+ // setter goes to recursively transform the provided data obj,
141
+ // setting properties from the list of parsed keys, e.g:
142
+ // ['foo', 'bar', 'baz'] -> { foo: { bar: { baz: {} } }
143
+ const keys = parseKeys(key)
144
+ const setKeys = (_data, _key) => {
145
+ // handles array indexes, converting valid integers to numbers,
146
+ // note that occurences of Symbol(append) will throw,
147
+ // so we just ignore these for now
148
+ let maybeIndex = Number.NaN
149
+ try {
150
+ maybeIndex = Number(_key)
151
+ } catch {
152
+ // leave it NaN
153
+ }
154
+ if (!Number.isNaN(maybeIndex)) {
155
+ _key = maybeIndex
156
+ }
157
+
158
+ // creates new array in case key is an index
159
+ // and the array obj is not yet defined
160
+ const keyIsAnArrayIndex = _key === maybeIndex || _key === _append
161
+ const dataHasNoItems = !Object.keys(_data).length
162
+ if (keyIsAnArrayIndex && dataHasNoItems && !Array.isArray(_data)) {
163
+ _data = []
164
+ }
165
+
166
+ // converting from array to an object is also possible, in case the
167
+ // user is using force mode, we should also convert existing arrays
168
+ // to an empty object if the current _data is an array
169
+ if (force && Array.isArray(_data) && !keyIsAnArrayIndex) {
170
+ _data = { ..._data }
171
+ }
172
+
173
+ // the _append key is a special key that is used to represent
174
+ // the empty-bracket notation, e.g: arr[] -> arr[arr.length]
175
+ if (_key === _append) {
176
+ if (!Array.isArray(_data)) {
177
+ throw Object.assign(new Error(`Can't use append syntax in non-Array element`), {
178
+ code: 'ENOAPPEND',
179
+ })
180
+ }
181
+ _key = _data.length
182
+ }
183
+
184
+ // retrieves the next data object to recursively iterate on,
185
+ // throws if trying to override a literal value or add props to an array
186
+ const next = () => {
187
+ const haveContents = !force && _data[_key] != null && value !== _delete
188
+ const shouldNotOverrideLiteralValue = !(typeof _data[_key] === 'object')
189
+ // if the next obj to recurse is an array and the next key to be
190
+ // appended to the resulting obj is not an array index, then it
191
+ // should throw since we can't append arbitrary props to arrays
192
+ const shouldNotAddPropsToArrays =
193
+ typeof keys[0] !== 'symbol' && Array.isArray(_data[_key]) && Number.isNaN(Number(keys[0]))
194
+
195
+ const overrideError = haveContents && shouldNotOverrideLiteralValue
196
+ if (overrideError) {
197
+ throw Object.assign(
198
+ new Error(`Property ${_key} already exists and is not an Array or Object.`),
199
+ { code: 'EOVERRIDEVALUE' }
200
+ )
201
+ }
202
+
203
+ const addPropsToArrayError = haveContents && shouldNotAddPropsToArrays
204
+ if (addPropsToArrayError) {
205
+ throw Object.assign(new Error(`Can't add property ${key} to an Array.`), {
206
+ code: 'ENOADDPROP',
207
+ })
208
+ }
209
+
210
+ return typeof _data[_key] === 'object' ? _data[_key] || {} : {}
211
+ }
212
+
213
+ // sets items from the parsed array of keys as objects, recurses to
214
+ // setKeys in case there are still items to be handled, otherwise it
215
+ // just sets the original value set by the user
216
+ if (keys.length) {
217
+ _data[_key] = setKeys(next(), keys.shift())
218
+ } else {
219
+ // handles special deletion cases for obj props / array items
220
+ if (value === _delete) {
221
+ if (Array.isArray(_data)) {
222
+ _data.splice(_key, 1)
223
+ } else {
224
+ delete _data[_key]
225
+ }
226
+ } else {
227
+ // finally, sets the value in its right place
228
+ _data[_key] = value
229
+ }
230
+ }
231
+
232
+ return _data
233
+ }
234
+
235
+ setKeys(data, keys.shift())
236
+ }
237
+
238
+ class Queryable {
239
+ constructor (obj) {
240
+ if (!obj || typeof obj !== 'object') {
241
+ throw Object.assign(new Error('Queryable needs an object to query properties from.'), {
242
+ code: 'ENOQUERYABLEOBJ',
243
+ })
244
+ }
245
+
246
+ this[_data] = obj
247
+ }
248
+
249
+ query (queries) {
250
+ // this ugly interface here is meant to be a compatibility layer
251
+ // with the legacy API lib/view.js is consuming, if at some point
252
+ // we refactor that command then we can revisit making this nicer
253
+ if (queries === '') {
254
+ return { '': this[_data] }
255
+ }
256
+
257
+ const q = query =>
258
+ getter({
259
+ data: this[_data],
260
+ key: query,
261
+ })
262
+
263
+ if (Array.isArray(queries)) {
264
+ let res = {}
265
+ for (const query of queries) {
266
+ res = { ...res, ...q(query) }
267
+ }
268
+ return res
269
+ } else {
270
+ return q(queries)
271
+ }
272
+ }
273
+
274
+ // return the value for a single query if found, otherwise returns undefined
275
+ get (query) {
276
+ const obj = this.query(query)
277
+ if (obj) {
278
+ return obj[query]
279
+ }
280
+ }
281
+
282
+ // creates objects along the way for the provided `query` parameter
283
+ // and assigns `value` to the last property of the query chain
284
+ set (query, value, { force } = {}) {
285
+ setter({
286
+ data: this[_data],
287
+ key: query,
288
+ value,
289
+ force,
290
+ })
291
+ }
292
+
293
+ // deletes the value of the property found at `query`
294
+ delete (query) {
295
+ setter({
296
+ data: this[_data],
297
+ key: query,
298
+ value: _delete,
299
+ })
300
+ }
301
+
302
+ toJSON () {
303
+ return this[_data]
304
+ }
305
+
306
+ [util.inspect.custom] () {
307
+ return this.toJSON()
308
+ }
309
+ }
310
+
311
+ module.exports = Queryable