@sentriflow/core 0.1.0
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.
- package/LICENSE +190 -0
- package/README.md +86 -0
- package/package.json +60 -0
- package/src/constants.ts +77 -0
- package/src/engine/RuleExecutor.ts +256 -0
- package/src/engine/Runner.ts +312 -0
- package/src/engine/SandboxedExecutor.ts +208 -0
- package/src/errors.ts +88 -0
- package/src/helpers/arista/helpers.ts +1220 -0
- package/src/helpers/arista/index.ts +12 -0
- package/src/helpers/aruba/helpers.ts +637 -0
- package/src/helpers/aruba/index.ts +13 -0
- package/src/helpers/cisco/helpers.ts +534 -0
- package/src/helpers/cisco/index.ts +11 -0
- package/src/helpers/common/helpers.ts +265 -0
- package/src/helpers/common/index.ts +5 -0
- package/src/helpers/common/validation.ts +280 -0
- package/src/helpers/cumulus/helpers.ts +676 -0
- package/src/helpers/cumulus/index.ts +12 -0
- package/src/helpers/extreme/helpers.ts +422 -0
- package/src/helpers/extreme/index.ts +12 -0
- package/src/helpers/fortinet/helpers.ts +892 -0
- package/src/helpers/fortinet/index.ts +12 -0
- package/src/helpers/huawei/helpers.ts +790 -0
- package/src/helpers/huawei/index.ts +11 -0
- package/src/helpers/index.ts +53 -0
- package/src/helpers/juniper/helpers.ts +756 -0
- package/src/helpers/juniper/index.ts +12 -0
- package/src/helpers/mikrotik/helpers.ts +722 -0
- package/src/helpers/mikrotik/index.ts +12 -0
- package/src/helpers/nokia/helpers.ts +856 -0
- package/src/helpers/nokia/index.ts +11 -0
- package/src/helpers/paloalto/helpers.ts +939 -0
- package/src/helpers/paloalto/index.ts +12 -0
- package/src/helpers/vyos/helpers.ts +429 -0
- package/src/helpers/vyos/index.ts +12 -0
- package/src/index.ts +30 -0
- package/src/json-rules/ExpressionEvaluator.ts +292 -0
- package/src/json-rules/HelperRegistry.ts +177 -0
- package/src/json-rules/JsonRuleCompiler.ts +339 -0
- package/src/json-rules/JsonRuleValidator.ts +371 -0
- package/src/json-rules/index.ts +97 -0
- package/src/json-rules/schema.json +350 -0
- package/src/json-rules/types.ts +303 -0
- package/src/pack-loader/PackLoader.ts +332 -0
- package/src/pack-loader/index.ts +17 -0
- package/src/pack-loader/types.ts +135 -0
- package/src/parser/IncrementalParser.ts +527 -0
- package/src/parser/Sanitizer.ts +104 -0
- package/src/parser/SchemaAwareParser.ts +504 -0
- package/src/parser/VendorSchema.ts +72 -0
- package/src/parser/vendors/arista-eos.ts +206 -0
- package/src/parser/vendors/aruba-aoscx.ts +123 -0
- package/src/parser/vendors/aruba-aosswitch.ts +113 -0
- package/src/parser/vendors/aruba-wlc.ts +173 -0
- package/src/parser/vendors/cisco-ios.ts +110 -0
- package/src/parser/vendors/cisco-nxos.ts +107 -0
- package/src/parser/vendors/cumulus-linux.ts +161 -0
- package/src/parser/vendors/extreme-exos.ts +154 -0
- package/src/parser/vendors/extreme-voss.ts +167 -0
- package/src/parser/vendors/fortinet-fortigate.ts +217 -0
- package/src/parser/vendors/huawei-vrp.ts +192 -0
- package/src/parser/vendors/index.ts +1521 -0
- package/src/parser/vendors/juniper-junos.ts +230 -0
- package/src/parser/vendors/mikrotik-routeros.ts +274 -0
- package/src/parser/vendors/nokia-sros.ts +251 -0
- package/src/parser/vendors/paloalto-panos.ts +264 -0
- package/src/parser/vendors/vyos-vyos.ts +454 -0
- package/src/types/ConfigNode.ts +72 -0
- package/src/types/DeclarativeRule.ts +158 -0
- package/src/types/IRule.ts +270 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2025 SentriFlow Authors
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# @sentriflow/core
|
|
2
|
+
|
|
3
|
+
Core engine for SentriFlow - a network configuration linter and validator.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
`@sentriflow/core` provides the fundamental building blocks for parsing, analyzing, and validating network device configurations across multiple vendors.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @sentriflow/core
|
|
13
|
+
# or
|
|
14
|
+
bun add @sentriflow/core
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Features
|
|
18
|
+
|
|
19
|
+
- **Multi-vendor support**: Cisco IOS/NX-OS, Juniper JunOS, Arista EOS, Fortinet FortiGate, Palo Alto PAN-OS, and more
|
|
20
|
+
- **AST-based parsing**: Converts configurations into a vendor-agnostic Abstract Syntax Tree
|
|
21
|
+
- **Extensible rule engine**: Create custom validation rules using the rule API
|
|
22
|
+
- **TypeScript native**: Full type safety with comprehensive type definitions
|
|
23
|
+
|
|
24
|
+
## Supported Vendors
|
|
25
|
+
|
|
26
|
+
| Vendor | Platform | Status |
|
|
27
|
+
|--------|----------|--------|
|
|
28
|
+
| Cisco | IOS, IOS-XE, NX-OS | Supported |
|
|
29
|
+
| Juniper | JunOS | Supported |
|
|
30
|
+
| Arista | EOS | Supported |
|
|
31
|
+
| Fortinet | FortiGate | Supported |
|
|
32
|
+
| Palo Alto | PAN-OS | Supported |
|
|
33
|
+
| Nokia | SR-OS | Supported |
|
|
34
|
+
| Huawei | VRP | Supported |
|
|
35
|
+
| MikroTik | RouterOS | Supported |
|
|
36
|
+
| Cumulus | Linux | Supported |
|
|
37
|
+
| VyOS | VyOS | Supported |
|
|
38
|
+
| Extreme | EXOS | Supported |
|
|
39
|
+
| Aruba | AOS-CX | Supported |
|
|
40
|
+
|
|
41
|
+
## Basic Usage
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import { parse, validate } from '@sentriflow/core';
|
|
45
|
+
import { defaultRules } from '@sentriflow/rules-default';
|
|
46
|
+
|
|
47
|
+
// Parse a configuration file
|
|
48
|
+
const config = `
|
|
49
|
+
interface GigabitEthernet0/1
|
|
50
|
+
ip address 192.168.1.1 255.255.255.0
|
|
51
|
+
no shutdown
|
|
52
|
+
`;
|
|
53
|
+
|
|
54
|
+
const ast = parse(config, { vendor: 'cisco-ios' });
|
|
55
|
+
|
|
56
|
+
// Validate against rules
|
|
57
|
+
const results = validate(ast, defaultRules);
|
|
58
|
+
|
|
59
|
+
for (const issue of results) {
|
|
60
|
+
console.log(`${issue.severity}: ${issue.message}`);
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## API Reference
|
|
65
|
+
|
|
66
|
+
### `parse(config: string, options: ParseOptions): AST`
|
|
67
|
+
|
|
68
|
+
Parses a network configuration string into an AST.
|
|
69
|
+
|
|
70
|
+
### `validate(ast: AST, rules: Rule[]): ValidationResult[]`
|
|
71
|
+
|
|
72
|
+
Validates an AST against a set of rules.
|
|
73
|
+
|
|
74
|
+
### `detect(config: string): VendorInfo | null`
|
|
75
|
+
|
|
76
|
+
Auto-detects the vendor/platform from configuration content.
|
|
77
|
+
|
|
78
|
+
## Related Packages
|
|
79
|
+
|
|
80
|
+
- [`@sentriflow/cli`](https://github.com/sentriflow/sentriflow/tree/main/packages/cli) - Command-line interface
|
|
81
|
+
- [`@sentriflow/rules-default`](https://github.com/sentriflow/sentriflow/tree/main/packages/rules-default) - Default validation rules
|
|
82
|
+
- [`@sentriflow/rule-helpers`](https://github.com/sentriflow/sentriflow/tree/main/packages/rule-helpers) - Helper functions for rule development
|
|
83
|
+
|
|
84
|
+
## License
|
|
85
|
+
|
|
86
|
+
Apache-2.0
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sentriflow/core",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "SentriFlow core engine for network configuration validation",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"module": "src/index.ts",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./src/index.ts",
|
|
10
|
+
"./helpers": "./src/helpers/index.ts",
|
|
11
|
+
"./helpers/common": "./src/helpers/common/index.ts",
|
|
12
|
+
"./helpers/arista": "./src/helpers/arista/index.ts",
|
|
13
|
+
"./helpers/aruba": "./src/helpers/aruba/index.ts",
|
|
14
|
+
"./helpers/cisco": "./src/helpers/cisco/index.ts",
|
|
15
|
+
"./helpers/cumulus": "./src/helpers/cumulus/index.ts",
|
|
16
|
+
"./helpers/extreme": "./src/helpers/extreme/index.ts",
|
|
17
|
+
"./helpers/fortinet": "./src/helpers/fortinet/index.ts",
|
|
18
|
+
"./helpers/huawei": "./src/helpers/huawei/index.ts",
|
|
19
|
+
"./helpers/juniper": "./src/helpers/juniper/index.ts",
|
|
20
|
+
"./helpers/mikrotik": "./src/helpers/mikrotik/index.ts",
|
|
21
|
+
"./helpers/nokia": "./src/helpers/nokia/index.ts",
|
|
22
|
+
"./helpers/paloalto": "./src/helpers/paloalto/index.ts",
|
|
23
|
+
"./helpers/vyos": "./src/helpers/vyos/index.ts"
|
|
24
|
+
},
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "https://github.com/sentriflow/sentriflow.git",
|
|
28
|
+
"directory": "packages/core"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/sentriflow/sentriflow#readme",
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/sentriflow/sentriflow/issues"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"network",
|
|
36
|
+
"configuration",
|
|
37
|
+
"validation",
|
|
38
|
+
"security",
|
|
39
|
+
"cisco",
|
|
40
|
+
"juniper",
|
|
41
|
+
"arista",
|
|
42
|
+
"firewall",
|
|
43
|
+
"linter",
|
|
44
|
+
"helpers"
|
|
45
|
+
],
|
|
46
|
+
"files": [
|
|
47
|
+
"src",
|
|
48
|
+
"LICENSE",
|
|
49
|
+
"README.md"
|
|
50
|
+
],
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"bun-types": "latest"
|
|
56
|
+
},
|
|
57
|
+
"peerDependencies": {
|
|
58
|
+
"typescript": "^5.0.0"
|
|
59
|
+
}
|
|
60
|
+
}
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// packages/core/src/constants.ts
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Security and performance constants for SentriFlow.
|
|
5
|
+
* Centralized configuration for limits and thresholds.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** Maximum length of a single line before skipping regex evaluation (ReDoS protection) */
|
|
9
|
+
export const MAX_LINE_LENGTH = 2048;
|
|
10
|
+
|
|
11
|
+
/** Maximum size of configuration content in bytes */
|
|
12
|
+
export const MAX_CONFIG_SIZE = 10 * 1024 * 1024; // 10MB
|
|
13
|
+
|
|
14
|
+
/** Maximum nesting depth for parsed configuration blocks */
|
|
15
|
+
export const MAX_NESTING_DEPTH = 50;
|
|
16
|
+
|
|
17
|
+
/** Maximum number of lines to parse */
|
|
18
|
+
export const MAX_LINE_COUNT = 100_000;
|
|
19
|
+
|
|
20
|
+
/** Maximum traversal depth when searching for config files */
|
|
21
|
+
export const MAX_TRAVERSAL_DEPTH = 20;
|
|
22
|
+
|
|
23
|
+
/** Allowed extensions for config/rules files */
|
|
24
|
+
export const ALLOWED_CONFIG_EXTENSIONS = ['.js', '.ts', '.mjs', '.cjs'];
|
|
25
|
+
|
|
26
|
+
/** SEC-012: Allowed extensions for encrypted rule packs */
|
|
27
|
+
export const ALLOWED_ENCRYPTED_PACK_EXTENSIONS = ['.grpx'];
|
|
28
|
+
|
|
29
|
+
/** Allowed extensions for JSON rule files */
|
|
30
|
+
export const ALLOWED_JSON_RULES_EXTENSIONS = ['.json'];
|
|
31
|
+
|
|
32
|
+
/** Maximum file size for config/rules files (1MB) */
|
|
33
|
+
export const MAX_CONFIG_FILE_SIZE = 1024 * 1024;
|
|
34
|
+
|
|
35
|
+
/** SEC-012: Maximum file size for encrypted rule packs (5MB) */
|
|
36
|
+
export const MAX_ENCRYPTED_PACK_SIZE = 5 * 1024 * 1024;
|
|
37
|
+
|
|
38
|
+
/** Maximum number of external rules that can be registered */
|
|
39
|
+
export const MAX_EXTERNAL_RULES = 2000;
|
|
40
|
+
|
|
41
|
+
/** Rule execution timeout in milliseconds */
|
|
42
|
+
export const RULE_TIMEOUT_MS = 5000;
|
|
43
|
+
|
|
44
|
+
// ============================================================================
|
|
45
|
+
// Performance Optimization Constants
|
|
46
|
+
// ============================================================================
|
|
47
|
+
|
|
48
|
+
/** Use simple (non-indexed) engine below this rule count */
|
|
49
|
+
export const MAX_RULES_WITHOUT_INDEX = 50;
|
|
50
|
+
|
|
51
|
+
/** Percentage of lines changed to trigger full reparse (0.3 = 30%) */
|
|
52
|
+
export const INCREMENTAL_PARSE_THRESHOLD = 0.3;
|
|
53
|
+
|
|
54
|
+
/** Maximum time per rule per node in milliseconds */
|
|
55
|
+
export const RULE_PER_NODE_TIMEOUT_MS = 100;
|
|
56
|
+
|
|
57
|
+
/** Maximum total scan time in milliseconds */
|
|
58
|
+
export const SCAN_TIMEOUT_MS = 5000;
|
|
59
|
+
|
|
60
|
+
/** Pattern for valid rule IDs */
|
|
61
|
+
export const RULE_ID_PATTERN = /^[A-Z][A-Z0-9_-]{2,49}$/;
|
|
62
|
+
|
|
63
|
+
// ============================================================================
|
|
64
|
+
// JSON Rules Security Constants
|
|
65
|
+
// ============================================================================
|
|
66
|
+
|
|
67
|
+
/** Maximum length for regex patterns in JSON rules (ReDoS protection) */
|
|
68
|
+
export const MAX_PATTERN_LENGTH = 500;
|
|
69
|
+
|
|
70
|
+
/** Maximum length for metadata string fields in JSON rules */
|
|
71
|
+
export const MAX_METADATA_LENGTH = 10_000;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* ReDoS detection pattern - matches nested quantifiers that cause exponential backtracking.
|
|
75
|
+
* Detects patterns like (a+)+, (a*)+, (a+)*, (a|b+)+, etc.
|
|
76
|
+
*/
|
|
77
|
+
export const REDOS_PATTERN = /\([^)]*[+*][^)]*\)[+*]|\(\?:[^)]*[+*][^)]*\)[+*]/;
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
// packages/core/src/engine/RuleExecutor.ts
|
|
2
|
+
|
|
3
|
+
import type { ConfigNode } from '../types/ConfigNode';
|
|
4
|
+
import type { IRule, RuleResult, Context } from '../types/IRule';
|
|
5
|
+
import { RULE_PER_NODE_TIMEOUT_MS } from '../constants';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Options for rule execution.
|
|
9
|
+
*/
|
|
10
|
+
export interface ExecutionOptions {
|
|
11
|
+
/** Timeout in milliseconds per rule per node. Default: RULE_PER_NODE_TIMEOUT_MS */
|
|
12
|
+
timeoutMs?: number;
|
|
13
|
+
/** Number of timeouts before auto-disabling a rule. Default: 3 */
|
|
14
|
+
maxTimeouts?: number;
|
|
15
|
+
/** Callback when a rule times out */
|
|
16
|
+
onTimeout?: (ruleId: string, nodeId: string, elapsedMs: number) => void;
|
|
17
|
+
/** Callback when a rule is auto-disabled */
|
|
18
|
+
onRuleDisabled?: (ruleId: string, reason: string) => void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Statistics about rule execution.
|
|
23
|
+
*/
|
|
24
|
+
export interface ExecutionStats {
|
|
25
|
+
/** Total rules executed */
|
|
26
|
+
rulesExecuted: number;
|
|
27
|
+
/** Total execution time in milliseconds */
|
|
28
|
+
totalTimeMs: number;
|
|
29
|
+
/** Number of timeouts detected */
|
|
30
|
+
timeoutCount: number;
|
|
31
|
+
/** Rules that exceeded timeout threshold */
|
|
32
|
+
slowRules: Map<string, { count: number; totalMs: number }>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Handles rule execution with timeout protection and auto-disabling of slow rules.
|
|
37
|
+
*
|
|
38
|
+
* Features:
|
|
39
|
+
* - Tracks execution time per rule
|
|
40
|
+
* - Auto-disables rules that repeatedly timeout
|
|
41
|
+
* - Provides execution statistics for debugging
|
|
42
|
+
* - Graceful error handling
|
|
43
|
+
*
|
|
44
|
+
* Usage:
|
|
45
|
+
* ```typescript
|
|
46
|
+
* const executor = new RuleExecutor();
|
|
47
|
+
*
|
|
48
|
+
* const result = executor.execute(rule, node, context);
|
|
49
|
+
*
|
|
50
|
+
* // Check for disabled rules
|
|
51
|
+
* const disabled = executor.getDisabledRules();
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export class RuleExecutor {
|
|
55
|
+
private timeoutCounts = new Map<string, number>();
|
|
56
|
+
private executionTimes = new Map<string, { count: number; totalMs: number }>();
|
|
57
|
+
private disabledRules = new Set<string>();
|
|
58
|
+
private options: Required<ExecutionOptions>;
|
|
59
|
+
|
|
60
|
+
constructor(options: ExecutionOptions = {}) {
|
|
61
|
+
this.options = {
|
|
62
|
+
timeoutMs: options.timeoutMs ?? RULE_PER_NODE_TIMEOUT_MS,
|
|
63
|
+
maxTimeouts: options.maxTimeouts ?? 3,
|
|
64
|
+
onTimeout: options.onTimeout ?? (() => {}),
|
|
65
|
+
onRuleDisabled: options.onRuleDisabled ?? (() => {}),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Execute a rule against a node with timeout protection.
|
|
71
|
+
*
|
|
72
|
+
* @param rule The rule to execute
|
|
73
|
+
* @param node The configuration node to check
|
|
74
|
+
* @param context The execution context
|
|
75
|
+
* @returns RuleResult or null if rule is disabled/skipped
|
|
76
|
+
*/
|
|
77
|
+
public execute(
|
|
78
|
+
rule: IRule,
|
|
79
|
+
node: ConfigNode,
|
|
80
|
+
context: Context
|
|
81
|
+
): RuleResult | null {
|
|
82
|
+
// Skip disabled rules
|
|
83
|
+
if (this.disabledRules.has(rule.id)) {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const startTime = performance.now();
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
const result = rule.check(node, context);
|
|
91
|
+
const elapsed = performance.now() - startTime;
|
|
92
|
+
|
|
93
|
+
// Track execution time
|
|
94
|
+
this.trackExecutionTime(rule.id, elapsed);
|
|
95
|
+
|
|
96
|
+
// Check for timeout
|
|
97
|
+
if (elapsed > this.options.timeoutMs) {
|
|
98
|
+
this.handleTimeout(rule.id, node.id, elapsed);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return result;
|
|
102
|
+
} catch (error) {
|
|
103
|
+
const elapsed = performance.now() - startTime;
|
|
104
|
+
this.trackExecutionTime(rule.id, elapsed);
|
|
105
|
+
|
|
106
|
+
// SEC-005: Sanitize error message to prevent information disclosure
|
|
107
|
+
// Don't expose internal error details which could reveal file paths or sensitive data
|
|
108
|
+
return {
|
|
109
|
+
passed: false,
|
|
110
|
+
message: `Rule ${rule.id} failed to execute. Check rule implementation.`,
|
|
111
|
+
ruleId: rule.id,
|
|
112
|
+
nodeId: node.id,
|
|
113
|
+
level: 'error',
|
|
114
|
+
loc: node.loc,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Track execution time for a rule.
|
|
121
|
+
*/
|
|
122
|
+
private trackExecutionTime(ruleId: string, elapsedMs: number): void {
|
|
123
|
+
const existing = this.executionTimes.get(ruleId) ?? { count: 0, totalMs: 0 };
|
|
124
|
+
existing.count++;
|
|
125
|
+
existing.totalMs += elapsedMs;
|
|
126
|
+
this.executionTimes.set(ruleId, existing);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Handle a rule timeout.
|
|
131
|
+
*/
|
|
132
|
+
private handleTimeout(ruleId: string, nodeId: string, elapsedMs: number): void {
|
|
133
|
+
const currentCount = (this.timeoutCounts.get(ruleId) ?? 0) + 1;
|
|
134
|
+
this.timeoutCounts.set(ruleId, currentCount);
|
|
135
|
+
|
|
136
|
+
this.options.onTimeout(ruleId, nodeId, elapsedMs);
|
|
137
|
+
|
|
138
|
+
// Auto-disable after max timeouts
|
|
139
|
+
if (currentCount >= this.options.maxTimeouts) {
|
|
140
|
+
this.disabledRules.add(ruleId);
|
|
141
|
+
this.options.onRuleDisabled(
|
|
142
|
+
ruleId,
|
|
143
|
+
`Auto-disabled after ${currentCount} timeouts (>${this.options.timeoutMs}ms)`
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Check if a rule is disabled.
|
|
150
|
+
*/
|
|
151
|
+
public isDisabled(ruleId: string): boolean {
|
|
152
|
+
return this.disabledRules.has(ruleId);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Get list of auto-disabled rule IDs.
|
|
157
|
+
*/
|
|
158
|
+
public getDisabledRules(): string[] {
|
|
159
|
+
return Array.from(this.disabledRules);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Get timeout count for a specific rule.
|
|
164
|
+
*/
|
|
165
|
+
public getTimeoutCount(ruleId: string): number {
|
|
166
|
+
return this.timeoutCounts.get(ruleId) ?? 0;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Manually disable a rule.
|
|
171
|
+
*/
|
|
172
|
+
public disableRule(ruleId: string): void {
|
|
173
|
+
this.disabledRules.add(ruleId);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Re-enable a previously disabled rule.
|
|
178
|
+
*/
|
|
179
|
+
public enableRule(ruleId: string): void {
|
|
180
|
+
this.disabledRules.delete(ruleId);
|
|
181
|
+
this.timeoutCounts.delete(ruleId);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Reset timeout counts (e.g., when rules are reloaded).
|
|
186
|
+
* Does not re-enable disabled rules.
|
|
187
|
+
*/
|
|
188
|
+
public resetTimeoutCounts(): void {
|
|
189
|
+
this.timeoutCounts.clear();
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Reset all state including disabled rules.
|
|
194
|
+
*/
|
|
195
|
+
public resetAll(): void {
|
|
196
|
+
this.timeoutCounts.clear();
|
|
197
|
+
this.executionTimes.clear();
|
|
198
|
+
this.disabledRules.clear();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Get execution statistics.
|
|
203
|
+
*/
|
|
204
|
+
public getStats(): ExecutionStats {
|
|
205
|
+
let totalExecutions = 0;
|
|
206
|
+
let totalTime = 0;
|
|
207
|
+
const slowRules = new Map<string, { count: number; totalMs: number }>();
|
|
208
|
+
|
|
209
|
+
for (const [ruleId, stats] of this.executionTimes) {
|
|
210
|
+
totalExecutions += stats.count;
|
|
211
|
+
totalTime += stats.totalMs;
|
|
212
|
+
|
|
213
|
+
// Consider rules slow if average time > 10ms
|
|
214
|
+
const avgTime = stats.totalMs / stats.count;
|
|
215
|
+
if (avgTime > 10) {
|
|
216
|
+
slowRules.set(ruleId, stats);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return {
|
|
221
|
+
rulesExecuted: totalExecutions,
|
|
222
|
+
totalTimeMs: totalTime,
|
|
223
|
+
timeoutCount: Array.from(this.timeoutCounts.values()).reduce((a, b) => a + b, 0),
|
|
224
|
+
slowRules,
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Get average execution time for a rule.
|
|
230
|
+
*/
|
|
231
|
+
public getAverageTime(ruleId: string): number {
|
|
232
|
+
const stats = this.executionTimes.get(ruleId);
|
|
233
|
+
if (!stats || stats.count === 0) return 0;
|
|
234
|
+
return stats.totalMs / stats.count;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Get the slowest rules by average execution time.
|
|
239
|
+
*
|
|
240
|
+
* @param limit Maximum number of rules to return
|
|
241
|
+
* @returns Array of [ruleId, avgTimeMs] sorted by time descending
|
|
242
|
+
*/
|
|
243
|
+
public getSlowestRules(limit = 10): Array<[string, number]> {
|
|
244
|
+
const avgTimes: Array<[string, number]> = [];
|
|
245
|
+
|
|
246
|
+
for (const [ruleId, stats] of this.executionTimes) {
|
|
247
|
+
if (stats.count > 0) {
|
|
248
|
+
avgTimes.push([ruleId, stats.totalMs / stats.count]);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
return avgTimes
|
|
253
|
+
.sort((a, b) => b[1] - a[1])
|
|
254
|
+
.slice(0, limit);
|
|
255
|
+
}
|
|
256
|
+
}
|