@chobantonov/jsonforms-react-extended-renderers 0.0.0-stage → 0.1.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/LICENSE +22 -0
- package/README.md +17 -2
- package/lib/AgGridControlRenderer.impl-BO-Z1x0Y.js +2 -0
- package/lib/AgGridControlRenderer.impl-BO-Z1x0Y.js.map +1 -0
- package/lib/AgGridControlRenderer.impl-BZS8_ocu.mjs +429 -0
- package/lib/AgGridControlRenderer.impl-BZS8_ocu.mjs.map +1 -0
- package/lib/DynamicJSXRenderer-Cyw40Kz0.js +2 -0
- package/lib/DynamicJSXRenderer-Cyw40Kz0.js.map +1 -0
- package/lib/DynamicJSXRenderer-DyB3ZpZV.mjs +109 -0
- package/lib/DynamicJSXRenderer-DyB3ZpZV.mjs.map +1 -0
- package/lib/MarkupLabelRenderer.impl-CWfdcUuH.mjs +12 -0
- package/lib/MarkupLabelRenderer.impl-CWfdcUuH.mjs.map +1 -0
- package/lib/MarkupLabelRenderer.impl-DnuzIPQg.js +2 -0
- package/lib/MarkupLabelRenderer.impl-DnuzIPQg.js.map +1 -0
- package/lib/MonacoControlRenderer.impl-BwgLGNMK.js +3 -0
- package/lib/MonacoControlRenderer.impl-BwgLGNMK.js.map +1 -0
- package/lib/MonacoControlRenderer.impl-CyjMHbOd.mjs +186 -0
- package/lib/MonacoControlRenderer.impl-CyjMHbOd.mjs.map +1 -0
- package/lib/RactiveTemplate-7L44A38e.js +2 -0
- package/lib/RactiveTemplate-7L44A38e.js.map +1 -0
- package/lib/RactiveTemplate-Cat7CxUp.mjs +74 -0
- package/lib/RactiveTemplate-Cat7CxUp.mjs.map +1 -0
- package/lib/ajv-localizers.cjs.js +2 -0
- package/lib/ajv-localizers.cjs.js.map +1 -0
- package/lib/ajv-localizers.esm.js +7 -0
- package/lib/ajv-localizers.esm.js.map +1 -0
- package/lib/ajvI18n-2UTyJv5g.js +2 -0
- package/lib/ajvI18n-2UTyJv5g.js.map +1 -0
- package/lib/ajvI18n-DO8FFvx1.mjs +227 -0
- package/lib/ajvI18n-DO8FFvx1.mjs.map +1 -0
- package/lib/celTemplate-Cl0alaCz.js +3 -0
- package/lib/celTemplate-Cl0alaCz.js.map +1 -0
- package/lib/celTemplate-D_AGhaIL.mjs +137 -0
- package/lib/celTemplate-D_AGhaIL.mjs.map +1 -0
- package/lib/components/DynamicJSXRenderer.d.ts +7 -0
- package/lib/components/ExtendedJsonForms.d.ts +80 -0
- package/lib/components/GenericAdditionalProperties.d.ts +4 -0
- package/lib/components/InterpolatedText.d.ts +34 -0
- package/lib/components/RactiveTemplate.d.ts +37 -0
- package/lib/components/editorRendererTypes.d.ts +139 -0
- package/lib/components/elementRender.d.ts +15 -0
- package/lib/configNamespaces-BFclHtB8.mjs +1 -0
- package/lib/configNamespaces-sboXDudL.js +1 -0
- package/lib/core/ajv.d.ts +82 -0
- package/lib/core/ajvI18n/bg.d.ts +2 -0
- package/lib/core/ajvI18n/index.d.ts +118 -0
- package/lib/core/ajvI18n/localizers.d.ts +20 -0
- package/lib/core/dynamicDefaults.d.ts +34 -0
- package/lib/core/keywords.d.ts +24 -0
- package/lib/core/transform.d.ts +29 -0
- package/lib/core/uiSchema.d.ts +48 -0
- package/lib/elementRender-Dbp2e-Cc.js +2 -0
- package/lib/elementRender-Dbp2e-Cc.js.map +1 -0
- package/lib/elementRender-y8MK9CCE.mjs +5 -0
- package/lib/elementRender-y8MK9CCE.mjs.map +1 -0
- package/lib/index.d.ts +41 -0
- package/lib/jsonforms-react-extended.cjs.js +16 -0
- package/lib/jsonforms-react-extended.cjs.js.map +1 -0
- package/lib/jsonforms-react-extended.esm.js +2335 -0
- package/lib/jsonforms-react-extended.esm.js.map +1 -0
- package/lib/locale/bg.d.ts +14 -0
- package/lib/markdown-C_y73Q0w.mjs +123 -0
- package/lib/markdown-C_y73Q0w.mjs.map +1 -0
- package/lib/markdown-CuqtpGVd.js +2 -0
- package/lib/markdown-CuqtpGVd.js.map +1 -0
- package/lib/objectWithoutProperties-6zedhz76.js +2 -0
- package/lib/objectWithoutProperties-6zedhz76.js.map +1 -0
- package/lib/objectWithoutProperties-yjQMyZ0y.mjs +58 -0
- package/lib/objectWithoutProperties-yjQMyZ0y.mjs.map +1 -0
- package/lib/renderers/AgGridControlRenderer.d.ts +10 -0
- package/lib/renderers/AgGridControlRenderer.impl.d.ts +4 -0
- package/lib/renderers/ButtonRenderer.d.ts +83 -0
- package/lib/renderers/GenericMixedRenderer.d.ts +9 -0
- package/lib/renderers/HorizontalLayoutRenderer.d.ts +5 -0
- package/lib/renderers/ImageViewRenderer.d.ts +48 -0
- package/lib/renderers/LinkRenderer.d.ts +23 -0
- package/lib/renderers/MarkupLabelRenderer.d.ts +35 -0
- package/lib/renderers/MarkupLabelRenderer.impl.d.ts +17 -0
- package/lib/renderers/MonacoControlRenderer.d.ts +8 -0
- package/lib/renderers/MonacoControlRenderer.impl.d.ts +4 -0
- package/lib/renderers/RactiveTemplateLayoutRenderer.d.ts +6 -0
- package/lib/renderers/SeparatorRenderer.d.ts +17 -0
- package/lib/renderers/SlotRenderer.d.ts +5 -0
- package/lib/renderers/SpacerRenderer.d.ts +5 -0
- package/lib/renderers/SplitLayoutRenderer.d.ts +18 -0
- package/lib/renderers/TemplateLayoutRenderer.d.ts +13 -0
- package/lib/renderers/TemplateRenderer.d.ts +17 -0
- package/lib/renderers/TsxTemplateLayoutRenderer.d.ts +111 -0
- package/lib/renderers/index.d.ts +13 -0
- package/lib/renderers/templateEngines.d.ts +43 -0
- package/lib/util/actionContext.d.ts +23 -0
- package/lib/util/additionalErrors.d.ts +77 -0
- package/lib/util/celEnvironment.d.ts +23 -0
- package/lib/util/celTemplate.d.ts +47 -0
- package/lib/util/childNames.d.ts +46 -0
- package/lib/util/configNamespaces.d.ts +1 -0
- package/lib/util/cron.d.ts +122 -0
- package/lib/util/editorControls.d.ts +8 -0
- package/lib/util/editorDiagnostics.d.ts +56 -0
- package/lib/util/extendedControls.d.ts +4 -0
- package/lib/util/extendedLocale.d.ts +32 -0
- package/lib/util/fileFilterValue.d.ts +3 -0
- package/lib/util/formContext.d.ts +54 -0
- package/lib/util/gridColumns.d.ts +6 -0
- package/lib/util/horizontalLayout.d.ts +10 -0
- package/lib/util/i18nDefaults.d.ts +45 -0
- package/lib/util/interpolate.d.ts +105 -0
- package/lib/util/labelFilterValue.d.ts +2 -0
- package/lib/util/lazyControl.d.ts +36 -0
- package/lib/util/lazyTemplate.d.ts +18 -0
- package/lib/util/markdown.d.ts +55 -0
- package/lib/util/markdownEscape.d.ts +27 -0
- package/lib/util/markup.d.ts +74 -0
- package/lib/util/maskControls.d.ts +2 -0
- package/lib/util/templateLang.d.ts +87 -0
- package/lib/util/templateUrls.d.ts +40 -0
- package/lib/util/urlPolicy.d.ts +42 -0
- package/lib/util/useCronPicker.d.ts +24 -0
- package/lib/util/useDurationControl.d.ts +99 -0
- package/lib/util/useEditorAppearance.d.ts +21 -0
- package/lib/util/useExtendedTranslator.d.ts +10 -0
- package/lib/util/useMonacoShadowStyles.d.ts +2 -0
- package/package.json +110 -3
- package/src/components/DynamicJSXRenderer.tsx +274 -0
- package/src/components/ExtendedJsonForms.tsx +329 -0
- package/src/components/GenericAdditionalProperties.tsx +362 -0
- package/src/components/InterpolatedText.tsx +114 -0
- package/src/components/RactiveTemplate.tsx +161 -0
- package/src/components/editorRendererTypes.ts +135 -0
- package/src/components/elementRender.ts +15 -0
- package/src/core/ajv.ts +177 -0
- package/src/core/ajvI18n/bg.ts +203 -0
- package/src/core/ajvI18n/index.ts +267 -0
- package/src/core/ajvI18n/localizers.ts +25 -0
- package/src/core/dynamicDefaults.ts +170 -0
- package/src/core/keywords.ts +71 -0
- package/src/core/transform.ts +137 -0
- package/src/core/uiSchema.ts +53 -0
- package/src/index.tsx +196 -0
- package/src/locale/bg.ts +51 -0
- package/src/renderers/AgGridControlRenderer.impl.tsx +788 -0
- package/src/renderers/AgGridControlRenderer.tsx +24 -0
- package/src/renderers/ButtonRenderer.tsx +328 -0
- package/src/renderers/GenericMixedRenderer.tsx +224 -0
- package/src/renderers/HorizontalLayoutRenderer.tsx +93 -0
- package/src/renderers/ImageViewRenderer.tsx +328 -0
- package/src/renderers/LinkRenderer.tsx +92 -0
- package/src/renderers/MarkupLabelRenderer.impl.tsx +26 -0
- package/src/renderers/MarkupLabelRenderer.tsx +327 -0
- package/src/renderers/MonacoControlRenderer.impl.tsx +351 -0
- package/src/renderers/MonacoControlRenderer.tsx +24 -0
- package/src/renderers/RactiveTemplateLayoutRenderer.tsx +172 -0
- package/src/renderers/SeparatorRenderer.tsx +55 -0
- package/src/renderers/SlotRenderer.tsx +34 -0
- package/src/renderers/SpacerRenderer.tsx +69 -0
- package/src/renderers/SplitLayoutRenderer.tsx +220 -0
- package/src/renderers/TemplateLayoutRenderer.tsx +394 -0
- package/src/renderers/TemplateRenderer.tsx +71 -0
- package/src/renderers/TsxTemplateLayoutRenderer.tsx +258 -0
- package/src/renderers/index.ts +14 -0
- package/src/renderers/templateEngines.tsx +100 -0
- package/src/util/actionContext.ts +29 -0
- package/src/util/additionalErrors.tsx +391 -0
- package/src/util/celEnvironment.ts +300 -0
- package/src/util/celTemplate.ts +267 -0
- package/src/util/childNames.ts +135 -0
- package/src/util/configNamespaces.ts +6 -0
- package/src/util/cron.ts +433 -0
- package/src/util/editorControls.ts +58 -0
- package/src/util/editorDiagnostics.ts +103 -0
- package/src/util/extendedControls.ts +34 -0
- package/src/util/extendedLocale.ts +67 -0
- package/src/util/fileFilterValue.ts +28 -0
- package/src/util/formContext.ts +74 -0
- package/src/util/gridColumns.ts +80 -0
- package/src/util/horizontalLayout.ts +59 -0
- package/src/util/i18nDefaults.ts +80 -0
- package/src/util/interpolate.ts +218 -0
- package/src/util/labelFilterValue.ts +49 -0
- package/src/util/lazyControl.tsx +66 -0
- package/src/util/lazyTemplate.tsx +110 -0
- package/src/util/markdown.tsx +345 -0
- package/src/util/markdownEscape.ts +29 -0
- package/src/util/markup.ts +144 -0
- package/src/util/maskControls.ts +6 -0
- package/src/util/templateLang.ts +145 -0
- package/src/util/templateUrls.ts +274 -0
- package/src/util/urlPolicy.ts +115 -0
- package/src/util/useCronPicker.ts +102 -0
- package/src/util/useDurationControl.ts +272 -0
- package/src/util/useEditorAppearance.ts +86 -0
- package/src/util/useExtendedTranslator.ts +25 -0
- package/src/util/useMonacoShadowStyles.ts +47 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import type { Layout, UISchemaElement } from '@jsonforms/core';
|
|
2
|
+
import type { ExtendedUISchemaElement } from '../core/uiSchema';
|
|
3
|
+
import { extendedConfig } from './configNamespaces';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Which engine renders a `TemplateLayout`, and whether it is allowed to run.
|
|
7
|
+
*
|
|
8
|
+
* "optional top-level `lang` selects the engine. Resolve the language from
|
|
9
|
+
* explicit lang, then the configured default, then ractive for the
|
|
10
|
+
* default web profile. Keep that existing config name."
|
|
11
|
+
*
|
|
12
|
+
* `defaultTemplateLang` is read from `config.jsonformsExtended`.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** The engines this renderer set implements. */
|
|
16
|
+
export const TEMPLATE_LANGS = ['ractive', 'jsx'] as const;
|
|
17
|
+
export type TemplateLang = (typeof TEMPLATE_LANGS)[number];
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A `TemplateLayout` element.
|
|
21
|
+
*
|
|
22
|
+
* `lang` is **not** narrowed to {@link TemplateLang}, and that is deliberate.
|
|
23
|
+
* The specification requires an unknown language to be *diagnosed* rather than
|
|
24
|
+
* refused - "unknown or unsupported languages must be diagnosed rather than
|
|
25
|
+
* interpreted as another engine" - and it names `vue` as a profile a web
|
|
26
|
+
* renderer set need not implement. A closed union would make the diagnostic
|
|
27
|
+
* path unauthorable: `lang: "handlebars"` would not compile, so the case the
|
|
28
|
+
* renderer exists to report could never be written down.
|
|
29
|
+
*
|
|
30
|
+
* `TemplateLang | (string & {})` keeps editor completion for the two engines
|
|
31
|
+
* this set implements while still accepting any string.
|
|
32
|
+
*/
|
|
33
|
+
export type TemplateLayoutElement = Omit<Layout, 'elements'> & {
|
|
34
|
+
type: 'TemplateLayout';
|
|
35
|
+
/** The template source. Required by section 13. */
|
|
36
|
+
template: string;
|
|
37
|
+
/** `ractive`, `jsx`, or anything else - which is diagnosed, not rejected. */
|
|
38
|
+
lang?: TemplateLang | (string & NonNullable<unknown>);
|
|
39
|
+
/** Lets a registered `Template` resolve this element by name. */
|
|
40
|
+
name?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Children; a named one is addressable from the template.
|
|
43
|
+
*
|
|
44
|
+
* Section 13: "each named child is available as a partial that mounts its
|
|
45
|
+
* delegated renderer". The names come from the project's own element type,
|
|
46
|
+
* since core declares none.
|
|
47
|
+
*/
|
|
48
|
+
elements: ExtendedUISchemaElement[];
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/** "then ractive for the default web profile" */
|
|
52
|
+
const DEFAULT_TEMPLATE_LANG: TemplateLang = 'ractive';
|
|
53
|
+
|
|
54
|
+
export interface ResolvedTemplateLang {
|
|
55
|
+
/** The engine to use, or undefined when the request cannot be honoured. */
|
|
56
|
+
lang?: TemplateLang;
|
|
57
|
+
/** What was asked for, when it is not a language this set implements. */
|
|
58
|
+
requested?: string;
|
|
59
|
+
/**
|
|
60
|
+
* Why nothing will render. `unsupported` is a language this set does not
|
|
61
|
+
* implement; `evaluation-disabled` is a supported one the host has not
|
|
62
|
+
* permitted.
|
|
63
|
+
*/
|
|
64
|
+
diagnostic?: 'unsupported' | 'evaluation-disabled';
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const asLang = (value: unknown): TemplateLang | undefined =>
|
|
68
|
+
typeof value === 'string' &&
|
|
69
|
+
(TEMPLATE_LANGS as readonly string[]).includes(value.toLowerCase())
|
|
70
|
+
? (value.toLowerCase() as TemplateLang)
|
|
71
|
+
: undefined;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Whether the host permits compiling strings into executable code.
|
|
75
|
+
*
|
|
76
|
+
* "String evaluation requires
|
|
77
|
+
* `jsonformsExtended.security.allowScriptEvaluation=true`… Enabling string
|
|
78
|
+
* evaluation means the host treats the UI schema as trusted executable code."
|
|
79
|
+
*
|
|
80
|
+
* **Both** engines need it. JSX is compiled and handed to `new Function`;
|
|
81
|
+
* Ractive compiles each `{{ }}` expression the same way. Neither is a sandbox,
|
|
82
|
+
* and the default is therefore off.
|
|
83
|
+
*/
|
|
84
|
+
export const allowsScriptEvaluation = (config: unknown): boolean => {
|
|
85
|
+
const security = extendedConfig(config)?.['security'];
|
|
86
|
+
return (
|
|
87
|
+
!!security &&
|
|
88
|
+
typeof security === 'object' &&
|
|
89
|
+
(security as Record<string, unknown>)['allowScriptEvaluation'] === true
|
|
90
|
+
);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The engine for one element, or a diagnostic saying why there is none.
|
|
95
|
+
*
|
|
96
|
+
* "Unknown or unsupported languages must be diagnosed rather than interpreted
|
|
97
|
+
* as another engine" - so an unrecognized `lang` does **not** fall back to the
|
|
98
|
+
* default. Falling back is what turns a typo into a form that renders the
|
|
99
|
+
* wrong thing and says nothing.
|
|
100
|
+
*/
|
|
101
|
+
export const resolveTemplateLang = (
|
|
102
|
+
uischema: UISchemaElement | undefined,
|
|
103
|
+
config: unknown,
|
|
104
|
+
): ResolvedTemplateLang => {
|
|
105
|
+
const requested = (uischema as { lang?: unknown } | undefined)?.lang;
|
|
106
|
+
|
|
107
|
+
if (requested !== undefined) {
|
|
108
|
+
const explicit = asLang(requested);
|
|
109
|
+
return explicit
|
|
110
|
+
? { lang: explicit }
|
|
111
|
+
: { requested: String(requested), diagnostic: 'unsupported' };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const namespaced = extendedConfig(config) as
|
|
115
|
+
| { defaultTemplateLang?: unknown }
|
|
116
|
+
| undefined;
|
|
117
|
+
const configured = namespaced?.defaultTemplateLang;
|
|
118
|
+
if (configured !== undefined) {
|
|
119
|
+
const fromConfig = asLang(configured);
|
|
120
|
+
return fromConfig
|
|
121
|
+
? { lang: fromConfig }
|
|
122
|
+
: { requested: String(configured), diagnostic: 'unsupported' };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return { lang: DEFAULT_TEMPLATE_LANG };
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* The same resolution, plus the security gate, for a renderer about to run.
|
|
130
|
+
*
|
|
131
|
+
* Kept separate from {@link resolveTemplateLang} because the **testers** must
|
|
132
|
+
* not consult it: a disabled host still needs the element to select a renderer,
|
|
133
|
+
* so that renderer can report why nothing is shown. A tester returning `-1`
|
|
134
|
+
* would leave the element unhandled and silent.
|
|
135
|
+
*/
|
|
136
|
+
export const resolveTemplateEngine = (
|
|
137
|
+
uischema: UISchemaElement | undefined,
|
|
138
|
+
config: unknown,
|
|
139
|
+
): ResolvedTemplateLang => {
|
|
140
|
+
const resolved = resolveTemplateLang(uischema, config);
|
|
141
|
+
if (resolved.lang && !allowsScriptEvaluation(config)) {
|
|
142
|
+
return { ...resolved, diagnostic: 'evaluation-disabled' };
|
|
143
|
+
}
|
|
144
|
+
return resolved;
|
|
145
|
+
};
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import {
|
|
2
|
+
UrlPolicy,
|
|
3
|
+
defaultUrlPolicy,
|
|
4
|
+
isAllowedImageUrl,
|
|
5
|
+
isAllowedUrl,
|
|
6
|
+
} from './urlPolicy';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The URL policy, applied to whatever a **template** produced.
|
|
10
|
+
*
|
|
11
|
+
* Section 12 makes the policy a MUST for "every URL-bearing value", and the
|
|
12
|
+
* `Link` and `ImageView` renderers have always honoured it. A template did
|
|
13
|
+
* not: `<a href={data.url}>` put the value straight into the anchor, so a
|
|
14
|
+
* `javascript:` URL arriving in form data became a working script the moment
|
|
15
|
+
* somebody clicked it. Text interpolation was never the hole - React escapes
|
|
16
|
+
* it - which is exactly why this one survived.
|
|
17
|
+
*
|
|
18
|
+
* The check belongs **below the engine**, not in it: there are three engines
|
|
19
|
+
* and no reason for each to learn the policy separately.
|
|
20
|
+
*
|
|
21
|
+
* **A refused URL drops the attribute, not the element.** An `<a>` without an
|
|
22
|
+
* `href` is text that cannot navigate; an `<img>` without a `src` shows its
|
|
23
|
+
* `alt`. Removing the element instead would make a hostile value indistinguishable
|
|
24
|
+
* from a missing one, and section 19 asks for honest rendering of bad data.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/** Stable code, so a host can grep for it. */
|
|
28
|
+
export const URL_REFUSED_DIAGNOSTIC = 'template.urlRefused';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* How a URL-bearing attribute is judged.
|
|
32
|
+
*
|
|
33
|
+
* `image` additionally allows `data:image/...` when the host has opted in with
|
|
34
|
+
* `allowImageDataUrls`; `link` never does. The split matters: an inline image
|
|
35
|
+
* is a reasonable asset, and a `data:` document handed to an anchor is a
|
|
36
|
+
* navigation to attacker-controlled HTML on this origin.
|
|
37
|
+
*/
|
|
38
|
+
type UrlKind = 'link' | 'image';
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Attributes that carry a URL, by their **React prop** spelling.
|
|
42
|
+
*
|
|
43
|
+
* `srcSet` and `ping` hold *lists*; see {@link splitUrlList}. Deliberately
|
|
44
|
+
* absent: `data`, which is a URL only on `<object>` and an ordinary prop name
|
|
45
|
+
* everywhere else - it is handled per element below.
|
|
46
|
+
*/
|
|
47
|
+
const URL_PROPS: Record<string, UrlKind> = {
|
|
48
|
+
href: 'link',
|
|
49
|
+
src: 'link',
|
|
50
|
+
srcSet: 'image',
|
|
51
|
+
poster: 'image',
|
|
52
|
+
background: 'image',
|
|
53
|
+
action: 'link',
|
|
54
|
+
formAction: 'link',
|
|
55
|
+
cite: 'link',
|
|
56
|
+
ping: 'link',
|
|
57
|
+
manifest: 'link',
|
|
58
|
+
profile: 'link',
|
|
59
|
+
longDesc: 'link',
|
|
60
|
+
codeBase: 'link',
|
|
61
|
+
classID: 'link',
|
|
62
|
+
xlinkHref: 'link',
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/** The same set, spelled as DOM attributes, for an engine that owns its DOM. */
|
|
66
|
+
const URL_ATTRS: Record<string, UrlKind> = {
|
|
67
|
+
href: 'link',
|
|
68
|
+
src: 'link',
|
|
69
|
+
srcset: 'image',
|
|
70
|
+
poster: 'image',
|
|
71
|
+
background: 'image',
|
|
72
|
+
action: 'link',
|
|
73
|
+
formaction: 'link',
|
|
74
|
+
cite: 'link',
|
|
75
|
+
ping: 'link',
|
|
76
|
+
manifest: 'link',
|
|
77
|
+
profile: 'link',
|
|
78
|
+
longdesc: 'link',
|
|
79
|
+
codebase: 'link',
|
|
80
|
+
classid: 'link',
|
|
81
|
+
'xlink:href': 'link',
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/** Elements whose `src` loads an image, so `allowImageDataUrls` applies. */
|
|
85
|
+
const IMAGE_SRC_ELEMENTS = new Set(['img', 'image', 'source', 'input']);
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* `src` is the one attribute whose kind depends on the element.
|
|
89
|
+
*
|
|
90
|
+
* On `<img>` it loads an image, so `allowImageDataUrls` may permit a
|
|
91
|
+
* `data:image/...`. On `<iframe>`, `<embed>` or `<script>` the same attribute
|
|
92
|
+
* loads a *document*, where that opt-in must not apply - which is why the
|
|
93
|
+
* table cannot settle this on the attribute name alone.
|
|
94
|
+
*/
|
|
95
|
+
const kindFor = (type: unknown, name: string, declared: UrlKind): UrlKind => {
|
|
96
|
+
if (name !== 'src') {
|
|
97
|
+
return declared;
|
|
98
|
+
}
|
|
99
|
+
const tag = typeof type === 'string' ? type.toLowerCase() : '';
|
|
100
|
+
return IMAGE_SRC_ELEMENTS.has(tag) ? 'image' : 'link';
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const allowed = (value: unknown, kind: UrlKind, policy: UrlPolicy): boolean =>
|
|
104
|
+
kind === 'image'
|
|
105
|
+
? isAllowedImageUrl(value, policy)
|
|
106
|
+
: isAllowedUrl(value, policy);
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The candidate URLs inside a list-valued attribute.
|
|
110
|
+
*
|
|
111
|
+
* `srcSet` is comma-separated with an optional descriptor (`x.png 2x`), and
|
|
112
|
+
* `ping` is space-separated. A list is refused **as a whole** when any
|
|
113
|
+
* candidate is: keeping the survivors would silently change which image loads.
|
|
114
|
+
*/
|
|
115
|
+
const splitUrlList = (name: string, value: string): string[] =>
|
|
116
|
+
name === 'ping'
|
|
117
|
+
? value.split(/\s+/).filter(Boolean)
|
|
118
|
+
: value
|
|
119
|
+
.split(',')
|
|
120
|
+
.map((candidate) => candidate.trim().split(/\s+/)[0])
|
|
121
|
+
.filter(Boolean);
|
|
122
|
+
|
|
123
|
+
const isList = (name: string) =>
|
|
124
|
+
name === 'srcSet' || name === 'srcset' || name === 'ping';
|
|
125
|
+
|
|
126
|
+
const report = (
|
|
127
|
+
onRefused: ((message: string) => void) | undefined,
|
|
128
|
+
name: string,
|
|
129
|
+
value: unknown,
|
|
130
|
+
) => {
|
|
131
|
+
const message = `${URL_REFUSED_DIAGNOSTIC}: ${name}=${JSON.stringify(
|
|
132
|
+
String(value),
|
|
133
|
+
)} is not permitted by the URL policy; the attribute was dropped.`;
|
|
134
|
+
if (onRefused) {
|
|
135
|
+
onRefused(message);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
// oxlint-disable-next-line no-console
|
|
139
|
+
console.warn(message);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Whether a prop's value is worth judging as a URL.
|
|
144
|
+
*
|
|
145
|
+
* On an **intrinsic** element (`type` is a string like `'a'`) React writes
|
|
146
|
+
* whatever it is given into the attribute, stringifying it on the way, so
|
|
147
|
+
* anything non-null is inspected - including an object with a hostile
|
|
148
|
+
* `toString`.
|
|
149
|
+
*
|
|
150
|
+
* On a **custom component** only strings are. A component is free to define
|
|
151
|
+
* `src` as something structured, and dropping a legitimate object prop because
|
|
152
|
+
* it is not a URL would break working templates to prevent nothing: the
|
|
153
|
+
* component, not this pragma, decides what reaches the DOM - and when it does,
|
|
154
|
+
* it goes through its own element, which passes back through here.
|
|
155
|
+
*/
|
|
156
|
+
const inspectable = (type: unknown, value: unknown): boolean =>
|
|
157
|
+
typeof type === 'string' ? true : typeof value === 'string';
|
|
158
|
+
|
|
159
|
+
const permits = (
|
|
160
|
+
type: unknown,
|
|
161
|
+
name: string,
|
|
162
|
+
kind: UrlKind,
|
|
163
|
+
value: unknown,
|
|
164
|
+
policy: UrlPolicy,
|
|
165
|
+
): boolean => {
|
|
166
|
+
const effective = kindFor(type, name, kind);
|
|
167
|
+
if (isList(name) && typeof value === 'string') {
|
|
168
|
+
const candidates = splitUrlList(name, value);
|
|
169
|
+
return candidates.every((candidate) =>
|
|
170
|
+
allowed(candidate, effective, policy),
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
return allowed(value, effective, policy);
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Props with every refused URL removed.
|
|
178
|
+
*
|
|
179
|
+
* Returns the **same object** when nothing was refused, so the common path
|
|
180
|
+
* allocates nothing and React's identity checks are unaffected.
|
|
181
|
+
*/
|
|
182
|
+
export const sanitizeUrlProps = (
|
|
183
|
+
type: unknown,
|
|
184
|
+
props: Record<string, unknown> | null | undefined,
|
|
185
|
+
policy: UrlPolicy = defaultUrlPolicy,
|
|
186
|
+
onRefused?: (message: string) => void,
|
|
187
|
+
): Record<string, unknown> | null | undefined => {
|
|
188
|
+
if (!props) {
|
|
189
|
+
return props;
|
|
190
|
+
}
|
|
191
|
+
let sanitized: Record<string, unknown> | undefined;
|
|
192
|
+
for (const [name, kind] of Object.entries(URL_PROPS)) {
|
|
193
|
+
if (!Object.prototype.hasOwnProperty.call(props, name)) {
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
const value = props[name];
|
|
197
|
+
if (value === undefined || value === null) {
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
if (!inspectable(type, value)) {
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
if (permits(type, name, kind, String(value), policy)) {
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
sanitized = sanitized ?? { ...props };
|
|
207
|
+
delete sanitized[name];
|
|
208
|
+
report(onRefused, name, value);
|
|
209
|
+
}
|
|
210
|
+
/*
|
|
211
|
+
`data` is a URL on `<object>` and an ordinary prop everywhere else, so it
|
|
212
|
+
cannot live in the table above - `<Foo data={{...}} />` must not be
|
|
213
|
+
inspected as a URL.
|
|
214
|
+
*/
|
|
215
|
+
if (
|
|
216
|
+
typeof type === 'string' &&
|
|
217
|
+
type.toLowerCase() === 'object' &&
|
|
218
|
+
typeof props.data === 'string' &&
|
|
219
|
+
!isAllowedUrl(props.data, policy)
|
|
220
|
+
) {
|
|
221
|
+
sanitized = sanitized ?? { ...props };
|
|
222
|
+
delete sanitized.data;
|
|
223
|
+
report(onRefused, 'data', props.data);
|
|
224
|
+
}
|
|
225
|
+
return sanitized ?? props;
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* The same policy, applied to a DOM subtree an engine rendered itself.
|
|
230
|
+
*
|
|
231
|
+
* Ractive owns its subtree and offers no hook for a bound attribute value, so
|
|
232
|
+
* this runs immediately after it renders. That is a real difference from the
|
|
233
|
+
* JSX profile, where the check happens *before* the element exists: here the
|
|
234
|
+
* attribute is briefly present, so a `src` may already have started loading
|
|
235
|
+
* even though it is removed in the same task. `javascript:` in an `href` -
|
|
236
|
+
* the case that actually executes attacker code - needs a click, and is gone
|
|
237
|
+
* long before one can arrive.
|
|
238
|
+
*/
|
|
239
|
+
export const sanitizeUrlAttributes = (
|
|
240
|
+
root: Element | null | undefined,
|
|
241
|
+
policy: UrlPolicy = defaultUrlPolicy,
|
|
242
|
+
onRefused?: (message: string) => void,
|
|
243
|
+
): number => {
|
|
244
|
+
if (!root) {
|
|
245
|
+
return 0;
|
|
246
|
+
}
|
|
247
|
+
let removed = 0;
|
|
248
|
+
const names = Object.keys(URL_ATTRS);
|
|
249
|
+
// Only `xlink:href` needs escaping, and `CSS.escape` is not guaranteed to
|
|
250
|
+
// exist in every DOM implementation this runs in.
|
|
251
|
+
const selector = names
|
|
252
|
+
.map((name) => `[${name.replace(':', '\\:')}]`)
|
|
253
|
+
.join(',');
|
|
254
|
+
const candidates: Element[] = [
|
|
255
|
+
...(root.matches?.(selector) ? [root] : []),
|
|
256
|
+
...Array.from(root.querySelectorAll(selector)),
|
|
257
|
+
];
|
|
258
|
+
for (const element of candidates) {
|
|
259
|
+
const tag = element.tagName.toLowerCase();
|
|
260
|
+
for (const name of names) {
|
|
261
|
+
const value = element.getAttribute(name);
|
|
262
|
+
if (value === null) {
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
if (permits(tag, name, URL_ATTRS[name], value, policy)) {
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
element.removeAttribute(name);
|
|
269
|
+
removed += 1;
|
|
270
|
+
report(onRefused, name, value);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return removed;
|
|
274
|
+
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { JSONFORMS_EXTENDED_CONFIG_KEY } from './configNamespaces';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Which URLs a URL-bearing element may navigate to or load.
|
|
5
|
+
*
|
|
6
|
+
* Section 12 of the specification makes this a MUST for every URL-bearing
|
|
7
|
+
* value - `Link.href`, `ImageView.src`, Markdown links and images - and fixes
|
|
8
|
+
* the defaults below for when no configuration is supplied.
|
|
9
|
+
*/
|
|
10
|
+
export interface UrlPolicy {
|
|
11
|
+
allowedSchemes: string[];
|
|
12
|
+
allowRelative: boolean;
|
|
13
|
+
allowImageDataUrls: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const defaultUrlPolicy: UrlPolicy = {
|
|
17
|
+
allowedSchemes: ['https', 'http', 'mailto'],
|
|
18
|
+
allowRelative: true,
|
|
19
|
+
allowImageDataUrls: false,
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/** Reads `config.jsonformsExtended.security.urlPolicy`, filling in defaults. */
|
|
23
|
+
export const resolveUrlPolicy = (config: unknown): UrlPolicy => {
|
|
24
|
+
const extended = (config as Record<string, unknown> | undefined)?.[
|
|
25
|
+
JSONFORMS_EXTENDED_CONFIG_KEY
|
|
26
|
+
] as Record<string, unknown> | undefined;
|
|
27
|
+
const security = extended?.security as Record<string, unknown> | undefined;
|
|
28
|
+
const supplied = security?.urlPolicy as Partial<UrlPolicy> | undefined;
|
|
29
|
+
if (!supplied) {
|
|
30
|
+
return defaultUrlPolicy;
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
allowedSchemes: Array.isArray(supplied.allowedSchemes)
|
|
34
|
+
? supplied.allowedSchemes.map((scheme) => String(scheme).toLowerCase())
|
|
35
|
+
: defaultUrlPolicy.allowedSchemes,
|
|
36
|
+
allowRelative:
|
|
37
|
+
supplied.allowRelative === undefined
|
|
38
|
+
? defaultUrlPolicy.allowRelative
|
|
39
|
+
: supplied.allowRelative === true,
|
|
40
|
+
allowImageDataUrls:
|
|
41
|
+
supplied.allowImageDataUrls === undefined
|
|
42
|
+
? defaultUrlPolicy.allowImageDataUrls
|
|
43
|
+
: supplied.allowImageDataUrls === true,
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** `scheme:` at the start of a URL. Deliberately strict about what counts. */
|
|
48
|
+
const SCHEME = /^([a-zA-Z][a-zA-Z0-9+.-]*):/;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Whether a URL may be used, under the supplied policy.
|
|
52
|
+
*
|
|
53
|
+
* A URL with no scheme is relative and governed by `allowRelative`. A URL with
|
|
54
|
+
* a scheme must name one the policy allows - so `javascript:`, `data:` and
|
|
55
|
+
* `vbscript:` are refused by default rather than being rendered into an
|
|
56
|
+
* anchor. Protocol-relative `//host/path` counts as absolute, because it
|
|
57
|
+
* inherits the page's scheme and can leave the origin.
|
|
58
|
+
*
|
|
59
|
+
* Leading control characters and whitespace are stripped before the test:
|
|
60
|
+
* `java\\nscript:alert(1)` is a scheme, and browsers treat it as one.
|
|
61
|
+
*/
|
|
62
|
+
export const isAllowedUrl = (
|
|
63
|
+
url: unknown,
|
|
64
|
+
policy: UrlPolicy = defaultUrlPolicy,
|
|
65
|
+
): boolean => {
|
|
66
|
+
if (typeof url !== 'string') {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
// oxlint-disable-next-line no-control-regex
|
|
70
|
+
const candidate = url.replace(/[\u0000- ]/g, '');
|
|
71
|
+
if (candidate === '') {
|
|
72
|
+
// An empty href is explicitly allowed by section 13; the Link renderer
|
|
73
|
+
// gives it non-navigating semantics rather than inventing a destination.
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
if (candidate.startsWith('//')) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
const scheme = SCHEME.exec(candidate)?.[1]?.toLowerCase();
|
|
80
|
+
if (scheme === undefined) {
|
|
81
|
+
return policy.allowRelative;
|
|
82
|
+
}
|
|
83
|
+
return policy.allowedSchemes.includes(scheme);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/** `data:image/<subtype>;...` - an inline image, and nothing else. */
|
|
87
|
+
const IMAGE_DATA_URL = /^data:image\/[a-zA-Z0-9.+-]+[;,]/;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Whether a URL may be **loaded as an image**.
|
|
91
|
+
*
|
|
92
|
+
* Same policy as {@link isAllowedUrl}, plus `allowImageDataUrls`: a
|
|
93
|
+
* `data:image/...` URL is a reasonable way to inline a small asset, and is
|
|
94
|
+
* nothing like a `data:text/html` document handed to a link. The flag only
|
|
95
|
+
* opens inline *images* - a `data:` URL of any other media type stays refused
|
|
96
|
+
* whatever it is used for, so turning this on cannot become a way to smuggle
|
|
97
|
+
* a document in.
|
|
98
|
+
*
|
|
99
|
+
* It is off by default, because an inline payload bypasses whatever CSP
|
|
100
|
+
* `img-src` allow-list the host maintains.
|
|
101
|
+
*/
|
|
102
|
+
export const isAllowedImageUrl = (
|
|
103
|
+
url: unknown,
|
|
104
|
+
policy: UrlPolicy = defaultUrlPolicy,
|
|
105
|
+
): boolean => {
|
|
106
|
+
if (
|
|
107
|
+
policy.allowImageDataUrls &&
|
|
108
|
+
typeof url === 'string' &&
|
|
109
|
+
// oxlint-disable-next-line no-control-regex
|
|
110
|
+
IMAGE_DATA_URL.test(url.replace(/[\u0000- ]/g, ''))
|
|
111
|
+
) {
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
return isAllowedUrl(url, policy);
|
|
115
|
+
};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
import type { ControlProps } from '@jsonforms/core';
|
|
3
|
+
import { useI18n } from '@chobantonov/jsonforms-react-renderer-common/locale/translate';
|
|
4
|
+
import {
|
|
5
|
+
CRON_FIELDS,
|
|
6
|
+
NOTHING_CHOSEN,
|
|
7
|
+
cronProblem,
|
|
8
|
+
fieldsForPeriod,
|
|
9
|
+
joinCron,
|
|
10
|
+
periodOf,
|
|
11
|
+
parseField,
|
|
12
|
+
sameExpression,
|
|
13
|
+
splitCron,
|
|
14
|
+
standDown,
|
|
15
|
+
withTimeDefaults,
|
|
16
|
+
type CronField,
|
|
17
|
+
type CronPeriod,
|
|
18
|
+
} from './cron';
|
|
19
|
+
|
|
20
|
+
/** Keeps the cron expression, picker draft and displayed period in sync. */
|
|
21
|
+
export const useCronPicker = (props: ControlProps) => {
|
|
22
|
+
const label = useI18n();
|
|
23
|
+
const expression = typeof props.data === 'string' ? props.data : '';
|
|
24
|
+
const [open, setOpen] = useState(false);
|
|
25
|
+
const [text, setText] = useState('');
|
|
26
|
+
const [period, setPeriod] = useState<CronPeriod>();
|
|
27
|
+
const [touched, setTouched] = useState(false);
|
|
28
|
+
const options = { ...props.config, ...props.uischema.options };
|
|
29
|
+
const showActions = options.showActions !== false;
|
|
30
|
+
const disabled = !props.enabled || Boolean(props.readonly);
|
|
31
|
+
const problem =
|
|
32
|
+
expression.trim() === '' ? undefined : cronProblem(expression);
|
|
33
|
+
const localError = problem ? label(`cron.error.${problem}` as any) : '';
|
|
34
|
+
const commit = (next: string) => {
|
|
35
|
+
if (disabled || sameExpression(next, expression)) return;
|
|
36
|
+
props.handleChange(props.path, next);
|
|
37
|
+
};
|
|
38
|
+
const draft = splitCron(text) ?? NOTHING_CHOSEN;
|
|
39
|
+
const change = (next: string) => {
|
|
40
|
+
setText(next);
|
|
41
|
+
setTouched(true);
|
|
42
|
+
if (!showActions) commit(next);
|
|
43
|
+
};
|
|
44
|
+
const changeField = (field: CronField, value: string) => {
|
|
45
|
+
const next = [...draft];
|
|
46
|
+
const chosen = value.trim() !== '' && value !== '*';
|
|
47
|
+
next[CRON_FIELDS.indexOf(field)] = chosen ? value : '*';
|
|
48
|
+
change(joinCron(chosen ? withTimeDefaults(next, field) : next));
|
|
49
|
+
};
|
|
50
|
+
const changeText = (next: string) => {
|
|
51
|
+
const fields = splitCron(next);
|
|
52
|
+
if (fields) setPeriod(periodOf(fields));
|
|
53
|
+
change(next);
|
|
54
|
+
};
|
|
55
|
+
const changePeriod = (next: CronPeriod) => {
|
|
56
|
+
if (disabled || next === period) return;
|
|
57
|
+
setPeriod(next);
|
|
58
|
+
if (next === 'second') change(joinCron(NOTHING_CHOSEN));
|
|
59
|
+
else if (splitCron(text)) change(joinCron(standDown(draft, next)));
|
|
60
|
+
};
|
|
61
|
+
const editedProblem = text.trim() === '' ? undefined : cronProblem(text);
|
|
62
|
+
const shownFields = period ? fieldsForPeriod(period) : [];
|
|
63
|
+
const openOn = (next: boolean) => {
|
|
64
|
+
if (disabled) return;
|
|
65
|
+
if (next) {
|
|
66
|
+
const fields = splitCron(expression);
|
|
67
|
+
setText(expression);
|
|
68
|
+
setPeriod(fields ? periodOf(fields) : undefined);
|
|
69
|
+
setTouched(false);
|
|
70
|
+
}
|
|
71
|
+
setOpen(next);
|
|
72
|
+
};
|
|
73
|
+
const accept = () => {
|
|
74
|
+
if (touched) commit(text);
|
|
75
|
+
setOpen(false);
|
|
76
|
+
};
|
|
77
|
+
return {
|
|
78
|
+
label,
|
|
79
|
+
expression,
|
|
80
|
+
open,
|
|
81
|
+
setOpen,
|
|
82
|
+
text,
|
|
83
|
+
period,
|
|
84
|
+
options,
|
|
85
|
+
showActions,
|
|
86
|
+
disabled,
|
|
87
|
+
localError,
|
|
88
|
+
draft,
|
|
89
|
+
changeField,
|
|
90
|
+
changeText,
|
|
91
|
+
changePeriod,
|
|
92
|
+
editedProblem,
|
|
93
|
+
shownFields,
|
|
94
|
+
hasCustomField: shownFields.some(
|
|
95
|
+
(field) =>
|
|
96
|
+
parseField(draft[CRON_FIELDS.indexOf(field)] ?? '*', field) ===
|
|
97
|
+
undefined,
|
|
98
|
+
),
|
|
99
|
+
openOn,
|
|
100
|
+
accept,
|
|
101
|
+
};
|
|
102
|
+
};
|