pds-dev-kit-web 2.2.320-trialguard.0 → 2.2.320-trialguard.1

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.
@@ -5,10 +5,15 @@ interface EmbedCoreProps {
5
5
  style: CSSProperties;
6
6
  }
7
7
  /**
8
- * 판매자가 넣은 Embed 코드를 회원 사이트와 분리된 창에서 실행한다.
9
- * - srcDoc + sandbox에서 allow-same-origin을 빼서 창의 출처를 회원 사이트와 다르게(opaque origin) 만든다.
10
- * - 코드는 그대로 실행되지만 바깥 사이트의 화면(안내 배너 등), 쿠키, 저장소에는 접근할 수 없다.
11
- * - 대신 Embed 코드 안에서 쿠키·localStorage를 쓰는 부분은 동작하지 않는다.
8
+ * 판매자가 넣은 Embed 코드를 회원 사이트의 화면(안내 배너 등)·쿠키·저장소에 닿지 않게 그린다.
9
+ *
10
+ * 1) iframe(다른 사이트 띄우기)만 있는 코드
11
+ * - 그 iframe을 직접 그린다. 다른 출처의 창이라 회원 사이트에 접근할 수 없고,
12
+ * 안쪽 사이트는 격리 전과 같은 sandbox 조건에서 쿠키·저장소를 쓸 수 있다.
13
+ * - http(s) 주소만, 회원 사이트 자기 주소는 제외한다(자기 사이트는 2번으로 보낸다).
14
+ * 2) 그 밖의 코드(스크립트 등)
15
+ * - srcDoc + allow-same-origin 없는 sandbox로 출처가 다른 격리 창(opaque origin)에서 실행한다.
16
+ * - 이 창 안에서는 쿠키·localStorage를 쓰는 부분과, 그것이 필요한 안쪽 사이트가 동작하지 않는다.
12
17
  */
13
18
  declare const EmbedIframe: React.FC<EmbedCoreProps>;
14
19
  export default EmbedIframe;
@@ -11,27 +11,125 @@ var jsx_runtime_1 = require("react/jsx-runtime");
11
11
  var react_1 = require("react");
12
12
  var styled_components_1 = __importDefault(require("styled-components"));
13
13
  /**
14
- * 판매자가 넣은 Embed 코드를 회원 사이트와 분리된 창에서 실행한다.
15
- * - srcDoc + sandbox에서 allow-same-origin을 빼서 창의 출처를 회원 사이트와 다르게(opaque origin) 만든다.
16
- * - 코드는 그대로 실행되지만 바깥 사이트의 화면(안내 배너 등), 쿠키, 저장소에는 접근할 수 없다.
17
- * - 대신 Embed 코드 안에서 쿠키·localStorage를 쓰는 부분은 동작하지 않는다.
14
+ * 격리 전 Embed에 쓰던 sandbox 값. 안쪽에 넣은 다른 사이트는 이 값을 그대로 물려받아 동작했다.
15
+ * iframe만 있는 Embed를 직접 그릴 때 같은 값을 걸어 격리 전과 같은 조건으로 띄운다.
16
+ */
17
+ var DIRECT_IFRAME_SANDBOX = 'allow-forms allow-modals allow-same-origin allow-popups allow-presentation allow-scripts allow-downloads allow-pointer-lock';
18
+ /** 스크립트가 든 Embed를 실행하는 격리 창의 sandbox 값(allow-same-origin 없음). */
19
+ var ISOLATED_SANDBOX = 'allow-forms allow-modals allow-popups allow-presentation allow-scripts allow-downloads allow-pointer-lock';
20
+ /** 판매자 iframe에서 옮겨 오는 속성. 여기 없는 속성(srcdoc, on…, name 등)은 버린다. */
21
+ var DIRECT_IFRAME_ATTRS = [
22
+ 'src',
23
+ 'width',
24
+ 'height',
25
+ 'style',
26
+ 'title',
27
+ 'allow',
28
+ 'allowfullscreen',
29
+ 'frameborder',
30
+ 'scrolling',
31
+ 'referrerpolicy',
32
+ 'loading'
33
+ ];
34
+ /**
35
+ * 판매자가 넣은 Embed 코드를 회원 사이트의 화면(안내 배너 등)·쿠키·저장소에 닿지 않게 그린다.
36
+ *
37
+ * 1) iframe(다른 사이트 띄우기)만 있는 코드
38
+ * - 그 iframe을 직접 그린다. 다른 출처의 창이라 회원 사이트에 접근할 수 없고,
39
+ * 안쪽 사이트는 격리 전과 같은 sandbox 조건에서 쿠키·저장소를 쓸 수 있다.
40
+ * - http(s) 주소만, 회원 사이트 자기 주소는 제외한다(자기 사이트는 2번으로 보낸다).
41
+ * 2) 그 밖의 코드(스크립트 등)
42
+ * - srcDoc + allow-same-origin 없는 sandbox로 출처가 다른 격리 창(opaque origin)에서 실행한다.
43
+ * - 이 창 안에서는 쿠키·localStorage를 쓰는 부분과, 그것이 필요한 안쪽 사이트가 동작하지 않는다.
18
44
  */
19
45
  var EmbedIframe = function (_a) {
20
46
  var embedCode = _a.embedCode, style = _a.style;
47
+ var directIframes = (0, react_1.useMemo)(function () { return getDirectIframes(embedCode); }, [embedCode]);
21
48
  var embedHtml = (0, react_1.useMemo)(function () {
22
- return getHtmlCode(embedCode, style);
23
- }, [embedCode, style]);
24
- return ((0, jsx_runtime_1.jsx)(S_Iframe, { title: "cb-embed", srcDoc: embedHtml, allow: "accelerometer; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; clipboard-write;", sandbox: "allow-forms allow-modals allow-popups allow-presentation allow-scripts allow-downloads allow-pointer-lock" }));
49
+ return directIframes ? '' : getHtmlCode(embedCode, style);
50
+ }, [directIframes, embedCode, style]);
51
+ if (directIframes) {
52
+ return (0, jsx_runtime_1.jsx)(DirectIframes, { iframes: directIframes, style: style });
53
+ }
54
+ return ((0, jsx_runtime_1.jsx)(S_Iframe, { title: "cb-embed", srcDoc: embedHtml, allow: "accelerometer; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; clipboard-write;", sandbox: ISOLATED_SANDBOX }));
55
+ };
56
+ var DirectIframes = function (_a) {
57
+ var iframes = _a.iframes, style = _a.style;
58
+ var containerRef = (0, react_1.useRef)(null);
59
+ (0, react_1.useLayoutEffect)(function () {
60
+ var container = containerRef.current;
61
+ if (!container) {
62
+ return;
63
+ }
64
+ var elements = iframes.map(function (attrs) {
65
+ var iframe = document.createElement('iframe');
66
+ // sandbox는 주소를 불러오기 전에 걸어야 적용된다.
67
+ iframe.setAttribute('sandbox', DIRECT_IFRAME_SANDBOX);
68
+ attrs.forEach(function (_a) {
69
+ var name = _a[0], value = _a[1];
70
+ return iframe.setAttribute(name, value);
71
+ });
72
+ return iframe;
73
+ });
74
+ container.replaceChildren.apply(container, elements);
75
+ return function () {
76
+ container.replaceChildren();
77
+ };
78
+ }, [iframes]);
79
+ return (0, jsx_runtime_1.jsx)(S_DirectContainer, { ref: containerRef, style: style });
25
80
  };
81
+ function getDirectIframes(code) {
82
+ if (!code || typeof DOMParser === 'undefined') {
83
+ return null;
84
+ }
85
+ var _a = new DOMParser().parseFromString(code, 'text/html'), body = _a.body, head = _a.head;
86
+ if (head.childNodes.length > 0) {
87
+ return null;
88
+ }
89
+ // 주석과 공백만 건너뛰고, 나머지는 모두 iframe이어야 한다.
90
+ var nodes = Array.from(body.childNodes).filter(function (node) {
91
+ return node.nodeType !== Node.COMMENT_NODE &&
92
+ !(node.nodeType === Node.TEXT_NODE && !(node.textContent || '').trim());
93
+ });
94
+ var isIframeOnly = nodes.length > 0 &&
95
+ nodes.every(function (node) {
96
+ return node instanceof HTMLIFrameElement &&
97
+ node.childNodes.length === 0 &&
98
+ isAllowedDirectSrc(node.getAttribute('src') || '');
99
+ });
100
+ if (!isIframeOnly) {
101
+ return null;
102
+ }
103
+ return nodes.map(function (node) {
104
+ return DIRECT_IFRAME_ATTRS.filter(function (name) { return node.hasAttribute(name); }).map(function (name) { return [
105
+ name,
106
+ node.getAttribute(name) || ''
107
+ ]; });
108
+ });
109
+ }
110
+ function isAllowedDirectSrc(src) {
111
+ try {
112
+ var url = new URL(src.trim());
113
+ if (url.protocol !== 'https:' && url.protocol !== 'http:') {
114
+ return false;
115
+ }
116
+ return url.origin !== window.location.origin;
117
+ }
118
+ catch (_a) {
119
+ return false;
120
+ }
121
+ }
26
122
  var S_Iframe = styled_components_1.default.iframe(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n border: none;\n display: block;\n height: 100%;\n position: absolute;\n width: 100%;\n"], ["\n border: none;\n display: block;\n height: 100%;\n position: absolute;\n width: 100%;\n"])));
123
+ // 격리 창의 html/body와 같은 역할. 판매자 iframe 스타일(position: fixed 등)이 이 칸 밖으로 나가지 않게 가둔다.
124
+ var S_DirectContainer = styled_components_1.default.div(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n box-sizing: border-box;\n contain: layout paint;\n height: 100%;\n left: 0;\n margin: 0;\n overflow: auto;\n padding: 0;\n position: absolute;\n top: 0;\n width: 100%;\n"], ["\n box-sizing: border-box;\n contain: layout paint;\n height: 100%;\n left: 0;\n margin: 0;\n overflow: auto;\n padding: 0;\n position: absolute;\n top: 0;\n width: 100%;\n"])));
27
125
  function getHtmlCode(code, style) {
28
126
  var styleString = Object.entries(style).reduce(function (styleStr, _a) {
29
127
  var prop = _a[0], value = _a[1];
30
128
  var kebabProp = prop.replace(/([a-z0])([A-Z])/g, '$1-$2').toLowerCase();
31
129
  return "".concat(styleStr).concat(kebabProp, ": ").concat(value, ";");
32
130
  }, '');
33
- var html = "\n <!DOCTYPE html>\n <html lang=\"en\">\n <head>\n <base href=\"https://example.com/\">\n <meta charset=\"UTF-8\">\n <style>\n html, body {\n margin: 0;\n padding: 0;\n width: 100%;\n height: 100%;\n ".concat(styleString, "\n }\n\n body > div { \n flex-grow: 1;\n width: 100%;\n }\n </style>\n </head>\n <body>\n ").concat(code, "\n </body>\n </html>\n ");
131
+ var html = "\n <!DOCTYPE html>\n <html lang=\"en\">\n <head>\n <base href=\"https://example.com/\">\n <meta charset=\"UTF-8\">\n <style>\n html, body {\n margin: 0;\n padding: 0;\n width: 100%;\n height: 100%;\n ".concat(styleString, "\n }\n\n body > div {\n flex-grow: 1;\n width: 100%;\n }\n </style>\n </head>\n <body>\n ").concat(code, "\n </body>\n </html>\n ");
34
132
  return html;
35
133
  }
36
134
  exports.default = EmbedIframe;
37
- var templateObject_1;
135
+ var templateObject_1, templateObject_2;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pds-dev-kit-web",
3
- "version": "2.2.320-trialguard.0",
3
+ "version": "2.2.320-trialguard.1",
4
4
  "license": "MIT",
5
5
  "private": false,
6
6
  "main": "dist/index.js",