@angular-helpers/security 21.1.0 → 21.3.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.
- package/README.es.md +356 -1
- package/README.md +197 -0
- package/fesm2022/angular-helpers-security-forms.mjs +110 -0
- package/fesm2022/angular-helpers-security-signal-forms.mjs +159 -0
- package/fesm2022/angular-helpers-security.mjs +920 -180
- package/package.json +15 -1
- package/types/angular-helpers-security-forms.d.ts +65 -0
- package/types/angular-helpers-security-signal-forms.d.ts +99 -0
- package/types/angular-helpers-security.d.ts +414 -10
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { assessPasswordStrength, isHtmlSafe, isUrlSafe, containsScriptInjection, containsSqlInjectionHints } from '@angular-helpers/security';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Collection of Reactive Forms validators that bridge the shared security helpers into
|
|
5
|
+
* the Angular `ValidatorFn` contract. All validators are static factory functions and do not
|
|
6
|
+
* require provider registration.
|
|
7
|
+
*
|
|
8
|
+
* For the Signal Forms equivalents see `@angular-helpers/security/signal-forms`.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const form = new FormGroup({
|
|
12
|
+
* password: new FormControl('', [
|
|
13
|
+
* Validators.required,
|
|
14
|
+
* SecurityValidators.strongPassword({ minScore: 3 }),
|
|
15
|
+
* ]),
|
|
16
|
+
* bio: new FormControl('', [SecurityValidators.safeHtml()]),
|
|
17
|
+
* homepage: new FormControl('', [SecurityValidators.safeUrl({ schemes: ['https:'] })]),
|
|
18
|
+
* });
|
|
19
|
+
*/
|
|
20
|
+
class SecurityValidators {
|
|
21
|
+
/**
|
|
22
|
+
* Validates password strength using the shared entropy-based scoring logic.
|
|
23
|
+
* Returns `{ weakPassword: { score, required } }` when the score is below `minScore`.
|
|
24
|
+
*
|
|
25
|
+
* @param options.minScore Minimum acceptable score (0..4). Default: `2` (fair).
|
|
26
|
+
*/
|
|
27
|
+
static strongPassword(options = {}) {
|
|
28
|
+
const required = options.minScore ?? 2;
|
|
29
|
+
return (control) => {
|
|
30
|
+
const value = control.value;
|
|
31
|
+
if (value === null || value === undefined || value === '')
|
|
32
|
+
return null;
|
|
33
|
+
if (typeof value !== 'string')
|
|
34
|
+
return null;
|
|
35
|
+
const { score } = assessPasswordStrength(value);
|
|
36
|
+
return score < required ? { weakPassword: { score, required } } : null;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Validates that the given HTML string contains no tags or attributes outside the allowlist.
|
|
41
|
+
* Returns `{ unsafeHtml: true }` when sanitization would alter the value.
|
|
42
|
+
*
|
|
43
|
+
* Requires a browser environment (DOMParser). In SSR contexts the validator returns `null`
|
|
44
|
+
* (no error) to avoid blocking forms server-side; re-validation happens automatically on
|
|
45
|
+
* hydration.
|
|
46
|
+
*/
|
|
47
|
+
static safeHtml(options = {}) {
|
|
48
|
+
return (control) => {
|
|
49
|
+
const value = control.value;
|
|
50
|
+
if (value === null || value === undefined || value === '')
|
|
51
|
+
return null;
|
|
52
|
+
if (typeof value !== 'string')
|
|
53
|
+
return null;
|
|
54
|
+
if (typeof DOMParser === 'undefined')
|
|
55
|
+
return null;
|
|
56
|
+
return isHtmlSafe(value, options) ? null : { unsafeHtml: true };
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Validates that the given URL is well-formed and uses an allowed scheme.
|
|
61
|
+
* Returns `{ unsafeUrl: true }` for `javascript:`, `data:`, relative URLs, and other
|
|
62
|
+
* non-allowlisted protocols.
|
|
63
|
+
*/
|
|
64
|
+
static safeUrl(options = {}) {
|
|
65
|
+
const schemes = options.schemes ?? ['http:', 'https:'];
|
|
66
|
+
return (control) => {
|
|
67
|
+
const value = control.value;
|
|
68
|
+
if (value === null || value === undefined || value === '')
|
|
69
|
+
return null;
|
|
70
|
+
if (typeof value !== 'string')
|
|
71
|
+
return null;
|
|
72
|
+
return isUrlSafe(value, schemes) ? null : { unsafeUrl: true };
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Rejects values that look like script injection attempts (`<script>`, `javascript:`,
|
|
77
|
+
* or inline event handlers). Lightweight pattern check — NOT a substitute for a full
|
|
78
|
+
* HTML sanitizer.
|
|
79
|
+
*/
|
|
80
|
+
static noScriptInjection() {
|
|
81
|
+
return (control) => {
|
|
82
|
+
const value = control.value;
|
|
83
|
+
if (value === null || value === undefined || value === '')
|
|
84
|
+
return null;
|
|
85
|
+
if (typeof value !== 'string')
|
|
86
|
+
return null;
|
|
87
|
+
return containsScriptInjection(value) ? { scriptInjection: true } : null;
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Heuristic check for common SQL-injection sentinel strings. Intended as defense-in-depth
|
|
92
|
+
* for user-facing inputs. Use parameterized queries on the server as the primary defense.
|
|
93
|
+
*/
|
|
94
|
+
static noSqlInjectionHints() {
|
|
95
|
+
return (control) => {
|
|
96
|
+
const value = control.value;
|
|
97
|
+
if (value === null || value === undefined || value === '')
|
|
98
|
+
return null;
|
|
99
|
+
if (typeof value !== 'string')
|
|
100
|
+
return null;
|
|
101
|
+
return containsSqlInjectionHints(value) ? { sqlInjectionHint: true } : null;
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Generated bundle index. Do not edit.
|
|
108
|
+
*/
|
|
109
|
+
|
|
110
|
+
export { SecurityValidators };
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { inject, resource } from '@angular/core';
|
|
2
|
+
import { validate, validateAsync } from '@angular/forms/signals';
|
|
3
|
+
import { assessPasswordStrength, isHtmlSafe, isUrlSafe, containsScriptInjection, containsSqlInjectionHints, HibpService } from '@angular-helpers/security';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Registers a sync validation rule on a string field that fails when the password strength
|
|
7
|
+
* is below the required score. Uses the shared `assessPasswordStrength` helper for behavioural
|
|
8
|
+
* parity with the Reactive Forms `SecurityValidators.strongPassword`.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* form(model, (p) => {
|
|
12
|
+
* required(p.password);
|
|
13
|
+
* strongPassword(p.password, { minScore: 3 });
|
|
14
|
+
* });
|
|
15
|
+
*/
|
|
16
|
+
function strongPassword(path, options) {
|
|
17
|
+
const required = options?.minScore ?? 2;
|
|
18
|
+
const message = options?.message ?? 'Password is too weak';
|
|
19
|
+
validate(path, ({ value }) => {
|
|
20
|
+
const raw = value();
|
|
21
|
+
if (!raw)
|
|
22
|
+
return null;
|
|
23
|
+
const { score } = assessPasswordStrength(raw);
|
|
24
|
+
return score < required ? { kind: 'weakPassword', message } : null;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Registers a sync validation rule that fails when the input contains tags or attributes
|
|
29
|
+
* outside the allowed HTML sanitizer allowlist.
|
|
30
|
+
*
|
|
31
|
+
* Requires a browser environment. In SSR contexts the rule is a no-op (returns `null`) so
|
|
32
|
+
* server-rendered forms remain submittable; re-validation happens on hydration.
|
|
33
|
+
*/
|
|
34
|
+
function safeHtml(path, options) {
|
|
35
|
+
const message = options?.message ?? 'Value contains unsafe HTML';
|
|
36
|
+
const sanitizerOptions = {
|
|
37
|
+
allowedTags: options?.allowedTags,
|
|
38
|
+
allowedAttributes: options?.allowedAttributes,
|
|
39
|
+
};
|
|
40
|
+
validate(path, ({ value }) => {
|
|
41
|
+
const raw = value();
|
|
42
|
+
if (!raw)
|
|
43
|
+
return null;
|
|
44
|
+
if (typeof DOMParser === 'undefined')
|
|
45
|
+
return null;
|
|
46
|
+
return isHtmlSafe(raw, sanitizerOptions) ? null : { kind: 'unsafeHtml', message };
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Registers a sync validation rule that fails for malformed URLs or URLs using schemes
|
|
51
|
+
* outside the allowlist (default: `http:` and `https:`).
|
|
52
|
+
*/
|
|
53
|
+
function safeUrl(path, options) {
|
|
54
|
+
const schemes = options?.schemes ?? ['http:', 'https:'];
|
|
55
|
+
const message = options?.message ?? 'URL scheme is not allowed';
|
|
56
|
+
validate(path, ({ value }) => {
|
|
57
|
+
const raw = value();
|
|
58
|
+
if (!raw)
|
|
59
|
+
return null;
|
|
60
|
+
return isUrlSafe(raw, schemes) ? null : { kind: 'unsafeUrl', message };
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Registers a sync validation rule that rejects values matching common script-injection
|
|
65
|
+
* sentinels (`<script>`, `javascript:`, inline event handlers).
|
|
66
|
+
*/
|
|
67
|
+
function noScriptInjection(path, options) {
|
|
68
|
+
const message = options?.message ?? 'Value contains script injection patterns';
|
|
69
|
+
validate(path, ({ value }) => {
|
|
70
|
+
const raw = value();
|
|
71
|
+
if (!raw)
|
|
72
|
+
return null;
|
|
73
|
+
return containsScriptInjection(raw) ? { kind: 'scriptInjection', message } : null;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Registers a sync validation rule that rejects common SQL-injection sentinel strings.
|
|
78
|
+
* Intended as defense-in-depth alongside server-side parameterized queries.
|
|
79
|
+
*/
|
|
80
|
+
function noSqlInjectionHints(path, options) {
|
|
81
|
+
const message = options?.message ?? 'Value contains SQL injection hints';
|
|
82
|
+
validate(path, ({ value }) => {
|
|
83
|
+
const raw = value();
|
|
84
|
+
if (!raw)
|
|
85
|
+
return null;
|
|
86
|
+
return containsSqlInjectionHints(raw) ? { kind: 'sqlInjectionHint', message } : null;
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Registers an async validation rule that checks the password against the Have I Been Pwned
|
|
91
|
+
* breach corpus via the k-anonymity API. Requires `provideHibp()` to be set up in the
|
|
92
|
+
* injector hierarchy.
|
|
93
|
+
*
|
|
94
|
+
* Fail-open semantics: network errors and unsupported environments do NOT produce a
|
|
95
|
+
* validation error — the form remains submittable. This is intentional to prevent HIBP
|
|
96
|
+
* outages from blocking user sign-ups.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* form(model, (p) => {
|
|
100
|
+
* required(p.password);
|
|
101
|
+
* strongPassword(p.password, { minScore: 3 });
|
|
102
|
+
* hibpPassword(p.password);
|
|
103
|
+
* });
|
|
104
|
+
*/
|
|
105
|
+
function hibpPassword(path, options) {
|
|
106
|
+
const message = options?.message ?? 'This password has appeared in a data breach';
|
|
107
|
+
const debounceMs = options?.debounceMs ?? 300;
|
|
108
|
+
validateAsync(path, {
|
|
109
|
+
params: ({ value }) => {
|
|
110
|
+
const raw = value();
|
|
111
|
+
return raw && raw.length >= 8 ? raw : undefined;
|
|
112
|
+
},
|
|
113
|
+
factory: (passwordSignal) => {
|
|
114
|
+
const hibp = inject(HibpService);
|
|
115
|
+
return resource({
|
|
116
|
+
params: () => passwordSignal(),
|
|
117
|
+
loader: async ({ params: password, abortSignal }) => {
|
|
118
|
+
if (!password)
|
|
119
|
+
return undefined;
|
|
120
|
+
if (debounceMs > 0) {
|
|
121
|
+
await debounce(debounceMs, abortSignal);
|
|
122
|
+
}
|
|
123
|
+
return hibp.isPasswordLeaked(password);
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
},
|
|
127
|
+
onSuccess: (result) => {
|
|
128
|
+
if (!result)
|
|
129
|
+
return null;
|
|
130
|
+
if (result.error)
|
|
131
|
+
return null;
|
|
132
|
+
return result.leaked ? { kind: 'leakedPassword', message, count: result.count } : null;
|
|
133
|
+
},
|
|
134
|
+
onError: () => null,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
function debounce(ms, signal) {
|
|
138
|
+
return new Promise((resolve, reject) => {
|
|
139
|
+
if (signal.aborted) {
|
|
140
|
+
reject(signal.reason);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
const timer = setTimeout(() => {
|
|
144
|
+
signal.removeEventListener('abort', onAbort);
|
|
145
|
+
resolve();
|
|
146
|
+
}, ms);
|
|
147
|
+
const onAbort = () => {
|
|
148
|
+
clearTimeout(timer);
|
|
149
|
+
reject(signal.reason);
|
|
150
|
+
};
|
|
151
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Generated bundle index. Do not edit.
|
|
157
|
+
*/
|
|
158
|
+
|
|
159
|
+
export { hibpPassword, noScriptInjection, noSqlInjectionHints, safeHtml, safeUrl, strongPassword };
|