@contrast/common 1.0.1 → 1.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.
@@ -1,6 +1,7 @@
1
1
  export declare enum Event {
2
2
  ASSESS = "assess",
3
- PROTECT = "protect"
3
+ PROTECT = "protect",
4
+ SERVER_SETTINGS_UPDATE = "server-settings-update"
4
5
  }
5
6
  export declare enum Rule {
6
7
  BOT_BLOCKER = "bot-blocker",
@@ -21,3 +22,28 @@ export declare enum Rule {
21
22
  UNSAFE_FILE_UPLOAD = "unsafe-file-upload",
22
23
  XXE = "xxe"
23
24
  }
25
+ export declare enum InputType {
26
+ UNDEFINED_TYPE = "UNDEFINED_TYPE",
27
+ BODY = "BODY",
28
+ COOKIE_NAME = "COOKIE_NAME",
29
+ COOKIE_VALUE = "COOKIE_VALUE",
30
+ HEADER = "HEADER",
31
+ PARAMETER_NAME = "PARAMETER_NAME",
32
+ PARAMETER_VALUE = "PARAMETER_VALUE",
33
+ QUERYSTRING = "QUERYSTRING",
34
+ URI = "URI",
35
+ SOCKET = "SOCKET",
36
+ JSON_VALUE = "JSON_VALUE",
37
+ JSON_ARRAYED_VALUE = "JSON_ARRAYED_VALUE",
38
+ MULTIPART_CONTENT_TYPE = "MULTIPART_CONTENT_TYPE",
39
+ MULTIPART_VALUE = "MULTIPART_VALUE",
40
+ MULTIPART_FIELD_NAME = "MULTIPART_FIELD_NAME",
41
+ MULTIPART_NAME = "MULTIPART_NAME",
42
+ XML_VALUE = "XML_VALUE",
43
+ DWR_VALUE = "DWR_VALUE",
44
+ METHOD = "METHOD",
45
+ REQUEST = "REQUEST",
46
+ URL_PARAMETER = "URL_PARAMETER",
47
+ UNKNOWN = "UNKNOWN"
48
+ }
49
+ export declare const BLOCKING_MODES: string[];
package/lib/constants.js CHANGED
@@ -1,10 +1,25 @@
1
1
  "use strict";
2
+ /*
3
+ * Copyright: 2022 Contrast Security, Inc
4
+ * Contact: support@contrastsecurity.com
5
+ * License: Commercial
6
+
7
+ * NOTICE: This Software and the patented inventions embodied within may only be
8
+ * used as part of Contrast Security’s commercial offerings. Even though it is
9
+ * made available through public repositories, use of this Software is subject to
10
+ * the applicable End User Licensing Agreement found at
11
+ * https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
12
+ * between Contrast Security and the End User. The Software may not be reverse
13
+ * engineered, modified, repackaged, sold, redistributed or otherwise used in a
14
+ * way not consistent with the End User License Agreement.
15
+ */
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Rule = exports.Event = void 0;
17
+ exports.BLOCKING_MODES = exports.InputType = exports.Rule = exports.Event = void 0;
4
18
  var Event;
5
19
  (function (Event) {
6
20
  Event["ASSESS"] = "assess";
7
21
  Event["PROTECT"] = "protect";
22
+ Event["SERVER_SETTINGS_UPDATE"] = "server-settings-update";
8
23
  })(Event = exports.Event || (exports.Event = {}));
9
24
  var Rule;
10
25
  (function (Rule) {
@@ -26,4 +41,30 @@ var Rule;
26
41
  Rule["UNSAFE_FILE_UPLOAD"] = "unsafe-file-upload";
27
42
  Rule["XXE"] = "xxe";
28
43
  })(Rule = exports.Rule || (exports.Rule = {}));
44
+ var InputType;
45
+ (function (InputType) {
46
+ InputType["UNDEFINED_TYPE"] = "UNDEFINED_TYPE";
47
+ InputType["BODY"] = "BODY";
48
+ InputType["COOKIE_NAME"] = "COOKIE_NAME";
49
+ InputType["COOKIE_VALUE"] = "COOKIE_VALUE";
50
+ InputType["HEADER"] = "HEADER";
51
+ InputType["PARAMETER_NAME"] = "PARAMETER_NAME";
52
+ InputType["PARAMETER_VALUE"] = "PARAMETER_VALUE";
53
+ InputType["QUERYSTRING"] = "QUERYSTRING";
54
+ InputType["URI"] = "URI";
55
+ InputType["SOCKET"] = "SOCKET";
56
+ InputType["JSON_VALUE"] = "JSON_VALUE";
57
+ InputType["JSON_ARRAYED_VALUE"] = "JSON_ARRAYED_VALUE";
58
+ InputType["MULTIPART_CONTENT_TYPE"] = "MULTIPART_CONTENT_TYPE";
59
+ InputType["MULTIPART_VALUE"] = "MULTIPART_VALUE";
60
+ InputType["MULTIPART_FIELD_NAME"] = "MULTIPART_FIELD_NAME";
61
+ InputType["MULTIPART_NAME"] = "MULTIPART_NAME";
62
+ InputType["XML_VALUE"] = "XML_VALUE";
63
+ InputType["DWR_VALUE"] = "DWR_VALUE";
64
+ InputType["METHOD"] = "METHOD";
65
+ InputType["REQUEST"] = "REQUEST";
66
+ InputType["URL_PARAMETER"] = "URL_PARAMETER";
67
+ InputType["UNKNOWN"] = "UNKNOWN";
68
+ })(InputType = exports.InputType || (exports.InputType = {}));
69
+ exports.BLOCKING_MODES = ['block', 'block_at_perimeter'];
29
70
  //# sourceMappingURL=constants.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";;;AAAA,IAAY,KAGX;AAHD,WAAY,KAAK;IACf,0BAAiB,CAAA;IACjB,4BAAmB,CAAA;AACrB,CAAC,EAHW,KAAK,GAAL,aAAK,KAAL,aAAK,QAGhB;AAED,IAAY,IAkBX;AAlBD,WAAY,IAAI;IACd,mCAA2B,CAAA;IAC3B,uCAA+B,CAAA;IAC/B,2EAAmE,CAAA;IACnE,2FAAmF,CAAA;IACnF,yFAAiF,CAAA;IACjF,mCAA2B,CAAA;IAC3B,6CAAqC,CAAA;IACrC,2CAAmC,CAAA;IACnC,uDAA+C,CAAA;IAC/C,yCAAiC,CAAA;IACjC,uCAA+B,CAAA;IAC/B,uCAA+B,CAAA;IAC/B,yCAAiC,CAAA;IACjC,uCAA+B,CAAA;IAC/B,+DAAuD,CAAA;IACvD,iDAAyC,CAAA;IACzC,mBAAW,CAAA;AACb,CAAC,EAlBW,IAAI,GAAJ,YAAI,KAAJ,YAAI,QAkBf"}
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;GAaG;;;AAEH,IAAY,KAIX;AAJD,W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'./types';
5
5
  * String object.
6
6
  */
7
7
  export declare function isString(value: unknown): value is string | String;
8
+ export declare function isNonEmptyObject(value: unknown): value is object;
8
9
  export declare function encodeString(str: string): string;
10
+ export declare function simpleTraverse(obj: any, cb: Function): void;
package/lib/index.js CHANGED
@@ -1,4 +1,18 @@
1
1
  "use strict";
2
+ /*
3
+ * Copyright: 2022 Contrast Security, Inc
4
+ * Contact: support@contrastsecurity.com
5
+ * License: Commercial
6
+
7
+ * NOTICE: This Software and the patented inventions embodied within may only be
8
+ * used as part of Contrast Security’s commercial offerings. Even though it is
9
+ * made available through public repositories, use of this Software is subject to
10
+ * the applicable End User Licensing Agreement found at
11
+ * https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
12
+ * between Contrast Security and the End User. The Software may not be reverse
13
+ * engineered, modified, repackaged, sold, redistributed or otherwise used in a
14
+ * way not consistent with the End User License Agreement.
15
+ */
2
16
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
17
  if (k2 === undefined) k2 = k;
4
18
  var desc = Object.getOwnPropertyDescriptor(m, k);
@@ -14,7 +28,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
28
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
29
  };
16
30
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.encodeString = exports.isString = void 0;
31
+ exports.simpleTraverse = exports.encodeString = exports.isNonEmptyObject = exports.isString = void 0;
18
32
  __exportStar(require("./constants"), exports);
19
33
  __exportStar(require("./types"), exports);
20
34
  /**
@@ -26,8 +40,55 @@ function isString(value) {
26
40
  return typeof value === 'string' || value instanceof String;
27
41
  }
28
42
  exports.isString = isString;
43
+ function isNonEmptyObject(value) {
44
+ return !!value && typeof value === 'object' && Object.keys(value).length > 0;
45
+ }
46
+ exports.isNonEmptyObject = isNonEmptyObject;
47
+ /* c8 ignore next 3 */
29
48
  function encodeString(str) {
30
49
  return Buffer.from(str).toString('base64');
31
50
  }
32
51
  exports.encodeString = encodeString;
52
+ function simpleTraverse(obj, cb) {
53
+ if (typeof obj !== 'object' || obj === null) {
54
+ return;
55
+ }
56
+ const path = [];
57
+ /* eslint-disable complexity */
58
+ function traverse(obj) {
59
+ const isArray = Array.isArray(obj);
60
+ for (const k in obj) {
61
+ if (isArray) {
62
+ const _k = Number(k);
63
+ // if it is an array, store each index in path but don't call the
64
+ // callback on the index itself as they are just numeric strings.
65
+ path.push(_k);
66
+ if (typeof obj[_k] === 'object' && obj[_k] !== null) {
67
+ traverse(obj[_k]);
68
+ }
69
+ else if (typeof obj[_k] === 'string' && obj[_k]) {
70
+ cb(path, 'Value', obj[_k], obj);
71
+ }
72
+ path.pop();
73
+ }
74
+ else if (typeof obj[k] === 'object' && obj[k] !== null) {
75
+ cb(path, 'Key', k, obj);
76
+ path.push(k);
77
+ traverse(obj[k]);
78
+ path.pop();
79
+ }
80
+ else {
81
+ cb(path, 'Key', k, obj);
82
+ // only callback if the value is a non-empty string
83
+ if (typeof obj[k] === 'string' && obj[k]) {
84
+ path.push(k);
85
+ cb(path, 'Value', obj[k], obj);
86
+ path.pop();
87
+ }
88
+ }
89
+ }
90
+ }
91
+ traverse(obj);
92
+ }
93
+ exports.simpleTraverse = simpleTraverse;
33
94
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,8CAA4B;AAC5B,0CAAwB;AAExB;;;GAGG;AACH,wDAAwD;AACxD,SAAgB,QAAQ,CAAC,KAAc;IACrC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,YAAY,MAAM,CAAC;AAC9D,CAAC;AAFD,4BAEC;AAED,SAAgB,YAAY,CAAC,GAAW;IACtC,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC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AAE,CAAC,KAAK,IAAI,EAAE;oBACnD,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC;iBACnB;qBAAM,IAAI,OAAO,GAAG,CAAC,EAAE,CAAC,KAAK,QAAQ,IAAI,GAAG,CAAC,EAAE,CAAC,EAAE;oBACjD,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,CAAC,EAAE,CAAC,EAAE,GAAG,CAAC,CAAC;iBACjC;gBACD,IAAI,CAAC,GAAG,EAAE,CAAC;aACZ;iBAAM,IAAI,OAAO,GAAG,CAAC,CAAC,CAAC,KAAK,QAAQ,IAAI,GAAG,CAAC,CAAC,CAAC,KAAK,IAAI,EAAE;gBACxD,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,CAAC;gBACxB,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;gBACb,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;gBACjB,IAAI,CAAC,GAAG,EAAE,CAAC;aACZ;iBAAM;gBACL,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,CAAC;gBACxB,mDAAmD;gBACnD,IAAI,OAAO,GAAG,CAAC,CAAC,CAAC,KAAK,QAAQ,IAAI,GAAG,CAAC,CAAC,CAAC,EAAE;oBACxC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;oBACb,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;oBAC/B,IAAI,CAAC,GAAG,EAAE,CAAC;iBACZ;aACF;SACF;IACH,CAAC;IAED,QAAQ,CAAC,GAAG,CAAC,CAAC;AAChB,CAAC;AAtCD,wCAsCC"}
package/lib/types.d.ts ADDED
@@ -0,0 +1,91 @@
1
+ /// <reference types="node" />
2
+ import { Event, Rule } from './constants';
3
+ import { EventEmitter } from 'events';
4
+ export interface RuleConfig {
5
+ mode: 'monitor' | 'block' | 'block_at_perimeter' | 'off';
6
+ }
7
+ export declare type SemanticAnalysisRules = Rule.CMD_INJECTION_SEMANTIC_CHAINED_COMMANDS | Rule.CMD_INJECTION_COMMAND_BACKDOORS | Rule.CMD_INJECTION_SEMANTIC_DANGEROUS_PATHS;
8
+ export interface RulesConfig extends Record<Rule, RuleConfig> {
9
+ }
10
+ export interface Result {
11
+ blocked: boolean;
12
+ ruleId: Rule;
13
+ inputType: string;
14
+ path?: string[];
15
+ key?: string;
16
+ value: string;
17
+ details?: any[];
18
+ mongoExpansionResult?: boolean;
19
+ }
20
+ export interface SemanticAnalysisResult extends Result {
21
+ findings?: {
22
+ command?: string;
23
+ };
24
+ sinkContext?: any;
25
+ }
26
+ export interface HardeningResult extends Result {
27
+ findings?: {
28
+ command?: boolean;
29
+ deserializer?: string;
30
+ };
31
+ sinkContext?: any;
32
+ }
33
+ export interface ServerFeaturePreliminaryResult {
34
+ name?: string;
35
+ uuid: string;
36
+ ip?: string;
37
+ }
38
+ export interface ServerFeatureResult extends Result {
39
+ details?: ServerFeaturePreliminaryResult[];
40
+ }
41
+ export interface ReqData {
42
+ method: string;
43
+ headers: string[];
44
+ uriPath: string;
45
+ queries: string;
46
+ contentType?: string;
47
+ standardUrlParsing: boolean;
48
+ ip: string;
49
+ httpVersion: string;
50
+ }
51
+ export interface Findings {
52
+ trackRequest: boolean;
53
+ securityException?: [mode: string, ruleId: string];
54
+ bodyType?: 'json' | 'urlencoded';
55
+ resultsMap: Record<Rule, Result[]>;
56
+ semanticResultsMap: Record<Rule, SemanticAnalysisResult[]>;
57
+ serverFeaturesResultsMap: Record<Rule, ServerFeaturePreliminaryResult[]>;
58
+ hardeningResultsMap: Record<Rule, HardeningResult[]>;
59
+ }
60
+ export interface RequestStore {
61
+ protect?: ProtectMessage;
62
+ }
63
+ export interface ProtectMessage {
64
+ reqData: ReqData;
65
+ block: (mode: string, ruleId: string) => void;
66
+ rules: {
67
+ agentLibRules: RulesConfig;
68
+ agentLibRulesMask: number;
69
+ agentRules: RulesConfig;
70
+ };
71
+ exclusions: any[];
72
+ virtualPatches: any[];
73
+ findings: Findings;
74
+ parsedBody: any;
75
+ parsedCookies: any;
76
+ parsedParams: any;
77
+ parsedQuery: any;
78
+ }
79
+ export interface Messages extends EventEmitter {
80
+ addListener(event: Event.PROTECT, listener: (msg: RequestStore) => void): this;
81
+ emit(event: Event.PROTECT, msg: RequestStore): boolean;
82
+ emit(event: Event.SERVER_SETTINGS_UPDATE, msg: {
83
+ [key: string]: any;
84
+ }): boolean;
85
+ on(event: Event.PROTECT, listener: (msg: RequestStore) => void): this;
86
+ on(event: Event.SERVER_SETTINGS_UPDATE, listener: (msg: {
87
+ [key: string]: any;
88
+ }) => void): this;
89
+ prependListener(event: Event.PROTECT, listener: (msg: RequestStore) => void): this;
90
+ prependOnceListener(event: Event.PROTECT, listener: (msg: RequestStore) => void): this;
91
+ }
package/lib/types.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright: 2022 Contrast Security, Inc
4
+ * Contact: support@contrastsecurity.com
5
+ * License: Commercial
6
+
7
+ * NOTICE: This Software and the patented inventions embodied within may only be
8
+ * used as part of Contrast Security’s commercial offerings. Even though it is
9
+ * made available through public repositories, use of this Software is subject to
10
+ * the applicable End User Licensing Agreement found at
11
+ * https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
12
+ * between Contrast Security and the End User. The Software may not be reverse
13
+ * engineered, modified, repackaged, sold, redistributed or otherwise used in a
14
+ * way not consistent with the End User License Agreement.
15
+ */
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;GAaG"}
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@contrast/common",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "Shared constants and utilities for all Contrast Agent modules",
5
5
  "license": "UNLICENSED",
6
6
  "author": "Contrast Security <nodejs@contrastsecurity.com> (https://www.contrastsecurity.com)",
7
7
  "main": "lib/index.js",
8
8
  "types": "lib/index.d.ts",
9
9
  "engines": {
10
- "npm": ">=6.13.7 <7 || >= 8.4.0",
10
+ "npm": ">=6.13.7 <7 || >= 8.3.1",
11
11
  "node": ">= 14.15.0"
12
12
  },
13
13
  "scripts": {
package/src/constants.ts CHANGED
@@ -1,6 +1,22 @@
1
+ /*
2
+ * Copyright: 2022 Contrast Security, Inc
3
+ * Contact: support@contrastsecurity.com
4
+ * License: Commercial
5
+
6
+ * NOTICE: This Software and the patented inventions embodied within may only be
7
+ * used as part of Contrast Security’s commercial offerings. Even though it is
8
+ * made available through public repositories, use of this Software is subject to
9
+ * the applicable End User Licensing Agreement found at
10
+ * https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
11
+ * between Contrast Security and the End User. The Software may not be reverse
12
+ * engineered, modified, repackaged, sold, redistributed or otherwise used in a
13
+ * way not consistent with the End User License Agreement.
14
+ */
15
+
1
16
  export enum Event {
2
17
  ASSESS = 'assess',
3
18
  PROTECT = 'protect',
19
+ SERVER_SETTINGS_UPDATE = 'server-settings-update',
4
20
  }
5
21
 
6
22
  export enum Rule {
@@ -22,3 +38,31 @@ export enum Rule {
22
38
  UNSAFE_FILE_UPLOAD = 'unsafe-file-upload',
23
39
  XXE = 'xxe',
24
40
  }
41
+
42
+ export enum InputType {
43
+ UNDEFINED_TYPE = 'UNDEFINED_TYPE',
44
+ BODY = 'BODY',
45
+ COOKIE_NAME = 'COOKIE_NAME',
46
+ COOKIE_VALUE = 'COOKIE_VALUE',
47
+ HEADER = 'HEADER',
48
+ PARAMETER_NAME = 'PARAMETER_NAME',
49
+ PARAMETER_VALUE = 'PARAMETER_VALUE',
50
+ QUERYSTRING = 'QUERYSTRING',
51
+ URI = 'URI',
52
+ SOCKET = 'SOCKET',
53
+ JSON_VALUE = 'JSON_VALUE',
54
+ JSON_ARRAYED_VALUE = 'JSON_ARRAYED_VALUE',
55
+ MULTIPART_CONTENT_TYPE = 'MULTIPART_CONTENT_TYPE',
56
+ MULTIPART_VALUE = 'MULTIPART_VALUE',
57
+ MULTIPART_FIELD_NAME = 'MULTIPART_FIELD_NAME',
58
+ MULTIPART_NAME = 'MULTIPART_NAME',
59
+ XML_VALUE = 'XML_VALUE',
60
+ DWR_VALUE = 'DWR_VALUE',
61
+ METHOD = 'METHOD',
62
+ REQUEST = 'REQUEST',
63
+ URL_PARAMETER = 'URL_PARAMETER',
64
+ UNKNOWN = 'UNKNOWN',
65
+ }
66
+
67
+ export const BLOCKING_MODES = ['block', 'block_at_perimeter'];
68
+
package/src/index.ts CHANGED
@@ -1,3 +1,18 @@
1
+ /*
2
+ * Copyright: 2022 Contrast Security, Inc
3
+ * Contact: support@contrastsecurity.com
4
+ * License: Commercial
5
+
6
+ * NOTICE: This Software and the patented inventions embodied within may only be
7
+ * used as part of Contrast Security’s commercial offerings. Even though it is
8
+ * made available through public repositories, use of this Software is subject to
9
+ * the applicable End User Licensing Agreement found at
10
+ * https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
11
+ * between Contrast Security and the End User. The Software may not be reverse
12
+ * engineered, modified, repackaged, sold, redistributed or otherwise used in a
13
+ * way not consistent with the End User License Agreement.
14
+ */
15
+
1
16
  export * from './constants';
2
17
  export * from './types';
3
18
 
@@ -10,6 +25,51 @@ export function isString(value: unknown): value is string | String {
10
25
  return typeof value === 'string' || value instanceof String;
11
26
  }
12
27
 
28
+ export function isNonEmptyObject(value: unknown): value is object {
29
+ return !!value && typeof value === 'object' && Object.keys(value).length > 0;
30
+ }
31
+
32
+ /* c8 ignore next 3 */
13
33
  export function encodeString(str: string): string {
14
34
  return Buffer.from(str).toString('base64');
15
35
  }
36
+
37
+ export function simpleTraverse(obj: any, cb: Function) {
38
+ if (typeof obj !== 'object' || obj === null) {
39
+ return;
40
+ }
41
+ const path: any[] = [];
42
+ /* eslint-disable complexity */
43
+ function traverse(obj: any) {
44
+ const isArray = Array.isArray(obj);
45
+ for (const k in obj) {
46
+ if (isArray) {
47
+ const _k = Number(k);
48
+ // if it is an array, store each index in path but don't call the
49
+ // callback on the index itself as they are just numeric strings.
50
+ path.push(_k);
51
+ if (typeof obj[_k] === 'object' && obj[_k] !== null) {
52
+ traverse(obj[_k]);
53
+ } else if (typeof obj[_k] === 'string' && obj[_k]) {
54
+ cb(path, 'Value', obj[_k], obj);
55
+ }
56
+ path.pop();
57
+ } else if (typeof obj[k] === 'object' && obj[k] !== null) {
58
+ cb(path, 'Key', k, obj);
59
+ path.push(k);
60
+ traverse(obj[k]);
61
+ path.pop();
62
+ } else {
63
+ cb(path, 'Key', k, obj);
64
+ // only callback if the value is a non-empty string
65
+ if (typeof obj[k] === 'string' && obj[k]) {
66
+ path.push(k);
67
+ cb(path, 'Value', obj[k], obj);
68
+ path.pop();
69
+ }
70
+ }
71
+ }
72
+ }
73
+
74
+ traverse(obj);
75
+ }
package/src/types.ts ADDED
@@ -0,0 +1,124 @@
1
+ /*
2
+ * Copyright: 2022 Contrast Security, Inc
3
+ * Contact: support@contrastsecurity.com
4
+ * License: Commercial
5
+
6
+ * NOTICE: This Software and the patented inventions embodied within may only be
7
+ * used as part of Contrast Security’s commercial offerings. Even though it is
8
+ * made available through public repositories, use of this Software is subject to
9
+ * the applicable End User Licensing Agreement found at
10
+ * https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
11
+ * between Contrast Security and the End User. The Software may not be reverse
12
+ * engineered, modified, repackaged, sold, redistributed or otherwise used in a
13
+ * way not consistent with the End User License Agreement.
14
+ */
15
+
16
+ import { Event, Rule } from './constants';
17
+ import { EventEmitter } from 'events';
18
+
19
+ export interface RuleConfig {
20
+ mode: 'monitor' | 'block' | 'block_at_perimeter' | 'off';
21
+ }
22
+
23
+ export type SemanticAnalysisRules = Rule.CMD_INJECTION_SEMANTIC_CHAINED_COMMANDS | Rule.CMD_INJECTION_COMMAND_BACKDOORS | Rule.CMD_INJECTION_SEMANTIC_DANGEROUS_PATHS;
24
+
25
+ export interface RulesConfig extends Record<Rule, RuleConfig> { }
26
+
27
+ export interface Result {
28
+ blocked: boolean;
29
+ ruleId: Rule,
30
+ inputType: string, // TODO
31
+ path?: string[],
32
+ key?: string,
33
+ value: string,
34
+ details?: any[], // TODO
35
+ mongoExpansionResult?: boolean
36
+ }
37
+
38
+ export interface SemanticAnalysisResult extends Result {
39
+ findings?: {
40
+ command?: string
41
+ }
42
+ sinkContext?: any
43
+ }
44
+
45
+ export interface HardeningResult extends Result {
46
+ findings?: {
47
+ command?: boolean,
48
+ deserializer?: string
49
+ }
50
+ sinkContext?: any
51
+ }
52
+
53
+ export interface ServerFeaturePreliminaryResult {
54
+ name?: string,
55
+ uuid: string,
56
+ ip?: string
57
+ }
58
+
59
+ export interface ServerFeatureResult extends Result {
60
+ details?: ServerFeaturePreliminaryResult[]
61
+ }
62
+
63
+ export interface ReqData {
64
+ method: string;
65
+ headers: string[];
66
+ uriPath: string;
67
+ queries: string;
68
+ contentType?: string;
69
+ standardUrlParsing: boolean;
70
+ ip: string;
71
+ httpVersion: string,
72
+ }
73
+
74
+ export interface Findings {
75
+ trackRequest: boolean;
76
+ securityException?: [mode: string, ruleId: string];
77
+ bodyType?: 'json' | 'urlencoded';
78
+ resultsMap: Record<Rule, Result[]>;
79
+ semanticResultsMap: Record<Rule, SemanticAnalysisResult[]>;
80
+ serverFeaturesResultsMap: Record<Rule, ServerFeaturePreliminaryResult[]>;
81
+ hardeningResultsMap: Record<Rule, HardeningResult[]>;
82
+ }
83
+
84
+ //
85
+ // this is known as RequestStore even though, in the future, instrumentation
86
+ // will exist for message buses or sources other than HTTP requests. "request"
87
+ // seems generic enough that it's not hard to understand that request can mean
88
+ // an amqp message or other request to perform work that might get user input.
89
+ // additionally, at this time, the only things instrumented are HTTP requests,
90
+ // and other things are only possible extensions to the core facility. it seems
91
+ // reasonable that they will fit into the primary concept that the agent deals
92
+ // with, requests, whether from HTTP or elsewhere.
93
+ //
94
+ export interface RequestStore {
95
+ // TODO: from protect/lib/make-source-context
96
+ protect?: ProtectMessage;
97
+ }
98
+
99
+ export interface ProtectMessage {
100
+ reqData: ReqData;
101
+ block: (mode: string, ruleId: string) => void;
102
+ rules: {
103
+ agentLibRules: RulesConfig;
104
+ agentLibRulesMask: number;
105
+ agentRules: RulesConfig;
106
+ };
107
+ exclusions: any[]; // TODO
108
+ virtualPatches: any[]; // TODO
109
+ findings: Findings;
110
+ parsedBody: any;
111
+ parsedCookies: any;
112
+ parsedParams: any;
113
+ parsedQuery: any;
114
+ }
115
+
116
+ export interface Messages extends EventEmitter {
117
+ addListener(event: Event.PROTECT, listener: (msg: RequestStore) => void): this;
118
+ emit(event: Event.PROTECT, msg: RequestStore): boolean;
119
+ emit(event: Event.SERVER_SETTINGS_UPDATE, msg: { [key: string]: any }): boolean;
120
+ on(event: Event.PROTECT, listener: (msg: RequestStore) => void): this;
121
+ on(event: Event.SERVER_SETTINGS_UPDATE, listener: (msg: { [key: string]: any }) => void): this;
122
+ prependListener(event: Event.PROTECT, listener: (msg: RequestStore) => void): this;
123
+ prependOnceListener(event: Event.PROTECT, listener: (msg: RequestStore) => void): this;
124
+ }
@@ -1,50 +0,0 @@
1
- /// <reference types="node" />
2
- import { Event, Rule } from '../constants';
3
- import { EventEmitter } from 'events';
4
- export interface RuleConfig {
5
- mode: 'monitor' | 'block' | 'block_at_perimeter' | 'off';
6
- }
7
- export interface RulesConfig extends Record<Rule, RuleConfig> {
8
- }
9
- export interface Result {
10
- ruleId: Rule;
11
- inputType: string;
12
- path: string[];
13
- key: string;
14
- value: string;
15
- details?: any[];
16
- }
17
- export interface RequestStore {
18
- protect?: {
19
- reqData: {
20
- method: string;
21
- headers: string[];
22
- uriPath: string;
23
- queries: string;
24
- contentType?: string;
25
- standardUrlParsing: boolean;
26
- };
27
- block: (mode: string, ruleId: string) => void;
28
- rules: {
29
- agentLibRules: RulesConfig;
30
- agentLibRulesMask: number;
31
- agentRules: RulesConfig;
32
- };
33
- exclusions: any[];
34
- virtualPatches: any[];
35
- findings: {
36
- trackRequest: boolean;
37
- securityException?: [mode: string, ruleId: string];
38
- bodyType?: 'json' | 'urlencoded';
39
- resultsMap: Record<Rule, Result[]>;
40
- };
41
- };
42
- }
43
- export interface Messages extends EventEmitter {
44
- addListener(event: Event.PROTECT, listener: (msg: RequestStore) => void): this;
45
- emit(event: Event.PROTECT, msg: RequestStore): boolean;
46
- on(event: Event.PROTECT, listener: (msg: RequestStore) => void): this;
47
- once(event: Event.PROTECT, listener: (msg: RequestStore) => void): this;
48
- prependListener(event: Event.PROTECT, listener: (msg: RequestStore) => void): this;
49
- prependOnceListener(event: Event.PROTECT, listener: (msg: RequestStore) => void): this;
50
- }
@@ -1,3 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":""}
@@ -1,64 +0,0 @@
1
- import { Event, Rule } from '../constants';
2
- import { EventEmitter } from 'events';
3
-
4
- export interface RuleConfig {
5
- mode: 'monitor' | 'block' | 'block_at_perimeter' | 'off';
6
- }
7
-
8
- export interface RulesConfig extends Record<Rule, RuleConfig> {}
9
-
10
- export interface Result {
11
- ruleId: Rule,
12
- inputType: string, // TODO
13
- path: string[],
14
- key: string,
15
- value: string,
16
- details?: any[],
17
- }
18
-
19
- //
20
- // this is known as RequestStore even though, in the future, instrumentation
21
- // will exist for message buses or sources other than HTTP requests. "request"
22
- // seems generic enough that it's not hard to understand that request can mean
23
- // an amqp message or other request to perform work that might get user input.
24
- // additionally, at this time, the only things instrumented are HTTP requests,
25
- // and other things are only possible extensions to the core facility. it seems
26
- // reasonable that they will fit into the primary concept that the agent deals
27
- // with, requests, whether from HTTP or elsewhere.
28
- //
29
- export interface RequestStore {
30
- // TODO: from protect/lib/make-source-context
31
- protect?: {
32
- reqData: {
33
- method: string;
34
- headers: string[];
35
- uriPath: string;
36
- queries: string;
37
- contentType?: string;
38
- standardUrlParsing: boolean;
39
- };
40
- block: (mode: string, ruleId: string) => void;
41
- rules: {
42
- agentLibRules: RulesConfig;
43
- agentLibRulesMask: number;
44
- agentRules: RulesConfig;
45
- };
46
- exclusions: any[]; // TODO
47
- virtualPatches: any[]; // TODO
48
- findings: {
49
- trackRequest: boolean;
50
- securityException?: [mode: string, ruleId: string];
51
- bodyType?: 'json' | 'urlencoded';
52
- resultsMap: Record<Rule, Result[]>
53
- };
54
- };
55
- }
56
-
57
- export interface Messages extends EventEmitter {
58
- addListener(event: Event.PROTECT, listener: (msg: RequestStore) => void): this;
59
- emit(event: Event.PROTECT, msg: RequestStore): boolean;
60
- on(event: Event.PROTECT, listener: (msg: RequestStore) => void): this;
61
- once(event: Event.PROTECT, listener: (msg: RequestStore) => void): this;
62
- prependListener(event: Event.PROTECT, listener: (msg: RequestStore) => void): this;
63
- prependOnceListener(event: Event.PROTECT, listener: (msg: RequestStore) => void): this;
64
- }