@wcstack/fetch 2.1.1 → 2.2.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.ja.md +17 -0
- package/README.md +17 -0
- package/dist/auto.min.js +1 -1
- package/dist/auto.min.js.map +1 -1
- package/dist/index.d.ts +33 -2
- package/dist/index.esm.js +114 -2
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
package/dist/index.esm.js
CHANGED
|
@@ -920,6 +920,114 @@ function upgradeProperties(element) {
|
|
|
920
920
|
}
|
|
921
921
|
}
|
|
922
922
|
|
|
923
|
+
/**
|
|
924
|
+
* Trusted Types (`require-trusted-types-for 'script'`) 対応。正本は docs/csp.md §7。
|
|
925
|
+
*
|
|
926
|
+
* `<wcs-fetch target="...">` の HTML 置換モードが innerHTML に流すのは **レスポンス**
|
|
927
|
+
* = TT がまさに止めたい値そのもの。ここに identity policy を噛ませて通すのは
|
|
928
|
+
* 「TT 対応」ではなく「TT の無効化」なので、fetch は自前の policy を作らない。
|
|
929
|
+
* Angular が DomSanitizer を必須にしているのと同じ立て付けで、利用側が sanitizer を
|
|
930
|
+
* 持つ policy を注入したときだけ通す。
|
|
931
|
+
*
|
|
932
|
+
* 注入口は全 @wcstack パッケージ共通のグローバルスロット。buildless(CDN 一発)でも
|
|
933
|
+
* inline script 1 本で差し込める:
|
|
934
|
+
*
|
|
935
|
+
* ```js
|
|
936
|
+
* globalThis[Symbol.for("wcstack.trustedTypes.policy")] =
|
|
937
|
+
* trustedTypes.createPolicy("my-app", { createHTML: (s) => DOMPurify.sanitize(s) });
|
|
938
|
+
* ```
|
|
939
|
+
*
|
|
940
|
+
* policy があるときは TT 非対応ブラウザでも通す。sanitizer が Chromium でだけ効いて
|
|
941
|
+
* Firefox では素通し、という差が出るほうが危ないため。
|
|
942
|
+
*/
|
|
943
|
+
/** 利用側が policy を差し込むグローバルスロット(全 @wcstack パッケージ共通)。 */
|
|
944
|
+
const TRUSTED_TYPES_POLICY_SLOT = Symbol.for("wcstack.trustedTypes.policy");
|
|
945
|
+
/** 利用側が注入した policy。fetch は identity policy にフォールバックしない。 */
|
|
946
|
+
function getTrustedTypesPolicy() {
|
|
947
|
+
const value = globalThis[TRUSTED_TYPES_POLICY_SLOT];
|
|
948
|
+
if (value === null || typeof value !== "object")
|
|
949
|
+
return null;
|
|
950
|
+
return value;
|
|
951
|
+
}
|
|
952
|
+
/** 利用側 policy を設定する(`null` で解除)。 */
|
|
953
|
+
function setTrustedTypesPolicy(policy) {
|
|
954
|
+
globalThis[TRUSTED_TYPES_POLICY_SLOT] = policy;
|
|
955
|
+
}
|
|
956
|
+
/**
|
|
957
|
+
* レスポンスを利用側 policy に通す。policy が無ければ素通し= TT 有効下では
|
|
958
|
+
* ブラウザが弾く(意図どおり)。
|
|
959
|
+
*/
|
|
960
|
+
function trustHtmlValue(value) {
|
|
961
|
+
if (typeof value !== "string")
|
|
962
|
+
return value;
|
|
963
|
+
const policy = getTrustedTypesPolicy();
|
|
964
|
+
const createHTML = policy?.createHTML;
|
|
965
|
+
if (typeof createHTML !== "function")
|
|
966
|
+
return value;
|
|
967
|
+
return createHTML.call(policy, value);
|
|
968
|
+
}
|
|
969
|
+
let _enforced = undefined;
|
|
970
|
+
let _reported = false;
|
|
971
|
+
/**
|
|
972
|
+
* TT が実際に強制されているかを実測する。エラーメッセージの文言に依存しないよう、
|
|
973
|
+
* 使い捨ての要素へ実際に書いて確かめる。`default` policy がある場合は書き込みが
|
|
974
|
+
* 通る=我々の書き込みも通るので、正しく false になる。
|
|
975
|
+
*/
|
|
976
|
+
function isTrustedTypesEnforced() {
|
|
977
|
+
if (_enforced !== undefined)
|
|
978
|
+
return _enforced;
|
|
979
|
+
if (!("trustedTypes" in globalThis)) {
|
|
980
|
+
_enforced = false;
|
|
981
|
+
return _enforced;
|
|
982
|
+
}
|
|
983
|
+
try {
|
|
984
|
+
document.createElement("div").innerHTML = "<i></i>";
|
|
985
|
+
_enforced = false;
|
|
986
|
+
}
|
|
987
|
+
catch {
|
|
988
|
+
_enforced = true;
|
|
989
|
+
}
|
|
990
|
+
return _enforced;
|
|
991
|
+
}
|
|
992
|
+
// 呼び出し元(writeTargetHTML)が isTrustedTypesEnforced() を確認してから呼ぶ。
|
|
993
|
+
function reportTrustedTypesBlock() {
|
|
994
|
+
if (_reported)
|
|
995
|
+
return;
|
|
996
|
+
_reported = true;
|
|
997
|
+
const hasPolicy = typeof getTrustedTypesPolicy()?.createHTML === "function";
|
|
998
|
+
const cause = hasPolicy
|
|
999
|
+
? "The injected policy's createHTML() did not return a TrustedHTML."
|
|
1000
|
+
: "No sanitizing policy is installed, and @wcstack/fetch deliberately does not pass "
|
|
1001
|
+
+ "responses through an identity policy — that would defeat the CSP.";
|
|
1002
|
+
console.error(`[@wcstack/fetch] The "target" HTML replace mode was blocked by Trusted Types `
|
|
1003
|
+
+ `(require-trusted-types-for 'script'). ${cause}\n`
|
|
1004
|
+
+ `Install a sanitizing policy before the first fetch:\n`
|
|
1005
|
+
+ ` globalThis[Symbol.for("wcstack.trustedTypes.policy")] =\n`
|
|
1006
|
+
+ ` trustedTypes.createPolicy("my-app", { createHTML: (s) => DOMPurify.sanitize(s) });\n`
|
|
1007
|
+
+ `Or drop "target" and bind the response through @wcstack/state. See docs/csp.md section 7.`);
|
|
1008
|
+
}
|
|
1009
|
+
/**
|
|
1010
|
+
* HTML 置換モードの書き込み。
|
|
1011
|
+
*
|
|
1012
|
+
* TT に弾かれたときは直し方を 1 度だけ報告して**投げ返さない**(never-throw §3.6)。
|
|
1013
|
+
* 自動 fetch 経路(connect / url 変更 / trigger)は `fetch()` の rejection を受け取る先が
|
|
1014
|
+
* 無く、投げ返すと未処理 rejection として page error になる — 実 Chromium の e2e で
|
|
1015
|
+
* 確認した。報告が信号であり、黙って握り潰すのとは違う。TT 以外の失敗(innerHTML の
|
|
1016
|
+
* setter が別の理由で throw した等)は従来どおり呼び出し元へ投げ返す。
|
|
1017
|
+
*/
|
|
1018
|
+
function writeTargetHTML(element, html) {
|
|
1019
|
+
try {
|
|
1020
|
+
element.innerHTML = trustHtmlValue(html);
|
|
1021
|
+
}
|
|
1022
|
+
catch (error) {
|
|
1023
|
+
if (isTrustedTypesEnforced()) {
|
|
1024
|
+
reportTrustedTypesBlock();
|
|
1025
|
+
return;
|
|
1026
|
+
}
|
|
1027
|
+
throw error;
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
|
|
923
1031
|
class Fetch extends HTMLElement {
|
|
924
1032
|
static hasConnectedCallbackPromise = true;
|
|
925
1033
|
static wcBindable = {
|
|
@@ -1268,10 +1376,14 @@ class Fetch extends HTMLElement {
|
|
|
1268
1376
|
// This is an opt-in convenience for trusted fragments only; the primary,
|
|
1269
1377
|
// recommended path is state-driven binding via @wcstack/state. Do not point
|
|
1270
1378
|
// `target` at an untrusted endpoint (XSS risk). See README "HTML Replace Mode".
|
|
1379
|
+
//
|
|
1380
|
+
// Under Trusted Types this is the one sink in wcstack whose input is remote
|
|
1381
|
+
// data, so it is routed through an *adopter-supplied* sanitizing policy only —
|
|
1382
|
+
// fetch never signs the response with an identity policy (docs/csp.md §7).
|
|
1271
1383
|
if (this.target && result !== null) {
|
|
1272
1384
|
const targetElement = document.getElementById(this.target);
|
|
1273
1385
|
if (targetElement) {
|
|
1274
|
-
targetElement
|
|
1386
|
+
writeTargetHTML(targetElement, result);
|
|
1275
1387
|
}
|
|
1276
1388
|
}
|
|
1277
1389
|
return result;
|
|
@@ -1491,5 +1603,5 @@ function bootstrapFetch(userConfig, registry) {
|
|
|
1491
1603
|
registerComponents(registry);
|
|
1492
1604
|
}
|
|
1493
1605
|
|
|
1494
|
-
export { FetchCore, WCS_FETCH_ERROR_CODE, Fetch as WcsFetch, InfiniteScroll as WcsInfiniteScroll, bootstrapFetch, getConfig };
|
|
1606
|
+
export { FetchCore, TRUSTED_TYPES_POLICY_SLOT, WCS_FETCH_ERROR_CODE, Fetch as WcsFetch, InfiniteScroll as WcsInfiniteScroll, bootstrapFetch, getConfig, getTrustedTypesPolicy, setTrustedTypesPolicy };
|
|
1495
1607
|
//# sourceMappingURL=index.esm.js.map
|