🌐 AI搜索 & 代理 主页
Skip to content

Commit 1d143a8

Browse files
committed
refactor(core): Add ngDevMode guards and new sanitization error codes
Adds new runtime sanitization error codes. Adds `ngDevMode` guards around error message strings to ensure detailed diagnostics are included only in development mode. This allows production builds to tree-shake verbose error descriptions, reducing bundle size.
1 parent 4f6014a commit 1d143a8

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

goldens/public-api/core/errors.api.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ export const enum RuntimeErrorCode {
5656
// (undocumented)
5757
HOST_DIRECTIVE_UNRESOLVABLE = 307,
5858
// (undocumented)
59+
HTML_SANITIZATION_CLOBBERED = 921,
60+
// (undocumented)
61+
HTML_SANITIZATION_UNSTABLE = 920,
62+
// (undocumented)
5963
HYDRATION_MISSING_NODE = -502,
6064
// (undocumented)
6165
HYDRATION_MISSING_SIBLINGS = -501,
@@ -170,6 +174,8 @@ export const enum RuntimeErrorCode {
170174
// (undocumented)
171175
RUNTIME_DEPS_ORPHAN_COMPONENT = 981,
172176
// (undocumented)
177+
SANITIZATION_BYPASS_TYPE_MISMATCH = 922,
178+
// (undocumented)
173179
SIGNAL_WRITE_FROM_ILLEGAL_CONTEXT = 600,
174180
// (undocumented)
175181
TEMPLATE_STRUCTURE_ERROR = 305,

packages/core/src/errors.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ export const enum RuntimeErrorCode {
134134
MISSING_DIRECTIVE_DEFINITION = 916,
135135
NO_COMPONENT_FACTORY_FOUND = 917,
136136
EXTERNAL_RESOURCE_LOADING_FAILED = 918,
137+
HTML_SANITIZATION_UNSTABLE = 920,
138+
HTML_SANITIZATION_CLOBBERED = 921,
139+
SANITIZATION_BYPASS_TYPE_MISMATCH = 922,
137140

138141
// Signal integration errors
139142
REQUIRED_INPUT_NO_VALUE = -950,

packages/core/src/sanitization/bypass.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import {XSS_SECURITY_URL} from '../error_details_base_url';
10+
import {RuntimeError, RuntimeErrorCode} from '../errors';
1011

1112
export const enum BypassType {
1213
Url = 'URL',
@@ -128,7 +129,10 @@ export function allowSanitizationBypassAndThrow(value: any, type: BypassType): b
128129
if (actualType != null && actualType !== type) {
129130
// Allow ResourceURLs in URL contexts, they are strictly more trusted.
130131
if (actualType === BypassType.ResourceUrl && type === BypassType.Url) return true;
131-
throw new Error(`Required a safe ${type}, got a ${actualType} (see ${XSS_SECURITY_URL})`);
132+
throw new RuntimeError(
133+
RuntimeErrorCode.SANITIZATION_BYPASS_TYPE_MISMATCH,
134+
ngDevMode && `Required a safe ${type}, got a ${actualType} (see ${XSS_SECURITY_URL})`,
135+
);
132136
}
133137
return actualType === type;
134138
}

packages/core/src/sanitization/html_sanitizer.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import {XSS_SECURITY_URL} from '../error_details_base_url';
10+
import {RuntimeError, RuntimeErrorCode} from '../errors';
1011
import {TrustedHTML} from '../util/security/trusted_type_defs';
1112
import {trustedHTMLFromString} from '../util/security/trusted_types';
1213

@@ -262,8 +263,10 @@ export function getNodeName(node: Node): string {
262263
}
263264

264265
function clobberedElementError(node: Node) {
265-
return new Error(
266-
`Failed to sanitize html because the element is clobbered: ${(node as Element).outerHTML}`,
266+
return new RuntimeError(
267+
RuntimeErrorCode.HTML_SANITIZATION_CLOBBERED,
268+
ngDevMode &&
269+
`Failed to sanitize html because the element is clobbered: ${(node as Element).outerHTML}`,
267270
);
268271
}
269272

@@ -314,7 +317,10 @@ export function _sanitizeHtml(defaultDoc: any, unsafeHtmlInput: string): Trusted
314317

315318
do {
316319
if (mXSSAttempts === 0) {
317-
throw new Error('Failed to sanitize html because the input is unstable');
320+
throw new RuntimeError(
321+
RuntimeErrorCode.HTML_SANITIZATION_UNSTABLE,
322+
ngDevMode && 'Failed to sanitize html because the input is unstable',
323+
);
318324
}
319325
mXSSAttempts--;
320326

0 commit comments

Comments
 (0)