perimeterx-js-core 0.7.0 → 0.8.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.
Files changed (216) hide show
  1. package/lib/action/utils.js +1 -1
  2. package/lib/activities/HttpActivityClient.d.ts +5 -5
  3. package/lib/activities/HttpActivityClient.js +28 -39
  4. package/lib/activities/HttpBatchedActivityClient.d.ts +2 -2
  5. package/lib/activities/HttpBatchedActivityClient.js +19 -32
  6. package/lib/activities/model/AsyncActivity.d.ts +14 -0
  7. package/lib/activities/model/{ActivityDetails.d.ts → AsyncActivityDetails.d.ts} +6 -23
  8. package/lib/activities/model/CommonActivityDetails.d.ts +40 -0
  9. package/lib/activities/model/CommonActivityDetails.js +1 -0
  10. package/lib/activities/model/HeaderEntry.d.ts +4 -0
  11. package/lib/activities/model/HeaderEntry.js +1 -0
  12. package/lib/activities/model/index.d.ts +4 -2
  13. package/lib/activities/model/index.js +4 -2
  14. package/lib/activities/utils.d.ts +6071 -9
  15. package/lib/activities/utils.js +111 -35
  16. package/lib/additional_activity_handler/AdditionalActivityHandler.d.ts +1 -1
  17. package/lib/additional_activity_handler/AdditionalActivityHandlerUtils.js +4 -12
  18. package/lib/config/ConfigurationParams.d.ts +2 -1
  19. package/lib/config/DefaultConfigurations.js +1 -0
  20. package/lib/config/IConfiguration.d.ts +5 -1
  21. package/lib/config/StaticConfigurationBase.d.ts +2 -1
  22. package/lib/config/StaticConfigurationBase.js +6 -1
  23. package/lib/context/DefaultContext.d.ts +1 -1
  24. package/lib/context/DefaultContext.js +40 -8
  25. package/lib/context/interfaces/RiskApiData.d.ts +4 -0
  26. package/lib/cors/DefaultCors.js +21 -31
  27. package/lib/custom_parameters/CustomParametersUtils.js +3 -12
  28. package/lib/enforcer/EnforcerBase.d.ts +2 -1
  29. package/lib/enforcer/EnforcerBase.js +56 -57
  30. package/lib/enforcer/options/EnforcerBaseOptions.d.ts +2 -0
  31. package/lib/graphql/DefaultGraphQLParser.js +30 -39
  32. package/lib/http/index.d.ts +0 -1
  33. package/lib/http/index.js +0 -1
  34. package/lib/http/interfaces/IBody.d.ts +3 -2
  35. package/lib/http/interfaces/IIncomingRequest.d.ts +4 -0
  36. package/lib/http/interfaces/IURL.d.ts +3 -0
  37. package/lib/http/interfaces/IURLSearchParams.d.ts +9 -0
  38. package/lib/http/interfaces/IURLSearchParams.js +1 -0
  39. package/lib/http/interfaces/index.d.ts +1 -0
  40. package/lib/http/interfaces/index.js +1 -0
  41. package/lib/http/{impl → utils}/FormDataImpl.js +4 -4
  42. package/lib/http/{impl → utils}/MinimalResponseImpl.js +3 -0
  43. package/lib/http/utils/MinimalResponseUtils.js +5 -7
  44. package/lib/http/utils/MultipartFormDataUtils.js +4 -5
  45. package/lib/http/{impl → utils}/OutgoingRequestImpl.d.ts +1 -1
  46. package/lib/http/{impl → utils}/OutgoingRequestImpl.js +5 -1
  47. package/lib/http/utils/URLUtils.d.ts +7 -0
  48. package/lib/http/utils/URLUtils.js +62 -0
  49. package/lib/http/utils/UrlImpl.d.ts +18 -0
  50. package/lib/http/utils/UrlImpl.js +54 -0
  51. package/lib/http/utils/UrlSearchParamsImpl.d.ts +19 -0
  52. package/lib/http/utils/UrlSearchParamsImpl.js +116 -0
  53. package/lib/http/utils/index.d.ts +6 -0
  54. package/lib/http/utils/index.js +6 -0
  55. package/lib/{utils → impl}/base64/AtobBase64Utils.d.ts +1 -1
  56. package/lib/{utils → impl}/base64/BufferBase64Utils.d.ts +1 -1
  57. package/lib/{utils → impl}/base64/BufferBase64Utils.js +1 -1
  58. package/lib/{utils → impl}/base64/JSBase64Base64Utils.d.ts +1 -1
  59. package/lib/{utils → impl}/cipher/CryptoCipherUtils.d.ts +1 -1
  60. package/lib/impl/cipher/CryptoCipherUtils.js +18 -0
  61. package/lib/{utils → impl}/cipher/SubtleCryptoCipherUtils.d.ts +1 -2
  62. package/lib/impl/cipher/SubtleCryptoCipherUtils.js +38 -0
  63. package/lib/{utils → impl}/hash/CryptoHashUtils.d.ts +1 -2
  64. package/lib/impl/hash/CryptoHashUtils.js +10 -0
  65. package/lib/{utils → impl}/hash/CryptoJSHashUtils.d.ts +1 -2
  66. package/lib/{utils → impl}/hash/CryptoJSHashUtils.js +1 -1
  67. package/lib/{utils → impl}/hash/SubtleCryptoHashUtils.d.ts +1 -2
  68. package/lib/impl/hash/SubtleCryptoHashUtils.js +21 -0
  69. package/lib/{utils → impl}/hmac/CryptoHmacUtils.d.ts +1 -2
  70. package/lib/{utils → impl}/hmac/CryptoHmacUtils.js +2 -1
  71. package/lib/{utils → impl}/hmac/CryptoJSHmacUtils.d.ts +1 -2
  72. package/lib/{utils → impl}/hmac/CryptoJSHmacUtils.js +1 -1
  73. package/lib/{http/impl → impl/http}/phin/PhinHttpClient.d.ts +1 -1
  74. package/lib/impl/http/phin/PhinHttpClient.js +31 -0
  75. package/lib/{http/impl → impl/http}/phin/PhinIncomingResponse.d.ts +2 -2
  76. package/lib/impl/http/phin/PhinIncomingResponse.js +23 -0
  77. package/lib/{utils → impl}/ip_range_checker/DefaultIpRangeChecker.d.ts +1 -1
  78. package/lib/{utils → impl}/request_id_generator/UuidRequestIdGenerator.d.ts +1 -1
  79. package/lib/{utils → impl}/url_parser/DefaultUrlParser.d.ts +4 -4
  80. package/lib/{utils → impl}/url_parser/DefaultUrlParser.js +9 -2
  81. package/lib/logger/HttpLogServiceClient.d.ts +17 -0
  82. package/lib/logger/HttpLogServiceClient.js +52 -0
  83. package/lib/logger/ILogServiceClient.d.ts +6 -0
  84. package/lib/logger/ILogServiceClient.js +1 -0
  85. package/lib/logger/ILogger.d.ts +6 -0
  86. package/lib/logger/LoggerBase.d.ts +6 -2
  87. package/lib/logger/LoggerBase.js +14 -1
  88. package/lib/logger/constants.d.ts +2 -0
  89. package/lib/logger/constants.js +2 -0
  90. package/lib/logger/index.d.ts +4 -0
  91. package/lib/logger/index.js +3 -0
  92. package/lib/logger/model/EnrichedLogRecord.d.ts +3 -0
  93. package/lib/logger/model/EnrichedLogRecord.js +1 -0
  94. package/lib/logger/model/LogMetadata.d.ts +9 -0
  95. package/lib/logger/model/LogMetadata.js +1 -0
  96. package/lib/logger/model/LogRecord.d.ts +6 -0
  97. package/lib/logger/model/LogRecord.js +1 -0
  98. package/lib/logger/model/index.d.ts +3 -0
  99. package/lib/logger/model/index.js +3 -0
  100. package/lib/phase/flow/EndEnforcerFlow.d.ts +6 -0
  101. package/lib/phase/flow/EndEnforcerFlow.js +10 -0
  102. package/lib/phase/flow/index.d.ts +1 -0
  103. package/lib/phase/flow/index.js +1 -0
  104. package/lib/phase/impl/AdditionalActivityHandlerPhase.js +4 -15
  105. package/lib/phase/impl/CompositePhase.js +9 -19
  106. package/lib/phase/impl/CreateBlockResponsePhase.js +24 -35
  107. package/lib/phase/impl/DecideActionPhase.js +11 -21
  108. package/lib/phase/impl/EnrichContextFromRequestPhase.js +28 -45
  109. package/lib/phase/impl/EnrichContextFromResponsePhase.js +12 -23
  110. package/lib/phase/impl/FilterPhase.js +3 -13
  111. package/lib/phase/impl/FirstPartyPhase.js +21 -32
  112. package/lib/phase/impl/ModifyIncomingRequestPhase.js +4 -14
  113. package/lib/phase/impl/ModifyOutgoingResponsePhase.js +7 -18
  114. package/lib/phase/impl/ParseTokenPhase.js +5 -15
  115. package/lib/phase/impl/PreflightPhase.js +12 -20
  116. package/lib/phase/impl/RiskApiPhase.js +24 -36
  117. package/lib/phase/impl/SendAsyncActivitiesOnRequestPhase.js +8 -20
  118. package/lib/phase/impl/SendAsyncActivitiesOnResponsePhase.js +4 -14
  119. package/lib/phase/impl/SendLogsPhase.d.ts +11 -0
  120. package/lib/phase/impl/SendLogsPhase.js +16 -0
  121. package/lib/phase/impl/TelemetryPhase.js +9 -21
  122. package/lib/phase/impl/index.d.ts +1 -0
  123. package/lib/phase/impl/index.js +1 -0
  124. package/lib/products/account_defender/AccountDefender.js +16 -35
  125. package/lib/products/bot_defender/BotDefender.js +51 -72
  126. package/lib/products/bot_defender/BotDefenderActionData.js +2 -0
  127. package/lib/products/bot_defender/block/DefaultBotDefenderBlocker.d.ts +4 -4
  128. package/lib/products/bot_defender/block/DefaultBotDefenderBlocker.js +5 -3
  129. package/lib/products/bot_defender/block/captcha/CaptchaBlocker.d.ts +7 -4
  130. package/lib/products/bot_defender/block/captcha/CaptchaBlocker.js +12 -8
  131. package/lib/products/bot_defender/block/captcha/HtmlCaptchaBlocker.d.ts +1 -1
  132. package/lib/products/bot_defender/block/captcha/HtmlCaptchaBlocker.js +1 -0
  133. package/lib/products/bot_defender/block/captcha/JsonCaptchaBlocker.d.ts +1 -1
  134. package/lib/products/bot_defender/block/captcha/JsonCaptchaBlocker.js +5 -4
  135. package/lib/products/bot_defender/block/captcha/MobileCaptchaBlocker.d.ts +2 -2
  136. package/lib/products/bot_defender/block/captcha/MobileCaptchaBlocker.js +2 -0
  137. package/lib/products/bot_defender/block/utils.js +1 -2
  138. package/lib/products/bot_defender/filter/DefaultBotDefenderFilter.js +7 -1
  139. package/lib/products/bot_defender/first_party/DefaultBotDefenderFirstParty.js +51 -68
  140. package/lib/products/bot_defender/first_party/constants.d.ts +0 -4
  141. package/lib/products/bot_defender/first_party/constants.js +0 -7
  142. package/lib/products/credential_intelligence/CredentialIntelligence.js +42 -63
  143. package/lib/products/credential_intelligence/endpoint/CredentialEndpoint.js +15 -23
  144. package/lib/products/credential_intelligence/endpoint/CredentialEndpointManager.js +9 -22
  145. package/lib/products/credential_intelligence/endpoint/extractor/BodyCredentialExtractor.d.ts +5 -5
  146. package/lib/products/credential_intelligence/endpoint/extractor/BodyCredentialExtractor.js +22 -33
  147. package/lib/products/credential_intelligence/endpoint/extractor/CustomCredentialExtractor.js +8 -18
  148. package/lib/products/credential_intelligence/endpoint/extractor/HeaderCredentialExtractor.js +2 -0
  149. package/lib/products/credential_intelligence/endpoint/extractor/QueryParamCredentialExtractor.d.ts +1 -1
  150. package/lib/products/credential_intelligence/endpoint/extractor/QueryParamCredentialExtractor.js +11 -16
  151. package/lib/products/credential_intelligence/endpoint/hash_protocol/MultistepHashProtocol.js +11 -21
  152. package/lib/products/credential_intelligence/endpoint/hash_protocol/SingleStepAndMultistepHashProtocol.js +9 -18
  153. package/lib/products/credential_intelligence/endpoint/hash_protocol/SingleStepHashProtocol.js +13 -25
  154. package/lib/products/credential_intelligence/endpoint/login_successful/BodyLoginSuccessfulParser.js +4 -14
  155. package/lib/products/credential_intelligence/endpoint/login_successful/CustomLoginSuccessfulParser.js +10 -20
  156. package/lib/products/credential_intelligence/endpoint/login_successful/HeaderLoginSuccessfulParser.js +7 -16
  157. package/lib/products/credential_intelligence/endpoint/login_successful/LoginSuccessfulParserFactory.js +7 -6
  158. package/lib/products/credential_intelligence/endpoint/login_successful/StatusLoginSuccessfulParser.js +3 -13
  159. package/lib/products/credential_intelligence/endpoint/matcher/ExactPathEndpointMatcher.js +2 -0
  160. package/lib/products/credential_intelligence/endpoint/matcher/RegexPathEndpointMatcher.js +2 -0
  161. package/lib/pxde/DefaultDataEnrichment.js +47 -61
  162. package/lib/pxhd/PXHDUtils.js +2 -2
  163. package/lib/risk_api/client/PostRiskApiClientBase.d.ts +7 -12
  164. package/lib/risk_api/client/PostRiskApiClientBase.js +70 -171
  165. package/lib/risk_api/model/RiskActivity.d.ts +5 -45
  166. package/lib/risk_api/risk_response/IRiskResponse.d.ts +1 -0
  167. package/lib/risk_api/risk_response/RiskResponseBase.d.ts +1 -0
  168. package/lib/risk_api/risk_response/RiskResponseBase.js +20 -33
  169. package/lib/risk_api/risk_response/v2/DefaultRiskResponseV2.d.ts +1 -1
  170. package/lib/risk_api/risk_response/v2/DefaultRiskResponseV2.js +3 -5
  171. package/lib/risk_api/risk_response/v2/RiskResponseV2Payload.d.ts +1 -0
  172. package/lib/risk_api/risk_response/v3/DefaultRiskResponseV3.js +2 -4
  173. package/lib/risk_api/risk_response/v3/RiskResponseV3Payload.d.ts +1 -0
  174. package/lib/risk_token/parser/TokenParserBase.js +25 -36
  175. package/lib/risk_token/token/TokenBase.js +24 -29
  176. package/lib/risk_token/token/v2/DefaultTokenV2.js +34 -49
  177. package/lib/risk_token/token/v3/DefaultTokenV3.js +63 -77
  178. package/lib/telemetry/DefaultTelemetry.js +46 -58
  179. package/lib/utils/base64/index.d.ts +0 -3
  180. package/lib/utils/base64/index.js +0 -3
  181. package/lib/utils/cipher/index.d.ts +1 -3
  182. package/lib/utils/cipher/index.js +1 -2
  183. package/lib/utils/constants.d.ts +2 -1
  184. package/lib/utils/constants.js +2 -1
  185. package/lib/utils/hash/index.d.ts +0 -3
  186. package/lib/utils/hash/index.js +0 -3
  187. package/lib/utils/hmac/index.d.ts +0 -2
  188. package/lib/utils/hmac/index.js +0 -2
  189. package/lib/utils/ip_range_checker/index.d.ts +1 -2
  190. package/lib/utils/ip_range_checker/index.js +1 -1
  191. package/lib/utils/request_id_generator/index.d.ts +1 -2
  192. package/lib/utils/request_id_generator/index.js +1 -1
  193. package/lib/utils/url_parser/index.d.ts +0 -1
  194. package/lib/utils/url_parser/index.js +0 -1
  195. package/lib/utils/utils.d.ts +1 -7
  196. package/lib/utils/utils.js +3 -25
  197. package/package.json +12 -6
  198. package/lib/activities/model/Activity.d.ts +0 -13
  199. package/lib/http/impl/index.d.ts +0 -4
  200. package/lib/http/impl/index.js +0 -4
  201. package/lib/http/impl/phin/PhinHttpClient.js +0 -41
  202. package/lib/http/impl/phin/PhinIncomingResponse.js +0 -38
  203. package/lib/utils/cipher/CryptoCipherUtils.js +0 -28
  204. package/lib/utils/cipher/SubtleCryptoCipherUtils.js +0 -47
  205. package/lib/utils/hash/CryptoHashUtils.js +0 -20
  206. package/lib/utils/hash/SubtleCryptoHashUtils.js +0 -31
  207. /package/lib/activities/model/{Activity.js → AsyncActivity.js} +0 -0
  208. /package/lib/activities/model/{ActivityDetails.js → AsyncActivityDetails.js} +0 -0
  209. /package/lib/http/{impl → utils}/FormDataImpl.d.ts +0 -0
  210. /package/lib/http/{impl → utils}/MinimalResponseImpl.d.ts +0 -0
  211. /package/lib/{utils → impl}/base64/AtobBase64Utils.js +0 -0
  212. /package/lib/{utils → impl}/base64/JSBase64Base64Utils.js +0 -0
  213. /package/lib/{http/impl → impl/http}/phin/index.d.ts +0 -0
  214. /package/lib/{http/impl → impl/http}/phin/index.js +0 -0
  215. /package/lib/{utils → impl}/ip_range_checker/DefaultIpRangeChecker.js +0 -0
  216. /package/lib/{utils → impl}/request_id_generator/UuidRequestIdGenerator.js +0 -0
@@ -1,16 +1,10 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  import { BlockAction } from '../../../blocker';
11
2
  import { Algorithm, isValidUuid } from '../../../utils';
12
3
  import { TokenBase } from '../TokenBase';
13
4
  export class DefaultTokenV2 extends TokenBase {
5
+ base64Utils;
6
+ hmacUtils;
7
+ isHighRisk;
14
8
  constructor(config, cookieString, base64Utils, hmacUtils) {
15
9
  super(config, cookieString);
16
10
  this.base64Utils = base64Utils;
@@ -20,7 +14,7 @@ export class DefaultTokenV2 extends TokenBase {
20
14
  decrypt(context) {
21
15
  try {
22
16
  const payload = this.decode(this.cookieString);
23
- if ((payload === null || payload === void 0 ? void 0 : payload.t) == null || (payload === null || payload === void 0 ? void 0 : payload.h) == null || (payload === null || payload === void 0 ? void 0 : payload.u) == null || (payload === null || payload === void 0 ? void 0 : payload.v) == null) {
17
+ if (payload?.t == null || payload?.h == null || payload?.u == null || payload?.v == null) {
24
18
  return null;
25
19
  }
26
20
  return payload;
@@ -30,32 +24,30 @@ export class DefaultTokenV2 extends TokenBase {
30
24
  }
31
25
  return null;
32
26
  }
33
- validate(context) {
34
- return __awaiter(this, void 0, void 0, function* () {
35
- if (typeof this.payload.t !== 'number' ||
36
- !isValidUuid(this.payload.v) ||
37
- !isValidUuid(this.payload.u) ||
38
- !this.payload.h) {
39
- return false;
40
- }
41
- try {
42
- const signingFields = this.getSigningFields(context);
43
- const passHmac = yield this.calculateHmac(this.getHashParam('0', signingFields));
44
- if (passHmac === this.payload.h) {
45
- this.isHighRisk = false;
46
- return true;
47
- }
48
- const blockHmac = yield this.calculateHmac(this.getHashParam('1', signingFields));
49
- if (blockHmac === this.payload.h) {
50
- this.isHighRisk = true;
51
- return true;
52
- }
27
+ async validate(context) {
28
+ if (typeof this.payload.t !== 'number' ||
29
+ !isValidUuid(this.payload.v) ||
30
+ !isValidUuid(this.payload.u) ||
31
+ !this.payload.h) {
32
+ return false;
33
+ }
34
+ try {
35
+ const signingFields = this.getSigningFields(context);
36
+ const passHmac = await this.calculateHmac(this.getHashParam('0', signingFields));
37
+ if (passHmac === this.payload.h) {
38
+ this.isHighRisk = false;
39
+ return true;
53
40
  }
54
- catch (e) {
55
- this.logger.debug(`cookie v2 validation caught error: ${e}`);
41
+ const blockHmac = await this.calculateHmac(this.getHashParam('1', signingFields));
42
+ if (blockHmac === this.payload.h) {
43
+ this.isHighRisk = true;
44
+ return true;
56
45
  }
57
- return false;
58
- });
46
+ }
47
+ catch (e) {
48
+ this.logger.debug(`cookie v2 validation caught error: ${e}`);
49
+ }
50
+ return false;
59
51
  }
60
52
  decode(cookieString) {
61
53
  const decodedCookie = this.base64Utils.base64Decode(cookieString);
@@ -71,34 +63,27 @@ export class DefaultTokenV2 extends TokenBase {
71
63
  }
72
64
  return hashParam;
73
65
  }
74
- calculateHmac(param) {
75
- return __awaiter(this, void 0, void 0, function* () {
76
- const payload = `${this.payload.t}${this.payload.u}${this.payload.v}${param}`;
77
- return this.hmacUtils.createHmac(Algorithm.SHA256, payload, this.cookieSecret);
78
- });
66
+ async calculateHmac(param) {
67
+ const payload = `${this.payload.t}${this.payload.u}${this.payload.v}${param}`;
68
+ return this.hmacUtils.createHmac(Algorithm.SHA256, payload, this.cookieSecret);
79
69
  }
80
70
  isExpired() {
81
- var _a;
82
- return Date.now() > ((_a = this.payload) === null || _a === void 0 ? void 0 : _a.t);
71
+ return Date.now() > this.payload?.t;
83
72
  }
84
73
  isHighScore() {
85
74
  return this.isHighRisk;
86
75
  }
87
76
  get hmac() {
88
- var _a;
89
- return (_a = this.payload) === null || _a === void 0 ? void 0 : _a.h;
77
+ return this.payload?.h;
90
78
  }
91
79
  get timestamp() {
92
- var _a;
93
- return (_a = this.payload) === null || _a === void 0 ? void 0 : _a.t;
80
+ return this.payload?.t;
94
81
  }
95
82
  get uuid() {
96
- var _a;
97
- return (_a = this.payload) === null || _a === void 0 ? void 0 : _a.u;
83
+ return this.payload?.u;
98
84
  }
99
85
  get vid() {
100
- var _a;
101
- return (_a = this.payload) === null || _a === void 0 ? void 0 : _a.v;
86
+ return this.payload?.v;
102
87
  }
103
88
  get score() {
104
89
  if (this.isValid()) {
@@ -1,17 +1,14 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  import { Algorithm } from '../../../utils';
11
2
  import { COOKIE_SPLIT_DELIMITER, COOKIE_V3_HMAC_INDEX, COOKIE_V3_ITERATIONS_INDEX, COOKIE_V3_MAXIMUM_SALT_LENGTH, COOKIE_V3_PARTS_COUNT, COOKIE_V3_PAYLOAD_INDEX, COOKIE_V3_SALT_INDEX, } from '../../constants';
12
3
  import { TokenSignField } from './TokenSignField';
13
4
  import { TokenBase } from '../TokenBase';
14
5
  export class DefaultTokenV3 extends TokenBase {
6
+ maxIterations;
7
+ minIterations;
8
+ blockingScore;
9
+ cipherUtils;
10
+ hmacUtils;
11
+ hash;
15
12
  constructor(config, cookieString, cipherUtils, hmacUtils) {
16
13
  super(config, cookieString);
17
14
  this.maxIterations = config.riskCookieMaxIterations;
@@ -21,72 +18,65 @@ export class DefaultTokenV3 extends TokenBase {
21
18
  this.cipherUtils = cipherUtils;
22
19
  this.hmacUtils = hmacUtils;
23
20
  }
24
- decrypt(context) {
25
- return __awaiter(this, void 0, void 0, function* () {
26
- try {
27
- const data = this.cookieString.split(COOKIE_SPLIT_DELIMITER);
28
- if (data.length !== COOKIE_V3_PARTS_COUNT) {
29
- return null;
30
- }
31
- this.hash = data[COOKIE_V3_HMAC_INDEX];
32
- const salt = data[COOKIE_V3_SALT_INDEX];
33
- const iterations = parseInt
34
- ? parseInt(data[COOKIE_V3_ITERATIONS_INDEX])
35
- : Number(data[COOKIE_V3_ITERATIONS_INDEX]);
36
- const encryptedCookie = data[COOKIE_V3_PAYLOAD_INDEX];
37
- if (!iterations || iterations > this.maxIterations || iterations < this.minIterations) {
38
- return null;
39
- }
40
- if (!salt || typeof salt !== 'string' || salt.length > COOKIE_V3_MAXIMUM_SALT_LENGTH) {
41
- return null;
42
- }
43
- if (!encryptedCookie || typeof encryptedCookie !== 'string') {
44
- return null;
45
- }
46
- return yield this.decryptPayload(encryptedCookie, salt, iterations);
21
+ async decrypt(context) {
22
+ try {
23
+ const data = this.cookieString.split(COOKIE_SPLIT_DELIMITER);
24
+ if (data.length !== COOKIE_V3_PARTS_COUNT) {
25
+ return null;
47
26
  }
48
- catch (e) {
49
- this.logger.debug(`cookie v3 decryption failed: ${e}`);
27
+ this.hash = data[COOKIE_V3_HMAC_INDEX];
28
+ const salt = data[COOKIE_V3_SALT_INDEX];
29
+ const iterations = parseInt
30
+ ? parseInt(data[COOKIE_V3_ITERATIONS_INDEX])
31
+ : Number(data[COOKIE_V3_ITERATIONS_INDEX]);
32
+ const encryptedCookie = data[COOKIE_V3_PAYLOAD_INDEX];
33
+ if (!iterations || iterations > this.maxIterations || iterations < this.minIterations) {
34
+ return null;
50
35
  }
51
- return null;
52
- });
53
- }
54
- decryptPayload(encryptedCookie, salt, iterations) {
55
- return __awaiter(this, void 0, void 0, function* () {
56
- try {
57
- const decryptedCookie = yield this.cipherUtils.pbkdf2Decrypt(this.cookieSecret, encryptedCookie, iterations, salt);
58
- if (!decryptedCookie) {
59
- return null;
60
- }
61
- return JSON.parse(decryptedCookie);
36
+ if (!salt || typeof salt !== 'string' || salt.length > COOKIE_V3_MAXIMUM_SALT_LENGTH) {
37
+ return null;
62
38
  }
63
- catch (e) {
64
- this.logger.debug(`error decrypting cookie v3: ${e}`);
39
+ if (!encryptedCookie || typeof encryptedCookie !== 'string') {
65
40
  return null;
66
41
  }
67
- });
42
+ return await this.decryptPayload(encryptedCookie, salt, iterations);
43
+ }
44
+ catch (e) {
45
+ this.logger.debug(`cookie v3 decryption failed: ${e}`);
46
+ }
47
+ return null;
68
48
  }
69
- validate(context) {
70
- return __awaiter(this, void 0, void 0, function* () {
71
- try {
72
- const signedFields = this.getSignedWithFields(context);
73
- const hmacStrBase = this.cookieString.substring(this.cookieString.indexOf(COOKIE_SPLIT_DELIMITER) + 1);
74
- const payload = `${hmacStrBase}${signedFields.join('')}`;
75
- const hash = this.hmacUtils.createHmac(Algorithm.SHA256, payload, this.cookieSecret);
76
- return hash === this.hmac;
77
- }
78
- catch (e) {
79
- this.logger.debug(`error validating cookie v3: ${e}`);
80
- return false;
49
+ async decryptPayload(encryptedCookie, salt, iterations) {
50
+ try {
51
+ const decryptedCookie = await this.cipherUtils.pbkdf2Decrypt(this.cookieSecret, encryptedCookie, iterations, salt);
52
+ if (!decryptedCookie) {
53
+ return null;
81
54
  }
82
- });
55
+ return JSON.parse(decryptedCookie);
56
+ }
57
+ catch (e) {
58
+ this.logger.debug(`error decrypting cookie v3: ${e}`);
59
+ return null;
60
+ }
61
+ }
62
+ async validate(context) {
63
+ try {
64
+ const signedFields = this.getSignedWithFields(context);
65
+ const hmacStrBase = this.cookieString.substring(this.cookieString.indexOf(COOKIE_SPLIT_DELIMITER) + 1);
66
+ const payload = `${hmacStrBase}${signedFields.join('')}`;
67
+ const hash = this.hmacUtils.createHmac(Algorithm.SHA256, payload, this.cookieSecret);
68
+ return hash === this.hmac;
69
+ }
70
+ catch (e) {
71
+ this.logger.debug(`error validating cookie v3: ${e}`);
72
+ return false;
73
+ }
83
74
  }
84
75
  getSignedWithFields(context) {
85
- var _a;
86
76
  if (context.isMobile) {
87
77
  return [];
88
78
  }
89
- else if ((_a = this.payload) === null || _a === void 0 ? void 0 : _a.x) {
79
+ else if (this.payload?.x) {
90
80
  return this.payload.x.split('').map((char) => this.getSignedWithField(char, context));
91
81
  }
92
82
  else {
@@ -104,32 +94,28 @@ export class DefaultTokenV3 extends TokenBase {
104
94
  }
105
95
  }
106
96
  isExpired() {
107
- var _a;
108
- return Date.now() > ((_a = this.payload) === null || _a === void 0 ? void 0 : _a.t);
97
+ return Date.now() > this.payload?.t;
109
98
  }
110
99
  isHighScore() {
111
- var _a;
112
- return ((_a = this.payload) === null || _a === void 0 ? void 0 : _a.s) >= this.blockingScore;
100
+ if (!this.payload) {
101
+ return undefined;
102
+ }
103
+ return this.payload.s >= this.blockingScore;
113
104
  }
114
105
  get timestamp() {
115
- var _a;
116
- return (_a = this.payload) === null || _a === void 0 ? void 0 : _a.t;
106
+ return this.payload?.t;
117
107
  }
118
108
  get uuid() {
119
- var _a;
120
- return (_a = this.payload) === null || _a === void 0 ? void 0 : _a.u;
109
+ return this.payload?.u;
121
110
  }
122
111
  get vid() {
123
- var _a;
124
- return (_a = this.payload) === null || _a === void 0 ? void 0 : _a.v;
112
+ return this.payload?.v;
125
113
  }
126
114
  get score() {
127
- var _a;
128
- return (_a = this.payload) === null || _a === void 0 ? void 0 : _a.s;
115
+ return this.payload?.s;
129
116
  }
130
117
  get action() {
131
- var _a;
132
- return (_a = this.payload) === null || _a === void 0 ? void 0 : _a.a;
118
+ return this.payload?.a;
133
119
  }
134
120
  get hmac() {
135
121
  return this.hash;
@@ -1,71 +1,60 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  import { AUTHORIZATION_HEADER_NAME, CONTENT_TYPE_HEADER_NAME, ContentType, HttpMethod, OutgoingRequestImpl, } from '../http';
11
2
  import { ActivityType } from '../activities';
12
3
  import { getAuthorizationHeader, Algorithm, removeSensitiveFields, transferExistingProperties, } from '../utils';
13
4
  import { TELEMETRY_ENDPOINT, TELEMETRY_HEADER_DELIMITER, TELEMETRY_HEADER_HMAC_INDEX, TELEMETRY_HEADER_NAME, TELEMETRY_HEADER_PART_COUNT, TELEMETRY_HEADER_TIMESTAMP_INDEX, TELEMETRY_UPDATE_REASON, } from './constants';
14
5
  export class DefaultTelemetry {
6
+ config;
7
+ httpClient;
8
+ base64Utils;
9
+ hmacUtils;
15
10
  constructor(config, httpClient, base64Utils, hmacUtils) {
16
11
  this.config = config;
17
12
  this.httpClient = httpClient;
18
13
  this.base64Utils = base64Utils;
19
14
  this.hmacUtils = hmacUtils;
20
15
  }
21
- isValidTelemetryRequest(context) {
22
- return __awaiter(this, void 0, void 0, function* () {
23
- try {
24
- const telemetryHeader = this.getTelemetryHeader(context);
25
- return !!telemetryHeader && (yield this.isTelemetryHeaderValid(telemetryHeader));
26
- }
27
- catch (e) {
28
- this.config.logger.debug(`error validating telemetry - ${e}`);
29
- return false;
30
- }
31
- });
16
+ async isValidTelemetryRequest(context) {
17
+ try {
18
+ const telemetryHeader = this.getTelemetryHeader(context);
19
+ return !!telemetryHeader && (await this.isTelemetryHeaderValid(telemetryHeader));
20
+ }
21
+ catch (e) {
22
+ this.config.logger.debug(`error validating telemetry - ${e}`);
23
+ return false;
24
+ }
32
25
  }
33
- sendTelemetry(context) {
34
- return __awaiter(this, void 0, void 0, function* () {
35
- try {
36
- yield this.sendTelemetryActivity(context);
37
- }
38
- catch (e) {
39
- this.config.logger.debug(`error sending telemetry - ${e}`);
40
- }
41
- });
26
+ async sendTelemetry(context) {
27
+ try {
28
+ await this.sendTelemetryActivity(context);
29
+ }
30
+ catch (e) {
31
+ this.config.logger.debug(`error sending telemetry - ${e}`);
32
+ }
42
33
  }
43
34
  getTelemetryHeader(context) {
44
35
  return context.requestData.request.headers.get(TELEMETRY_HEADER_NAME) || '';
45
36
  }
46
- isTelemetryHeaderValid(headerValue) {
47
- return __awaiter(this, void 0, void 0, function* () {
48
- this.config.logger.debug('received command to send enforcer telemetry');
49
- const decodedValue = this.base64Utils.base64Decode(headerValue);
50
- const splitValue = decodedValue.split(TELEMETRY_HEADER_DELIMITER);
51
- if (splitValue.length !== TELEMETRY_HEADER_PART_COUNT) {
52
- this.config.logger.debug(`malformed ${TELEMETRY_HEADER_NAME} header: ${decodedValue}`);
53
- return false;
54
- }
55
- const timestamp = splitValue[TELEMETRY_HEADER_TIMESTAMP_INDEX];
56
- const givenHmac = splitValue[TELEMETRY_HEADER_HMAC_INDEX];
57
- if (!this.isTelemetryHmacValid(givenHmac, timestamp)) {
58
- this.config.logger.debug(`${TELEMETRY_HEADER_NAME} hmac validation failed. original hmac: ${givenHmac}, timestamp: ${timestamp}.`);
59
- return false;
60
- }
61
- const curUnixTime = +new Date();
62
- const timestampNumber = parseInt ? parseInt(timestamp) : Number(timestamp);
63
- if (isNaN(timestampNumber) || timestampNumber < curUnixTime) {
64
- this.config.logger.debug(`${TELEMETRY_HEADER_NAME} timestamp expired: ${timestamp} < ${curUnixTime}`);
65
- return false;
66
- }
67
- return true;
68
- });
37
+ async isTelemetryHeaderValid(headerValue) {
38
+ this.config.logger.debug('received command to send enforcer telemetry');
39
+ const decodedValue = this.base64Utils.base64Decode(headerValue);
40
+ const splitValue = decodedValue.split(TELEMETRY_HEADER_DELIMITER);
41
+ if (splitValue.length !== TELEMETRY_HEADER_PART_COUNT) {
42
+ this.config.logger.debug(`malformed ${TELEMETRY_HEADER_NAME} header: ${decodedValue}`);
43
+ return false;
44
+ }
45
+ const timestamp = splitValue[TELEMETRY_HEADER_TIMESTAMP_INDEX];
46
+ const givenHmac = splitValue[TELEMETRY_HEADER_HMAC_INDEX];
47
+ if (!this.isTelemetryHmacValid(givenHmac, timestamp)) {
48
+ this.config.logger.debug(`${TELEMETRY_HEADER_NAME} hmac validation failed. original hmac: ${givenHmac}, timestamp: ${timestamp}.`);
49
+ return false;
50
+ }
51
+ const curUnixTime = +new Date();
52
+ const timestampNumber = parseInt ? parseInt(timestamp) : Number(timestamp);
53
+ if (isNaN(timestampNumber) || timestampNumber < curUnixTime) {
54
+ this.config.logger.debug(`${TELEMETRY_HEADER_NAME} timestamp expired: ${timestamp} < ${curUnixTime}`);
55
+ return false;
56
+ }
57
+ return true;
69
58
  }
70
59
  isTelemetryHmacValid(givenHmac, timestamp) {
71
60
  try {
@@ -76,12 +65,10 @@ export class DefaultTelemetry {
76
65
  return false;
77
66
  }
78
67
  }
79
- sendTelemetryActivity(context) {
80
- return __awaiter(this, void 0, void 0, function* () {
81
- const telemetryRequest = this.createTelemetryRequest(context);
82
- this.config.logger.debug(`sending telemetry to ${telemetryRequest.url}`);
83
- yield this.httpClient.send(telemetryRequest);
84
- });
68
+ async sendTelemetryActivity(context) {
69
+ const telemetryRequest = this.createTelemetryRequest(context);
70
+ this.config.logger.debug(`sending telemetry to ${telemetryRequest.url}`);
71
+ await this.httpClient.send(telemetryRequest);
85
72
  }
86
73
  createTelemetryRequest(context) {
87
74
  const url = `${this.config.backendScoreApiUrl}${TELEMETRY_ENDPOINT}`;
@@ -97,6 +84,7 @@ export class DefaultTelemetry {
97
84
  const SENSITIVE_CONFIG_FIELDS = [
98
85
  'px_auth_token',
99
86
  'px_cookie_secret',
87
+ 'px_logger_auth_token',
100
88
  ];
101
89
  let config = this.config.toParams();
102
90
  config = removeSensitiveFields(config, SENSITIVE_CONFIG_FIELDS);
@@ -1,4 +1 @@
1
1
  export * from './IBase64Utils';
2
- export * from './JSBase64Base64Utils';
3
- export * from './BufferBase64Utils';
4
- export * from './AtobBase64Utils';
@@ -1,4 +1 @@
1
1
  export * from './IBase64Utils';
2
- export * from './JSBase64Base64Utils';
3
- export * from './BufferBase64Utils';
4
- export * from './AtobBase64Utils';
@@ -1,3 +1 @@
1
- export { ICipherUtils, Pbkdf2DecryptOptions } from './ICipherUtils';
2
- export * from './SubtleCryptoCipherUtils';
3
- export * from './CryptoCipherUtils';
1
+ export * from './ICipherUtils';
@@ -1,2 +1 @@
1
- export * from './SubtleCryptoCipherUtils';
2
- export * from './CryptoCipherUtils';
1
+ export * from './ICipherUtils';
@@ -8,4 +8,5 @@ export declare const X_PX_AUTHORIZATION_HEADER_NAME = "x-px-authorization";
8
8
  export declare const X_PX_ORIGINAL_TOKEN_HEADER_NAME = "x-px-original-token";
9
9
  export declare const X_PX_BYPASS_REASON_HEADER_NAME = "x-px-bypass-reason";
10
10
  export declare const EMAIL_ADDRESS_REGEX: RegExp;
11
- export declare const CORE_MODULE_VERSION = "JS Core 0.7.0";
11
+ export declare const URL_REGEX: RegExp;
12
+ export declare const CORE_MODULE_VERSION = "JS Core 0.8.0";
@@ -8,4 +8,5 @@ export const X_PX_AUTHORIZATION_HEADER_NAME = 'x-px-authorization';
8
8
  export const X_PX_ORIGINAL_TOKEN_HEADER_NAME = 'x-px-original-token';
9
9
  export const X_PX_BYPASS_REASON_HEADER_NAME = 'x-px-bypass-reason';
10
10
  export const EMAIL_ADDRESS_REGEX = /^[a-zA-Z0-9_+&*-]+(?:\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,7}$/;
11
- export const CORE_MODULE_VERSION = 'JS Core 0.7.0';
11
+ export const URL_REGEX = /^(https?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)([\/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/;
12
+ export const CORE_MODULE_VERSION = 'JS Core 0.8.0';
@@ -1,4 +1 @@
1
1
  export * from './IHashUtils';
2
- export * from './SubtleCryptoHashUtils';
3
- export * from './CryptoHashUtils';
4
- export * from './CryptoJSHashUtils';
@@ -1,4 +1 @@
1
1
  export * from './IHashUtils';
2
- export * from './SubtleCryptoHashUtils';
3
- export * from './CryptoHashUtils';
4
- export * from './CryptoJSHashUtils';
@@ -1,3 +1 @@
1
1
  export * from './IHmacUtils';
2
- export * from './CryptoHmacUtils';
3
- export * from './CryptoJSHmacUtils';
@@ -1,3 +1 @@
1
1
  export * from './IHmacUtils';
2
- export * from './CryptoHmacUtils';
3
- export * from './CryptoJSHmacUtils';
@@ -1,2 +1 @@
1
- export { IIpRangeChecker } from './IIpRangeChecker';
2
- export { DefaultIpRangeChecker } from './DefaultIpRangeChecker';
1
+ export * from './IIpRangeChecker';
@@ -1 +1 @@
1
- export { DefaultIpRangeChecker } from './DefaultIpRangeChecker';
1
+ export * from './IIpRangeChecker';
@@ -1,2 +1 @@
1
- export { IRequestIdGenerator } from './IRequestIdGenerator';
2
- export { UuidRequestIdGenerator } from './UuidRequestIdGenerator';
1
+ export * from './IRequestIdGenerator';
@@ -1 +1 @@
1
- export { UuidRequestIdGenerator } from './UuidRequestIdGenerator';
1
+ export * from './IRequestIdGenerator';
@@ -1,2 +1 @@
1
1
  export * from './IURLParser';
2
- export * from './DefaultUrlParser';
@@ -1,2 +1 @@
1
1
  export * from './IURLParser';
2
- export * from './DefaultUrlParser';
@@ -1,4 +1,4 @@
1
- import { ReadonlyHeaders, IURL } from '../http';
1
+ import { ReadonlyHeaders } from '../http';
2
2
  export declare const isValidEnumValue: <T>(options: Record<string, T>, selection: T) => boolean;
3
3
  export declare const isValidUuid: (uuid: string) => boolean;
4
4
  export declare const isEmailAddress: (string: string) => boolean;
@@ -6,12 +6,6 @@ export declare const getScoreApiDomain: (appId: string) => string;
6
6
  export declare const getCollectorDomain: (appId: string) => string;
7
7
  export declare const getAuthorizationHeader: (authToken: string) => string;
8
8
  export declare const getExtension: (route: string) => string;
9
- /**
10
- *
11
- * @param rawUrl
12
- * @deprecated use {@link IURLParser} instead.
13
- */
14
- export declare const getDecodedUrl: (rawUrl: string) => IURL;
15
9
  export declare const removeSensitiveFields: <T extends Record<string, any>>(object: T, sensitiveFields: (keyof T)[]) => T;
16
10
  export declare const removeSensitiveHeaders: (headers: ReadonlyHeaders, sensitiveHeaderNames: string[]) => Record<string, string[]>;
17
11
  export declare const isRouteInPatterns: (route: string, patterns: Array<string | RegExp>) => boolean;
@@ -1,12 +1,3 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  import { EnforcerTimeoutError } from './error';
11
2
  import { toMutableHeaders } from '../http';
12
3
  import { EMAIL_ADDRESS_REGEX } from './constants';
@@ -39,19 +30,6 @@ export const getExtension = (route) => {
39
30
  }
40
31
  return endOfPath.substring(extensionIndex);
41
32
  };
42
- /**
43
- *
44
- * @param rawUrl
45
- * @deprecated use {@link IURLParser} instead.
46
- */
47
- export const getDecodedUrl = (rawUrl) => {
48
- try {
49
- return new URL(decodeURIComponent(rawUrl));
50
- }
51
- catch (e) {
52
- return new URL(rawUrl);
53
- }
54
- };
55
33
  export const removeSensitiveFields = (object, sensitiveFields) => {
56
34
  const newObj = Object.assign({}, object);
57
35
  sensitiveFields.forEach((fieldName) => {
@@ -95,7 +73,7 @@ export const transferExistingProperties = (fromObj, toObj, propertyMappings) =>
95
73
  }
96
74
  });
97
75
  };
98
- export const rejectOnTimeout = (promise, ms) => __awaiter(void 0, void 0, void 0, function* () {
76
+ export const rejectOnTimeout = async (promise, ms) => {
99
77
  let id;
100
78
  const timeout = new Promise((resolve, reject) => {
101
79
  id = setTimeout(() => {
@@ -103,10 +81,10 @@ export const rejectOnTimeout = (promise, ms) => __awaiter(void 0, void 0, void 0
103
81
  reject(new EnforcerTimeoutError(ms));
104
82
  }, ms);
105
83
  });
106
- const resolvedPromise = yield Promise.race([promise, timeout]);
84
+ const resolvedPromise = await Promise.race([promise, timeout]);
107
85
  clearTimeout(id);
108
86
  return resolvedPromise;
109
- });
87
+ };
110
88
  export const getPropertyFromObject = (object, ...keys) => {
111
89
  let value = object;
112
90
  for (const key of keys) {