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/dist/index.d.ts CHANGED
@@ -1,237 +1,116 @@
1
1
  import { z } from 'zod';
2
2
 
3
- declare const PasswordStrengthSchema: z.ZodObject<{
4
- score: z.ZodNumber;
5
- feedback: z.ZodObject<{
6
- warning: z.ZodOptional<z.ZodString>;
7
- suggestions: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
8
- }, "strip", z.ZodTypeAny, {
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 PasswordStrength = z.infer<typeof PasswordStrengthSchema>;
32
- declare const HashOptionsSchema: z.ZodObject<{
33
- algorithm: z.ZodDefault<z.ZodEnum<["md5", "sha1", "sha256", "sha512", "sha3"]>>;
34
- encoding: z.ZodDefault<z.ZodEnum<["hex", "base64"]>>;
35
- }, "strip", z.ZodTypeAny, {
36
- algorithm: "md5" | "sha1" | "sha256" | "sha512" | "sha3";
37
- encoding: "hex" | "base64";
38
- }, {
39
- algorithm?: "md5" | "sha1" | "sha256" | "sha512" | "sha3" | undefined;
40
- encoding?: "hex" | "base64" | undefined;
41
- }>;
42
- type HashOptions = z.infer<typeof HashOptionsSchema>;
43
- declare const PasswordHashOptionsSchema: z.ZodObject<{
44
- algorithm: z.ZodDefault<z.ZodEnum<["pbkdf2", "argon2", "bcrypt"]>>;
45
- iterations: z.ZodDefault<z.ZodNumber>;
46
- saltLength: z.ZodDefault<z.ZodNumber>;
47
- keyLength: z.ZodDefault<z.ZodNumber>;
48
- }, "strip", z.ZodTypeAny, {
49
- algorithm: "pbkdf2" | "argon2" | "bcrypt";
50
- iterations: number;
51
- saltLength: number;
52
- keyLength: number;
53
- }, {
54
- algorithm?: "pbkdf2" | "argon2" | "bcrypt" | undefined;
55
- iterations?: number | undefined;
56
- saltLength?: number | undefined;
57
- keyLength?: number | undefined;
58
- }>;
59
- type PasswordHashOptions = z.infer<typeof PasswordHashOptionsSchema>;
60
- declare const TimingSafeOptionsSchema: z.ZodObject<{
61
- encoding: z.ZodDefault<z.ZodEnum<["utf8", "hex", "base64"]>>;
62
- }, "strip", z.ZodTypeAny, {
63
- encoding: "hex" | "base64" | "utf8";
64
- }, {
65
- encoding?: "hex" | "base64" | "utf8" | undefined;
66
- }>;
67
- type TimingSafeOptions = z.infer<typeof TimingSafeOptionsSchema>;
68
- declare const EncryptionOptionsSchema: z.ZodObject<{
69
- mode: z.ZodDefault<z.ZodEnum<["aes-cbc", "aes-ctr", "aes-ecb", "aes-gcm"]>>;
70
- padding: z.ZodDefault<z.ZodEnum<["Pkcs7", "NoPadding", "ZeroPadding"]>>;
71
- iv: z.ZodOptional<z.ZodString>;
72
- authTag: z.ZodOptional<z.ZodString>;
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
- * Creates a hash of the input string using the specified algorithm
111
- * @param input - The string to hash
112
- * @param options - Hash options
113
- * @returns The hashed string
114
- */
115
- declare function hash(input: string, options?: Partial<HashOptions>): string;
116
- /**
117
- * Verifies if an input matches a previously hashed value
118
- * @param input - The plain text to verify
119
- * @param hashedValue - The hashed value to compare against
120
- * @param options - Hash options
121
- * @returns True if the input matches the hash, false otherwise
122
- */
123
- declare function verifyHash(input: string, hashedValue: string, options?: Partial<HashOptions>): boolean;
124
- /**
125
- * Creates a HMAC (Hash-based Message Authentication Code) of the input
126
- * @param input - The input message
127
- * @param key - The secret key
128
- * @param options - Hash options
129
- * @returns The HMAC signature
130
- */
131
- declare function hmac(input: string, key: string, options?: Partial<HashOptions>): string;
132
- /**
133
- * Performs a secure password hashing using PBKDF2 (default), Argon2, or bcrypt simulation
134
- * Note: This is a simplified implementation. For production, consider using a specialized library
135
- * @param password - The password to hash
136
- * @param options - Password hash options
137
- * @returns An object containing the hash, salt, and parameters
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
- * Verifies a password against a previously hashed password
146
- * @param password - The password to verify
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 generateRandomBytes(length?: number, encoding?: 'hex' | 'base64'): string;
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
- * A const enum for security levels, used for warnings
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 generateSecurePassword(config?: Partial<Config>): string;
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 Config, ConfigSchema, type EncryptionOptions, EncryptionOptionsSchema, type HashOptions, HashOptionsSchema, type PasswordHashOptions, PasswordHashOptionsSchema, type PasswordStrength, PasswordStrengthSchema, SecurityLevel, type TimingSafeOptions, TimingSafeOptionsSchema, analyzePasswordPatterns, calculatePasswordEntropy, checkPasswordStrength, decrypt, encrypt, generateKey, generateRandomBytes, generateSecurePassword, getSecurityLevel, hash, hashPassword, hmac, timingSafeEqual, verifyHash, verifyPassword };
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 };