@sparkleideas/security 3.0.0-alpha.22 → 3.0.0-alpha.29
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/dist/CVE-REMEDIATION.d.ts +86 -0
- package/dist/CVE-REMEDIATION.d.ts.map +1 -0
- package/dist/CVE-REMEDIATION.js +221 -0
- package/dist/CVE-REMEDIATION.js.map +1 -0
- package/dist/application/index.d.ts +7 -0
- package/dist/application/index.d.ts.map +1 -0
- package/dist/application/index.js +7 -0
- package/dist/application/index.js.map +1 -0
- package/dist/application/services/security-application-service.d.ts +71 -0
- package/dist/application/services/security-application-service.d.ts.map +1 -0
- package/dist/application/services/security-application-service.js +153 -0
- package/dist/application/services/security-application-service.js.map +1 -0
- package/dist/credential-generator.d.ts +176 -0
- package/dist/credential-generator.d.ts.map +1 -0
- package/dist/credential-generator.js +272 -0
- package/dist/credential-generator.js.map +1 -0
- package/dist/domain/entities/security-context.d.ts +68 -0
- package/dist/domain/entities/security-context.d.ts.map +1 -0
- package/dist/domain/entities/security-context.js +132 -0
- package/dist/domain/entities/security-context.js.map +1 -0
- package/dist/domain/index.d.ts +8 -0
- package/dist/domain/index.d.ts.map +1 -0
- package/dist/domain/index.js +8 -0
- package/dist/domain/index.js.map +1 -0
- package/dist/domain/services/security-domain-service.d.ts +71 -0
- package/dist/domain/services/security-domain-service.d.ts.map +1 -0
- package/dist/domain/services/security-domain-service.js +237 -0
- package/dist/domain/services/security-domain-service.js.map +1 -0
- package/dist/index.d.ts +119 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +145 -0
- package/dist/index.js.map +1 -0
- package/dist/input-validator.d.ts +338 -0
- package/dist/input-validator.d.ts.map +1 -0
- package/dist/input-validator.js +393 -0
- package/dist/input-validator.js.map +1 -0
- package/dist/password-hasher.d.ts +128 -0
- package/dist/password-hasher.d.ts.map +1 -0
- package/dist/password-hasher.js +183 -0
- package/dist/password-hasher.js.map +1 -0
- package/dist/path-validator.d.ts +148 -0
- package/dist/path-validator.d.ts.map +1 -0
- package/dist/path-validator.js +421 -0
- package/dist/path-validator.js.map +1 -0
- package/dist/safe-executor.d.ts +173 -0
- package/dist/safe-executor.d.ts.map +1 -0
- package/dist/safe-executor.js +370 -0
- package/dist/safe-executor.js.map +1 -0
- package/dist/token-generator.d.ts +224 -0
- package/dist/token-generator.d.ts.map +1 -0
- package/dist/token-generator.js +351 -0
- package/dist/token-generator.js.map +1 -0
- package/package.json +1 -1
- package/tsconfig.build.tsbuildinfo +1 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Password Hasher - CVE-2 Remediation
|
|
3
|
+
*
|
|
4
|
+
* Fixes weak password hashing by replacing SHA-256 with hardcoded salt
|
|
5
|
+
* with bcrypt using 12 rounds (configurable).
|
|
6
|
+
*
|
|
7
|
+
* Security Properties:
|
|
8
|
+
* - bcrypt with adaptive cost factor (12 rounds)
|
|
9
|
+
* - Automatic salt generation per password
|
|
10
|
+
* - Timing-safe comparison
|
|
11
|
+
* - Minimum password length enforcement
|
|
12
|
+
*
|
|
13
|
+
* @module v3/security/password-hasher
|
|
14
|
+
*/
|
|
15
|
+
import * as bcrypt from 'bcrypt';
|
|
16
|
+
export class PasswordHashError extends Error {
|
|
17
|
+
code;
|
|
18
|
+
constructor(message, code) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.code = code;
|
|
21
|
+
this.name = 'PasswordHashError';
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Secure password hasher using bcrypt.
|
|
26
|
+
*
|
|
27
|
+
* This class replaces the vulnerable SHA-256 + hardcoded salt implementation
|
|
28
|
+
* with industry-standard bcrypt hashing.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* const hasher = new PasswordHasher({ rounds: 12 });
|
|
33
|
+
* const hash = await hasher.hash('securePassword123');
|
|
34
|
+
* const isValid = await hasher.verify('securePassword123', hash);
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export class PasswordHasher {
|
|
38
|
+
config;
|
|
39
|
+
constructor(config = {}) {
|
|
40
|
+
this.config = {
|
|
41
|
+
rounds: config.rounds ?? 12,
|
|
42
|
+
minLength: config.minLength ?? 8,
|
|
43
|
+
maxLength: config.maxLength ?? 128,
|
|
44
|
+
requireUppercase: config.requireUppercase ?? true,
|
|
45
|
+
requireLowercase: config.requireLowercase ?? true,
|
|
46
|
+
requireDigit: config.requireDigit ?? true,
|
|
47
|
+
requireSpecial: config.requireSpecial ?? false,
|
|
48
|
+
};
|
|
49
|
+
// Validate configuration
|
|
50
|
+
if (this.config.rounds < 10 || this.config.rounds > 20) {
|
|
51
|
+
throw new PasswordHashError('Bcrypt rounds must be between 10 and 20 for security and performance balance', 'INVALID_ROUNDS');
|
|
52
|
+
}
|
|
53
|
+
if (this.config.minLength < 8) {
|
|
54
|
+
throw new PasswordHashError('Minimum password length must be at least 8 characters', 'INVALID_MIN_LENGTH');
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Validates password against configured requirements.
|
|
59
|
+
*
|
|
60
|
+
* @param password - The password to validate
|
|
61
|
+
* @returns Validation result with errors if any
|
|
62
|
+
*/
|
|
63
|
+
validate(password) {
|
|
64
|
+
const errors = [];
|
|
65
|
+
if (!password) {
|
|
66
|
+
errors.push('Password is required');
|
|
67
|
+
return { isValid: false, errors };
|
|
68
|
+
}
|
|
69
|
+
if (password.length < this.config.minLength) {
|
|
70
|
+
errors.push(`Password must be at least ${this.config.minLength} characters`);
|
|
71
|
+
}
|
|
72
|
+
if (password.length > this.config.maxLength) {
|
|
73
|
+
errors.push(`Password must not exceed ${this.config.maxLength} characters`);
|
|
74
|
+
}
|
|
75
|
+
if (this.config.requireUppercase && !/[A-Z]/.test(password)) {
|
|
76
|
+
errors.push('Password must contain at least one uppercase letter');
|
|
77
|
+
}
|
|
78
|
+
if (this.config.requireLowercase && !/[a-z]/.test(password)) {
|
|
79
|
+
errors.push('Password must contain at least one lowercase letter');
|
|
80
|
+
}
|
|
81
|
+
if (this.config.requireDigit && !/\d/.test(password)) {
|
|
82
|
+
errors.push('Password must contain at least one digit');
|
|
83
|
+
}
|
|
84
|
+
if (this.config.requireSpecial && !/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(password)) {
|
|
85
|
+
errors.push('Password must contain at least one special character');
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
isValid: errors.length === 0,
|
|
89
|
+
errors,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Hashes a password using bcrypt.
|
|
94
|
+
*
|
|
95
|
+
* @param password - The plaintext password to hash
|
|
96
|
+
* @returns The bcrypt hash
|
|
97
|
+
* @throws PasswordHashError if password is invalid
|
|
98
|
+
*/
|
|
99
|
+
async hash(password) {
|
|
100
|
+
const validation = this.validate(password);
|
|
101
|
+
if (!validation.isValid) {
|
|
102
|
+
throw new PasswordHashError(validation.errors.join('; '), 'VALIDATION_FAILED');
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
// bcrypt automatically generates a random salt per hash
|
|
106
|
+
return await bcrypt.hash(password, this.config.rounds);
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
throw new PasswordHashError('Failed to hash password', 'HASH_FAILED');
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Verifies a password against a bcrypt hash.
|
|
114
|
+
* Uses timing-safe comparison internally.
|
|
115
|
+
*
|
|
116
|
+
* @param password - The plaintext password to verify
|
|
117
|
+
* @param hash - The bcrypt hash to compare against
|
|
118
|
+
* @returns True if password matches, false otherwise
|
|
119
|
+
*/
|
|
120
|
+
async verify(password, hash) {
|
|
121
|
+
if (!password || !hash) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
// Validate hash format (bcrypt hashes start with $2a$, $2b$, or $2y$)
|
|
125
|
+
if (!this.isValidBcryptHash(hash)) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
try {
|
|
129
|
+
// bcrypt.compare uses timing-safe comparison
|
|
130
|
+
return await bcrypt.compare(password, hash);
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
// Return false on any error to prevent timing attacks
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Checks if a hash needs to be rehashed with updated parameters.
|
|
139
|
+
* Useful for upgrading hash strength over time.
|
|
140
|
+
*
|
|
141
|
+
* @param hash - The bcrypt hash to check
|
|
142
|
+
* @returns True if hash should be updated
|
|
143
|
+
*/
|
|
144
|
+
needsRehash(hash) {
|
|
145
|
+
if (!this.isValidBcryptHash(hash)) {
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
// Extract rounds from hash (format: $2b$XX$...)
|
|
149
|
+
const match = hash.match(/^\$2[aby]\$(\d{2})\$/);
|
|
150
|
+
if (!match) {
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
const hashRounds = parseInt(match[1], 10);
|
|
154
|
+
return hashRounds < this.config.rounds;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Validates bcrypt hash format.
|
|
158
|
+
*
|
|
159
|
+
* @param hash - The hash to validate
|
|
160
|
+
* @returns True if valid bcrypt hash format
|
|
161
|
+
*/
|
|
162
|
+
isValidBcryptHash(hash) {
|
|
163
|
+
// bcrypt hash format: $2a$XX$22charsSalt31charsHash
|
|
164
|
+
// Total length: 60 characters
|
|
165
|
+
return /^\$2[aby]\$\d{2}\$[./A-Za-z0-9]{53}$/.test(hash);
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Returns current configuration (without sensitive defaults).
|
|
169
|
+
*/
|
|
170
|
+
getConfig() {
|
|
171
|
+
return { ...this.config };
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Factory function to create a production-ready password hasher.
|
|
176
|
+
*
|
|
177
|
+
* @param rounds - Bcrypt rounds (default: 12)
|
|
178
|
+
* @returns Configured PasswordHasher instance
|
|
179
|
+
*/
|
|
180
|
+
export function createPasswordHasher(rounds = 12) {
|
|
181
|
+
return new PasswordHasher({ rounds });
|
|
182
|
+
}
|
|
183
|
+
//# sourceMappingURL=password-hasher.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"password-hasher.js","sourceRoot":"","sources":["../src/password-hasher.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AAoDjC,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IAGxB;IAFlB,YACE,OAAe,EACC,IAAY;QAE5B,KAAK,CAAC,OAAO,CAAC,CAAC;QAFC,SAAI,GAAJ,IAAI,CAAQ;QAG5B,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;IAClC,CAAC;CACF;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,cAAc;IACR,MAAM,CAAiC;IAExD,YAAY,SAA+B,EAAE;QAC3C,IAAI,CAAC,MAAM,GAAG;YACZ,MAAM,EAAE,MAAM,CAAC,MAAM,IAAI,EAAE;YAC3B,SAAS,EAAE,MAAM,CAAC,SAAS,IAAI,CAAC;YAChC,SAAS,EAAE,MAAM,CAAC,SAAS,IAAI,GAAG;YAClC,gBAAgB,EAAE,MAAM,CAAC,gBAAgB,IAAI,IAAI;YACjD,gBAAgB,EAAE,MAAM,CAAC,gBAAgB,IAAI,IAAI;YACjD,YAAY,EAAE,MAAM,CAAC,YAAY,IAAI,IAAI;YACzC,cAAc,EAAE,MAAM,CAAC,cAAc,IAAI,KAAK;SAC/C,CAAC;QAEF,yBAAyB;QACzB,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG,EAAE,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG,EAA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C7C,OAAO,MAAM,MAAM,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;QAC9C,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,sDAAsD;YACtD,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;IAED;;;;;;OAMG;IACH,WAAW,CAAC,IAAY;QACtB,IAAI,CAAC,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,EAAE,CAAC;YAClC,OAAO,IAAI,CAAC;QACd,CAAC;QAED,gDAAgD;QAChD,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,sBAAsB,CAAC,CAAC;QACjD,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,OAAO,IAAI,CAAC;QACd,CAAC;QAED,MAAM,UAAU,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;QAC1C,OAAO,UAAU,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;IACzC,CAAC;IAED;;;;;OAKG;IACK,iBAAiB,CAAC,IAAY;QACpC,oDAAoD;QACpD,8BAA8B;QAC9B,OAAO,sCAAsC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC3D,CAAC;IAED;;OAEG;IACH,SAAS;QACP,OAAO,EAAE,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;IAC5B,CAAC;CACF;AAED;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAAC,MAAM,GAAG,EAAE;IAC9C,OAAO,IAAI,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;AACxC,CAAC"}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Path Validator - HIGH-2 Remediation
|
|
3
|
+
*
|
|
4
|
+
* Fixes path traversal vulnerabilities by:
|
|
5
|
+
* - Validating all file paths against allowed prefixes
|
|
6
|
+
* - Using path.resolve() for canonicalization
|
|
7
|
+
* - Blocking traversal patterns (../, etc.)
|
|
8
|
+
* - Enforcing path length limits
|
|
9
|
+
*
|
|
10
|
+
* Security Properties:
|
|
11
|
+
* - Path canonicalization
|
|
12
|
+
* - Prefix validation
|
|
13
|
+
* - Symlink resolution (optional)
|
|
14
|
+
* - Traversal pattern detection
|
|
15
|
+
*
|
|
16
|
+
* @module v3/security/path-validator
|
|
17
|
+
*/
|
|
18
|
+
export interface PathValidatorConfig {
|
|
19
|
+
/**
|
|
20
|
+
* Allowed directory prefixes.
|
|
21
|
+
* Paths must start with one of these after resolution.
|
|
22
|
+
*/
|
|
23
|
+
allowedPrefixes: string[];
|
|
24
|
+
/**
|
|
25
|
+
* Blocked file extensions.
|
|
26
|
+
* Files with these extensions are rejected.
|
|
27
|
+
*/
|
|
28
|
+
blockedExtensions?: string[];
|
|
29
|
+
/**
|
|
30
|
+
* Blocked file names.
|
|
31
|
+
* Files matching these names are rejected.
|
|
32
|
+
*/
|
|
33
|
+
blockedNames?: string[];
|
|
34
|
+
/**
|
|
35
|
+
* Maximum path length.
|
|
36
|
+
* Default: 4096 characters
|
|
37
|
+
*/
|
|
38
|
+
maxPathLength?: number;
|
|
39
|
+
/**
|
|
40
|
+
* Whether to resolve symlinks.
|
|
41
|
+
* Default: true
|
|
42
|
+
*/
|
|
43
|
+
resolveSymlinks?: boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Whether to allow paths that don't exist.
|
|
46
|
+
* Default: true (for write operations)
|
|
47
|
+
*/
|
|
48
|
+
allowNonExistent?: boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Whether to allow hidden files/directories.
|
|
51
|
+
* Default: false
|
|
52
|
+
*/
|
|
53
|
+
allowHidden?: boolean;
|
|
54
|
+
}
|
|
55
|
+
export interface PathValidationResult {
|
|
56
|
+
isValid: boolean;
|
|
57
|
+
resolvedPath: string;
|
|
58
|
+
relativePath: string;
|
|
59
|
+
matchedPrefix: string;
|
|
60
|
+
errors: string[];
|
|
61
|
+
}
|
|
62
|
+
export declare class PathValidatorError extends Error {
|
|
63
|
+
readonly code: string;
|
|
64
|
+
readonly path?: string | undefined;
|
|
65
|
+
constructor(message: string, code: string, path?: string | undefined);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Path validator that prevents traversal attacks.
|
|
69
|
+
*
|
|
70
|
+
* This class validates file paths to ensure they stay within
|
|
71
|
+
* allowed directories and don't access sensitive files.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* const validator = new PathValidator({
|
|
76
|
+
* allowedPrefixes: ['/workspaces/project']
|
|
77
|
+
* });
|
|
78
|
+
*
|
|
79
|
+
* const result = await validator.validate('/workspaces/project/src/file.ts');
|
|
80
|
+
* if (result.isValid) {
|
|
81
|
+
* // Safe to use result.resolvedPath
|
|
82
|
+
* }
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export declare class PathValidator {
|
|
86
|
+
private readonly config;
|
|
87
|
+
private readonly resolvedPrefixes;
|
|
88
|
+
constructor(config: PathValidatorConfig);
|
|
89
|
+
/**
|
|
90
|
+
* Validates a path against security rules.
|
|
91
|
+
*
|
|
92
|
+
* @param inputPath - The path to validate
|
|
93
|
+
* @returns Validation result with resolved path
|
|
94
|
+
*/
|
|
95
|
+
validate(inputPath: string): Promise<PathValidationResult>;
|
|
96
|
+
/**
|
|
97
|
+
* Validates and returns resolved path, throwing on failure.
|
|
98
|
+
*
|
|
99
|
+
* @param inputPath - The path to validate
|
|
100
|
+
* @returns Resolved path if valid
|
|
101
|
+
* @throws PathValidatorError if validation fails
|
|
102
|
+
*/
|
|
103
|
+
validateOrThrow(inputPath: string): Promise<string>;
|
|
104
|
+
/**
|
|
105
|
+
* Synchronous validation (without symlink resolution).
|
|
106
|
+
*
|
|
107
|
+
* @param inputPath - The path to validate
|
|
108
|
+
* @returns Validation result
|
|
109
|
+
*/
|
|
110
|
+
validateSync(inputPath: string): PathValidationResult;
|
|
111
|
+
/**
|
|
112
|
+
* Securely joins path segments within allowed directories.
|
|
113
|
+
*
|
|
114
|
+
* @param prefix - Base directory (must be in allowedPrefixes)
|
|
115
|
+
* @param segments - Path segments to join
|
|
116
|
+
* @returns Validated resolved path
|
|
117
|
+
*/
|
|
118
|
+
securePath(prefix: string, ...segments: string[]): Promise<string>;
|
|
119
|
+
/**
|
|
120
|
+
* Adds a prefix to the allowed list at runtime.
|
|
121
|
+
*
|
|
122
|
+
* @param prefix - Prefix to add
|
|
123
|
+
*/
|
|
124
|
+
addPrefix(prefix: string): void;
|
|
125
|
+
/**
|
|
126
|
+
* Returns the current allowed prefixes.
|
|
127
|
+
*/
|
|
128
|
+
getAllowedPrefixes(): readonly string[];
|
|
129
|
+
/**
|
|
130
|
+
* Checks if a path is within allowed prefixes (quick check).
|
|
131
|
+
*/
|
|
132
|
+
isWithinAllowed(inputPath: string): boolean;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Factory function to create a path validator for a project directory.
|
|
136
|
+
*
|
|
137
|
+
* @param projectRoot - Root directory of the project
|
|
138
|
+
* @returns Configured PathValidator
|
|
139
|
+
*/
|
|
140
|
+
export declare function createProjectPathValidator(projectRoot: string): PathValidator;
|
|
141
|
+
/**
|
|
142
|
+
* Factory function to create a path validator for the entire project.
|
|
143
|
+
*
|
|
144
|
+
* @param projectRoot - Root directory of the project
|
|
145
|
+
* @returns Configured PathValidator
|
|
146
|
+
*/
|
|
147
|
+
export declare function createFullProjectPathValidator(projectRoot: string): PathValidator;
|
|
148
|
+
//# sourceMappingURL=path-validator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"path-validator.d.ts","sourceRoot":"","sources":["../src/path-validator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAKH,MAAM,WAAW,mBAAmB;IAClC;;;OAGG;IACH,eAAe,EAAE,MAAM,EAAE,CAAC;IAE1B;;;OAGG;IACH,iBAAiB,CAAC,EAAE,MAAM,EAAE,CAAC;IAE7B;;;OAGG;IACH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