typesecure 0.1.0 → 0.2.2
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/README.md +91 -99
- package/dist/index.d.mts +105 -226
- package/dist/index.d.ts +105 -226
- package/dist/index.js +200 -663
- package/dist/index.mjs +185 -638
- package/package.json +37 -35
package/dist/index.d.ts
CHANGED
|
@@ -1,237 +1,116 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
warning?: string | undefined;
|
|
10
|
-
suggestions?: string[] | undefined;
|
|
11
|
-
}, {
|
|
12
|
-
warning?: string | undefined;
|
|
13
|
-
suggestions?: string[] | undefined;
|
|
14
|
-
}>;
|
|
15
|
-
isStrong: z.ZodBoolean;
|
|
16
|
-
}, "strip", z.ZodTypeAny, {
|
|
17
|
-
score: number;
|
|
18
|
-
feedback: {
|
|
19
|
-
warning?: string | undefined;
|
|
20
|
-
suggestions?: string[] | undefined;
|
|
21
|
-
};
|
|
22
|
-
isStrong: boolean;
|
|
23
|
-
}, {
|
|
24
|
-
score: number;
|
|
25
|
-
feedback: {
|
|
26
|
-
warning?: string | undefined;
|
|
27
|
-
suggestions?: string[] | undefined;
|
|
28
|
-
};
|
|
29
|
-
isStrong: boolean;
|
|
3
|
+
type DataClassification = "public" | "pii" | "secret" | "token" | "credential";
|
|
4
|
+
declare const TYPESECURE_SYMBOL: unique symbol;
|
|
5
|
+
type Classified<K extends DataClassification, T> = Readonly<{
|
|
6
|
+
kind: K;
|
|
7
|
+
value: T;
|
|
8
|
+
[TYPESECURE_SYMBOL]: true;
|
|
30
9
|
}>;
|
|
31
|
-
type
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
declare const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
declare
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
aad: z.ZodOptional<z.ZodString>;
|
|
74
|
-
}, "strip", z.ZodTypeAny, {
|
|
75
|
-
mode: "aes-cbc" | "aes-ctr" | "aes-ecb" | "aes-gcm";
|
|
76
|
-
padding: "Pkcs7" | "NoPadding" | "ZeroPadding";
|
|
77
|
-
iv?: string | undefined;
|
|
78
|
-
authTag?: string | undefined;
|
|
79
|
-
aad?: string | undefined;
|
|
80
|
-
}, {
|
|
81
|
-
mode?: "aes-cbc" | "aes-ctr" | "aes-ecb" | "aes-gcm" | undefined;
|
|
82
|
-
padding?: "Pkcs7" | "NoPadding" | "ZeroPadding" | undefined;
|
|
83
|
-
iv?: string | undefined;
|
|
84
|
-
authTag?: string | undefined;
|
|
85
|
-
aad?: string | undefined;
|
|
86
|
-
}>;
|
|
87
|
-
type EncryptionOptions = z.infer<typeof EncryptionOptionsSchema>;
|
|
88
|
-
declare const ConfigSchema: z.ZodObject<{
|
|
89
|
-
minimumPasswordLength: z.ZodDefault<z.ZodNumber>;
|
|
90
|
-
requireNumbers: z.ZodDefault<z.ZodBoolean>;
|
|
91
|
-
requireSymbols: z.ZodDefault<z.ZodBoolean>;
|
|
92
|
-
requireUppercase: z.ZodDefault<z.ZodBoolean>;
|
|
93
|
-
requireLowercase: z.ZodDefault<z.ZodBoolean>;
|
|
94
|
-
}, "strip", z.ZodTypeAny, {
|
|
95
|
-
minimumPasswordLength: number;
|
|
96
|
-
requireNumbers: boolean;
|
|
97
|
-
requireSymbols: boolean;
|
|
98
|
-
requireUppercase: boolean;
|
|
99
|
-
requireLowercase: boolean;
|
|
100
|
-
}, {
|
|
101
|
-
minimumPasswordLength?: number | undefined;
|
|
102
|
-
requireNumbers?: boolean | undefined;
|
|
103
|
-
requireSymbols?: boolean | undefined;
|
|
104
|
-
requireUppercase?: boolean | undefined;
|
|
105
|
-
requireLowercase?: boolean | undefined;
|
|
106
|
-
}>;
|
|
107
|
-
type Config = z.infer<typeof ConfigSchema>;
|
|
10
|
+
type PublicString = Classified<"public", string>;
|
|
11
|
+
type PIIString = Classified<"pii", string>;
|
|
12
|
+
type SecretString = Classified<"secret", string>;
|
|
13
|
+
type TokenString = Classified<"token", string>;
|
|
14
|
+
type CredentialString = Classified<"credential", string>;
|
|
15
|
+
declare function isClassified(value: unknown): value is Classified<DataClassification, unknown>;
|
|
16
|
+
declare function classificationOf(value: unknown): DataClassification | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Intentionally explicit: use this when you need the raw string (e.g. building an HTTP header).
|
|
19
|
+
* Prefer passing classified values to safe sinks/policies instead of revealing early.
|
|
20
|
+
*/
|
|
21
|
+
declare function reveal<T>(value: Classified<DataClassification, T>): T;
|
|
22
|
+
declare const PublicStringSchema: z.ZodEffects<z.ZodString, Readonly<{
|
|
23
|
+
kind: "public";
|
|
24
|
+
value: string;
|
|
25
|
+
[TYPESECURE_SYMBOL]: true;
|
|
26
|
+
}>, string>;
|
|
27
|
+
declare const PIIStringSchema: z.ZodEffects<z.ZodString, Readonly<{
|
|
28
|
+
kind: "pii";
|
|
29
|
+
value: string;
|
|
30
|
+
[TYPESECURE_SYMBOL]: true;
|
|
31
|
+
}>, string>;
|
|
32
|
+
declare const SecretStringSchema: z.ZodEffects<z.ZodString, Readonly<{
|
|
33
|
+
kind: "secret";
|
|
34
|
+
value: string;
|
|
35
|
+
[TYPESECURE_SYMBOL]: true;
|
|
36
|
+
}>, string>;
|
|
37
|
+
declare const TokenStringSchema: z.ZodEffects<z.ZodString, Readonly<{
|
|
38
|
+
kind: "token";
|
|
39
|
+
value: string;
|
|
40
|
+
[TYPESECURE_SYMBOL]: true;
|
|
41
|
+
}>, string>;
|
|
42
|
+
declare const CredentialStringSchema: z.ZodEffects<z.ZodString, Readonly<{
|
|
43
|
+
kind: "credential";
|
|
44
|
+
value: string;
|
|
45
|
+
[TYPESECURE_SYMBOL]: true;
|
|
46
|
+
}>, string>;
|
|
47
|
+
declare function publicText(value: string): PublicString;
|
|
48
|
+
declare function piiText(value: string): PIIString;
|
|
49
|
+
declare function secretText(value: string): SecretString;
|
|
50
|
+
declare function token(value: string): TokenString;
|
|
51
|
+
declare function credential(value: string): CredentialString;
|
|
108
52
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
*
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
*/
|
|
139
|
-
declare function hashPassword(password: string, options?: Partial<PasswordHashOptions>): {
|
|
140
|
-
hash: string;
|
|
141
|
-
salt: string;
|
|
142
|
-
params: PasswordHashOptions;
|
|
53
|
+
type RedactOptions = Readonly<{
|
|
54
|
+
/**
|
|
55
|
+
* If true, redact values for suspicious keys even if they aren't classified.
|
|
56
|
+
* Defaults to true.
|
|
57
|
+
*/
|
|
58
|
+
guessByKey?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Placeholder format for redacted values.
|
|
61
|
+
* Defaults to "[REDACTED:<kind>]".
|
|
62
|
+
*/
|
|
63
|
+
placeholder?: (kind: DataClassification | "unknown") => string;
|
|
64
|
+
/**
|
|
65
|
+
* Max depth to traverse to avoid pathological structures.
|
|
66
|
+
* Defaults to 25.
|
|
67
|
+
*/
|
|
68
|
+
maxDepth?: number;
|
|
69
|
+
}>;
|
|
70
|
+
declare function redact<T>(value: T, options?: RedactOptions): T;
|
|
71
|
+
declare function safeJsonStringify(value: unknown, options?: RedactOptions, space?: number): string;
|
|
72
|
+
/**
|
|
73
|
+
* Convenience logger that will redact classified data and suspicious keys.
|
|
74
|
+
* You can pass your own logger implementation (pino, winston, console, etc).
|
|
75
|
+
*/
|
|
76
|
+
declare function safeLoggerAdapter(logger: Pick<Console, "info" | "warn" | "error" | "debug" | "log">): {
|
|
77
|
+
info: (...args: unknown[]) => void;
|
|
78
|
+
warn: (...args: unknown[]) => void;
|
|
79
|
+
error: (...args: unknown[]) => void;
|
|
80
|
+
debug: (...args: unknown[]) => void;
|
|
81
|
+
log: (...args: unknown[]) => void;
|
|
143
82
|
};
|
|
144
83
|
/**
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
* @param hash - The previously generated hash
|
|
148
|
-
* @param salt - The salt used in the original hash
|
|
149
|
-
* @param options - Password hash options (must match those used in hashPassword)
|
|
150
|
-
* @returns True if the password matches, false otherwise
|
|
151
|
-
*/
|
|
152
|
-
declare function verifyPassword(password: string, hash: string, salt: string, options?: Partial<PasswordHashOptions>): boolean;
|
|
153
|
-
/**
|
|
154
|
-
* Performs a constant-time comparison of two strings to prevent timing attacks
|
|
155
|
-
* @param a - First string to compare
|
|
156
|
-
* @param b - Second string to compare
|
|
157
|
-
* @param options - Timing safe comparison options
|
|
158
|
-
* @returns True if both strings are equal, false otherwise
|
|
159
|
-
*/
|
|
160
|
-
declare function timingSafeEqual(a: string, b: string, options?: Partial<TimingSafeOptions>): boolean;
|
|
161
|
-
/**
|
|
162
|
-
* Generates a cryptographically secure random string
|
|
163
|
-
* @param length - The length of the random string in bytes
|
|
164
|
-
* @param encoding - The encoding to use for the output
|
|
165
|
-
* @returns A random string in the specified encoding
|
|
84
|
+
* Helper for cases where you must emit a raw header value.
|
|
85
|
+
* Prefer using the policy layer to validate crossings before revealing.
|
|
166
86
|
*/
|
|
167
|
-
declare function
|
|
87
|
+
declare function httpAuthorizationBearer(tokenValue: TokenString): string;
|
|
168
88
|
|
|
89
|
+
type PolicyAction = "log" | "network" | "storage" | "analytics";
|
|
90
|
+
type PolicyDecision = Readonly<{
|
|
91
|
+
allowed: boolean;
|
|
92
|
+
reason?: string;
|
|
93
|
+
detectedKinds?: DataClassification[];
|
|
94
|
+
}>;
|
|
95
|
+
type Policy = Readonly<{
|
|
96
|
+
name: string;
|
|
97
|
+
allow: Record<PolicyAction, ReadonlySet<DataClassification>>;
|
|
98
|
+
redactBefore?: ReadonlySet<PolicyAction>;
|
|
99
|
+
redaction?: RedactOptions;
|
|
100
|
+
}>;
|
|
101
|
+
declare function defaultPolicy(): Policy;
|
|
102
|
+
declare function decide(policy: Policy, action: PolicyAction, data: unknown): PolicyDecision;
|
|
103
|
+
declare function assertAllowed(policy: Policy, action: PolicyAction, data: unknown): void;
|
|
104
|
+
type AuditEvent = Readonly<{
|
|
105
|
+
at: number;
|
|
106
|
+
policy: string;
|
|
107
|
+
action: PolicyAction;
|
|
108
|
+
decision: PolicyDecision;
|
|
109
|
+
}>;
|
|
110
|
+
declare function audit(policy: Policy, action: PolicyAction, data: unknown): AuditEvent;
|
|
169
111
|
/**
|
|
170
|
-
*
|
|
171
|
-
* @public
|
|
172
|
-
*/
|
|
173
|
-
declare enum SecurityLevel {
|
|
174
|
-
HIGH = "HIGH",
|
|
175
|
-
MEDIUM = "MEDIUM",
|
|
176
|
-
LOW = "LOW",
|
|
177
|
-
INSECURE = "INSECURE"
|
|
178
|
-
}
|
|
179
|
-
/**
|
|
180
|
-
* Returns the security level of the encryption options
|
|
181
|
-
* @param options - The encryption options
|
|
182
|
-
* @returns The security level
|
|
183
|
-
*/
|
|
184
|
-
declare function getSecurityLevel(options: EncryptionOptions): SecurityLevel;
|
|
185
|
-
/**
|
|
186
|
-
* Encrypts the provided text using the specified encryption options
|
|
187
|
-
* @param text - The text to encrypt
|
|
188
|
-
* @param key - The encryption key
|
|
189
|
-
* @param options - Encryption options
|
|
190
|
-
* @returns The encrypted text
|
|
191
|
-
*/
|
|
192
|
-
declare function encrypt(text: string, key: string, options?: Partial<EncryptionOptions>): string;
|
|
193
|
-
/**
|
|
194
|
-
* Decrypts the provided encrypted text using the specified options
|
|
195
|
-
* @param encryptedText - The text to decrypt
|
|
196
|
-
* @param key - The decryption key
|
|
197
|
-
* @param options - Encryption options
|
|
198
|
-
* @returns The decrypted text
|
|
199
|
-
*/
|
|
200
|
-
declare function decrypt(encryptedText: string, key: string, options?: Partial<EncryptionOptions>): string;
|
|
201
|
-
/**
|
|
202
|
-
* Generates a random encryption key
|
|
203
|
-
* @param length - Length of the key in bytes
|
|
204
|
-
* @returns A random key as a hex string
|
|
205
|
-
*/
|
|
206
|
-
declare function generateKey(length?: number): string;
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* Calculates the entropy of a password in bits
|
|
210
|
-
* @param password - The password to calculate entropy for
|
|
211
|
-
* @returns The entropy in bits
|
|
212
|
-
*/
|
|
213
|
-
declare function calculatePasswordEntropy(password: string): number;
|
|
214
|
-
/**
|
|
215
|
-
* Analyzes patterns in the password that might reduce its effective entropy
|
|
216
|
-
* @param password - The password to analyze
|
|
217
|
-
* @returns The entropy reduction (0 to 1)
|
|
218
|
-
*/
|
|
219
|
-
declare function analyzePasswordPatterns(password: string): {
|
|
220
|
-
entropyReduction: number;
|
|
221
|
-
patterns: string[];
|
|
222
|
-
};
|
|
223
|
-
/**
|
|
224
|
-
* Checks the strength of a password using both rule-based criteria and entropy
|
|
225
|
-
* @param password - The password to check
|
|
226
|
-
* @param config - Password configuration options
|
|
227
|
-
* @returns A PasswordStrength object containing score and feedback
|
|
228
|
-
*/
|
|
229
|
-
declare function checkPasswordStrength(password: string, config?: Partial<Config>): PasswordStrength;
|
|
230
|
-
/**
|
|
231
|
-
* Generates a secure random password based on configuration
|
|
232
|
-
* @param config - Password configuration options
|
|
233
|
-
* @returns A random secure password
|
|
112
|
+
* Safe sink example: log with enforcement + redaction (if configured).
|
|
234
113
|
*/
|
|
235
|
-
declare function
|
|
114
|
+
declare function policyLog(policy: Policy, logger: Pick<Console, "info" | "warn" | "error" | "debug" | "log">, level: keyof Pick<Console, "info" | "warn" | "error" | "debug" | "log">, ...args: unknown[]): void;
|
|
236
115
|
|
|
237
|
-
export { type
|
|
116
|
+
export { type AuditEvent, type Classified, type CredentialString, CredentialStringSchema, type DataClassification, type PIIString, PIIStringSchema, type Policy, type PolicyAction, type PolicyDecision, type PublicString, PublicStringSchema, type RedactOptions, type SecretString, SecretStringSchema, type TokenString, TokenStringSchema, assertAllowed, audit, classificationOf, credential, decide, defaultPolicy, httpAuthorizationBearer, isClassified, piiText, policyLog, publicText, redact, reveal, safeJsonStringify, safeLoggerAdapter, secretText, token };
|