Skip to content

PasteFerry (隔空贴): clipboard and file sync across macOS, Windows and Linux. Direct-first, end-to-end encrypted. Optional official access: no server to host. Independent self-hosting remains available.

fix(site): enforce feedback CSP before executable scripts

baiyuzeb4dfe741ccc3
diff --git a/docs/assets/platform.js b/docs/assets/platform.js
new file mode 100644
index 0000000..7bbb196
--- /dev/null
+++ b/docs/assets/platform.js
@@ -0,0 +1,12 @@
+'use strict';
+(function () {
+ if (location.hostname !== 'baiyuze.github.io') return;
+ var prefix = /^\/(?:pasteferry|copysync)(?=\/|$)/.exec(location.pathname);
+ if (!prefix) return;
+ var path = location.pathname.slice(prefix[0].length) || '/';
+ location.replace('https://pasteferry.cc' + path + location.search + location.hash);
+})();
+document.documentElement.classList.add('js');
+if (!/Mac|iPhone|iPad/.test(navigator.platform || navigator.userAgent)) {
+ document.documentElement.classList.add('pc');
+}
diff --git a/docs/en/feedback/index.html b/docs/en/feedback/index.html
index 6f6471c..b649918 100644
--- a/docs/en/feedback/index.html
+++ b/docs/en/feedback/index.html
@@ -1,16 +1,8 @@
<!doctype html>
<html lang="en">
<head>
+ <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'none'; form-action 'self'">
<meta charset="utf-8">
- <script>
- (function () {
- if (location.hostname !== "baiyuze.github.io") return;
- var prefix = /^\/(?:pasteferry|copysync)(?=\/|$)/.exec(location.pathname);
- if (!prefix) return;
- var path = location.pathname.slice(prefix[0].length) || "/";
- location.replace("https://pasteferry.cc" + path + location.search + location.hash);
- })();
- </script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Discussion & feedback — PasteFerry</title>
<meta name="description" content="Report a problem, suggest a feature, or share your experience.">
@@ -33,10 +25,9 @@
<link rel="icon" href="../../assets/favicon-32.png?v=8e9afa506d" sizes="32x32" type="image/png">
<link rel="apple-touch-icon" href="../../assets/apple-touch-icon.png?v=bcbf406cb8">
<link rel="stylesheet" href="../../assets/site.css?v=dbae55d27c">
- <script>document.documentElement.classList.add("js");if (!/Mac|iPhone|iPad/.test(navigator.platform || navigator.userAgent)) document.documentElement.classList.add("pc");</script>
+ <script src="../../assets/platform.js?v=831bd90379"></script>
<link rel="stylesheet" href="../../assets/feedback.css?v=921fafb422">
<meta name="referrer" content="no-referrer">
- <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'none'; form-action 'self'">
</head>
<body>
<a class="skip" href="#main">Skip to content</a>
diff --git a/docs/feedback/index.html b/docs/feedback/index.html
index ca27971..120e194 100644
--- a/docs/feedback/index.html
+++ b/docs/feedback/index.html
@@ -1,16 +1,8 @@
<!doctype html>
<html lang="zh-CN">
<head>
+ <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'none'; form-action 'self'">
<meta charset="utf-8">
- <script>
- (function () {
- if (location.hostname !== "baiyuze.github.io") return;
- var prefix = /^\/(?:pasteferry|copysync)(?=\/|$)/.exec(location.pathname);
- if (!prefix) return;
- var path = location.pathname.slice(prefix[0].length) || "/";
- location.replace("https://pasteferry.cc" + path + location.search + location.hash);
- })();
- </script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>讨论与反馈 — PasteFerry</title>
<meta name="description" content="反馈问题、提出建议,或分享你的使用经验。">
@@ -33,10 +25,9 @@
<link rel="icon" href="../assets/favicon-32.png?v=8e9afa506d" sizes="32x32" type="image/png">
<link rel="apple-touch-icon" href="../assets/apple-touch-icon.png?v=bcbf406cb8">
<link rel="stylesheet" href="../assets/site.css?v=dbae55d27c">
- <script>document.documentElement.classList.add("js");if (!/Mac|iPhone|iPad/.test(navigator.platform || navigator.userAgent)) document.documentElement.classList.add("pc");</script>
+ <script src="../assets/platform.js?v=831bd90379"></script>
<link rel="stylesheet" href="../assets/feedback.css?v=921fafb422">
<meta name="referrer" content="no-referrer">
- <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'none'; form-action 'self'">
</head>
<body>
<a class="skip" href="#main">跳到正文</a>
diff --git a/docs/ja/feedback/index.html b/docs/ja/feedback/index.html
index 6899b3c..7cccbe8 100644
--- a/docs/ja/feedback/index.html
+++ b/docs/ja/feedback/index.html
@@ -1,16 +1,8 @@
<!doctype html>
<html lang="ja">
<head>
+ <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'none'; form-action 'self'">
<meta charset="utf-8">
- <script>
- (function () {
- if (location.hostname !== "baiyuze.github.io") return;
- var prefix = /^\/(?:pasteferry|copysync)(?=\/|$)/.exec(location.pathname);
- if (!prefix) return;
- var path = location.pathname.slice(prefix[0].length) || "/";
- location.replace("https://pasteferry.cc" + path + location.search + location.hash);
- })();
- </script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ディスカッションとフィードバック — PasteFerry</title>
<meta name="description" content="不具合の報告、機能の提案、使い方の共有はこちら。">
@@ -33,10 +25,9 @@
<link rel="icon" href="../../assets/favicon-32.png?v=8e9afa506d" sizes="32x32" type="image/png">
<link rel="apple-touch-icon" href="../../assets/apple-touch-icon.png?v=bcbf406cb8">
<link rel="stylesheet" href="../../assets/site.css?v=dbae55d27c">
- <script>document.documentElement.classList.add("js");if (!/Mac|iPhone|iPad/.test(navigator.platform || navigator.userAgent)) document.documentElement.classList.add("pc");</script>
+ <script src="../../assets/platform.js?v=831bd90379"></script>
<link rel="stylesheet" href="../../assets/feedback.css?v=921fafb422">
<meta name="referrer" content="no-referrer">
- <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'none'; form-action 'self'">
</head>
<body>
<a class="skip" href="#main">本文へ移動</a>
diff --git a/docs/zh-hk/feedback/index.html b/docs/zh-hk/feedback/index.html
index 85adaf1..e9f5956 100644
--- a/docs/zh-hk/feedback/index.html
+++ b/docs/zh-hk/feedback/index.html
@@ -1,16 +1,8 @@
<!doctype html>
<html lang="zh-HK">
<head>
+ <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'none'; form-action 'self'">
<meta charset="utf-8">
- <script>
- (function () {
- if (location.hostname !== "baiyuze.github.io") return;
- var prefix = /^\/(?:pasteferry|copysync)(?=\/|$)/.exec(location.pathname);
- if (!prefix) return;
- var path = location.pathname.slice(prefix[0].length) || "/";
- location.replace("https://pasteferry.cc" + path + location.search + location.hash);
- })();
- </script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>討論與意見 — PasteFerry</title>
<meta name="description" content="回報問題、提出建議,或分享你的使用經驗。">
@@ -33,10 +25,9 @@
<link rel="icon" href="../../assets/favicon-32.png?v=8e9afa506d" sizes="32x32" type="image/png">
<link rel="apple-touch-icon" href="../../assets/apple-touch-icon.png?v=bcbf406cb8">
<link rel="stylesheet" href="../../assets/site.css?v=dbae55d27c">
- <script>document.documentElement.classList.add("js");if (!/Mac|iPhone|iPad/.test(navigator.platform || navigator.userAgent)) document.documentElement.classList.add("pc");</script>
+ <script src="../../assets/platform.js?v=831bd90379"></script>
<link rel="stylesheet" href="../../assets/feedback.css?v=921fafb422">
<meta name="referrer" content="no-referrer">
- <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'none'; form-action 'self'">
</head>
<body>
<a class="skip" href="#main">跳至主要內容</a>
diff --git a/docs/zh-tw/feedback/index.html b/docs/zh-tw/feedback/index.html
index 283d2b8..9170716 100644
--- a/docs/zh-tw/feedback/index.html
+++ b/docs/zh-tw/feedback/index.html
@@ -1,16 +1,8 @@
<!doctype html>
<html lang="zh-TW">
<head>
+ <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'none'; form-action 'self'">
<meta charset="utf-8">
- <script>
- (function () {
- if (location.hostname !== "baiyuze.github.io") return;
- var prefix = /^\/(?:pasteferry|copysync)(?=\/|$)/.exec(location.pathname);
- if (!prefix) return;
- var path = location.pathname.slice(prefix[0].length) || "/";
- location.replace("https://pasteferry.cc" + path + location.search + location.hash);
- })();
- </script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>討論與回饋 — PasteFerry</title>
<meta name="description" content="回報問題、提出建議,或分享你的使用經驗。">
@@ -33,10 +25,9 @@
<link rel="icon" href="../../assets/favicon-32.png?v=8e9afa506d" sizes="32x32" type="image/png">
<link rel="apple-touch-icon" href="../../assets/apple-touch-icon.png?v=bcbf406cb8">
<link rel="stylesheet" href="../../assets/site.css?v=dbae55d27c">
- <script>document.documentElement.classList.add("js");if (!/Mac|iPhone|iPad/.test(navigator.platform || navigator.userAgent)) document.documentElement.classList.add("pc");</script>
+ <script src="../../assets/platform.js?v=831bd90379"></script>
<link rel="stylesheet" href="../../assets/feedback.css?v=921fafb422">
<meta name="referrer" content="no-referrer">
- <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'none'; form-action 'self'">
</head>
<body>
<a class="skip" href="#main">跳到主要內容</a>
diff --git a/tools/site/build.py b/tools/site/build.py
index 0c50fc3..1b7e9d0 100644
--- a/tools/site/build.py
+++ b/tools/site/build.py
@@ -2834,8 +2834,11 @@ def write(path: Path, text: str) -> None:
chinese = bool(re.search(r'<html[^>]+lang="zh(?:-CN|-TW|-HK)?"', text))
download_base = GITEE_DL if chinese else f"{REPO}/releases/download/v{VERSION}/"
text = text.replace(DL, download_base)
- text = text.replace(' <meta charset="utf-8">\n',
- ' <meta charset="utf-8">\n' + GITHUB_PAGES_REDIRECT, 1)
+ # Feedback's external platform script includes the legacy redirect so
+ # its strict policy never needs inline JavaScript.
+ if path.parent.name != 'feedback':
+ text = text.replace(' <meta charset="utf-8">\n',
+ ' <meta charset="utf-8">\n' + GITHUB_PAGES_REDIRECT, 1)
text = versioned(path, text)
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(text, encoding="utf-8")
@@ -2845,9 +2848,12 @@ def feedback_page(code: str) -> str:
c, f = load(code), FEEDBACK_COPY[code]
up = "../" * (LANGS[code][0] + FEEDBACK).count("/")
top = page_top(c, code, FEEDBACK, f['name'] + ' — PasteFerry', f['lede'])
+ # Apply the policy before any executable script, including platform setup.
+ top = top.replace('<head>\n', '<head>\n'
+ " <meta http-equiv=\"Content-Security-Policy\" content=\"default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'none'; form-action 'self'\">\n")
+ top = top.replace(PLATFORM, f' <script src="{up}assets/platform.js"></script>\n')
top = top.replace('</head>', f' <link rel="stylesheet" href="{up}assets/feedback.css">\n'
- ' <meta name="referrer" content="no-referrer">\n'
- " <meta http-equiv=\"Content-Security-Policy\" content=\"default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'none'; form-action 'self'\">\n</head>")
+ ' <meta name="referrer" content="no-referrer">\n</head>')
categories = ''.join(f'<option value="{key}">{esc(f[key])}</option>' for key in ('bug', 'idea', 'discussion'))
form_categories = categories.replace('value="discussion"', 'value="discussion" selected')
language_options = ''.join(f'<option value="{key}">{name}</option>' for key, (_, _, _, name) in LANGS.items())
diff --git a/tools/site/feedback.test.cjs b/tools/site/feedback.test.cjs
index bfcdc99..764e95a 100644
--- a/tools/site/feedback.test.cjs
+++ b/tools/site/feedback.test.cjs
@@ -9,9 +9,22 @@ const html = fs.readFileSync(path.join(__dirname, '../../docs/feedback/index.htm
const copy = JSON.parse(html.match(/id="feedback-copy">([^<]+)<\/script>/)[1]);
const flush = () => new Promise(resolve => setImmediate(resolve));
+test('all feedback languages enforce CSP before scripts and contain no inline JavaScript', () => {
+ for (const lang of ['', 'en/', 'ja/', 'zh-tw/', 'zh-hk/']) {
+ const page = fs.readFileSync(path.join(__dirname, '../../docs', lang, 'feedback/index.html'), 'utf8');
+ assert.ok(page.indexOf('Content-Security-Policy') < page.indexOf('<script'));
+ for (const match of page.matchAll(/<script\b([^>]*)>([\s\S]*?)<\/script>/g)) {
+ assert.ok(/src=|type="application\/json"/.test(match[1]), lang + ': inline executable script');
+ }
+ assert.match(page, /assets\/platform\.js/);
+ }
+});
+
function fixture(options = {}) {
class Element {
constructor() { this.textContent = ''; this.value = ''; this.hidden = false; this.listeners = {}; this.children = []; }
+ set innerHTML(_) { throw new Error('Untrusted HTML sink used'); }
+ insertAdjacentHTML() { throw new Error('Untrusted HTML sink used'); }
addEventListener(name, listener) { this.listeners[name] = listener; }
append(...nodes) { this.children.push(...nodes); }
replaceChildren(...nodes) { this.children = nodes; }
@@ -42,7 +55,7 @@ function fixture(options = {}) {
fetch: async (url, init) => {
if (url === '/_feedback/v1/messages') {
const input = JSON.parse(init.body); requests.push(input);
- return new Promise(resolve => { finish = () => resolve(options.failure ? {ok: false, status: 429, json: async () => ({code: 'rate_limited'})} : {ok: true, status: 201, json: async () => ({...input, owner_token: undefined, status: 'pending', route: 'manual'})}); });
+ return new Promise(resolve => { finish = () => resolve(options.failure ? {ok: false, status: 429, json: async () => ({code: 'rate_limited'})} : {ok: true, status: 201, json: async () => ({...input, owner_token: undefined, status: 'pending', route: 'manual', ...options.ownerFields})}); });
}
return {ok: true, status: 200, json: async () => ({items: options.items || [], next_cursor: ''})};
},
@@ -112,3 +125,17 @@ test('discussion topics and replies do not display issue progress', async () =>
assert.equal(rendered.includes(copy.open), hasProgress, category + '/' + parent_id);
}
});
+
+test('malicious public and owner content stays text and never becomes HTML', async () => {
+ const payload = '</script><img src=x onerror=alert(1)><svg/onload=alert(2)>';
+ const item = {id: 'a0b1c2d3-1111-4111-8111-111111111111', nickname: payload, title: payload, body: payload, reason: payload, status: 'approved', category: 'discussion'};
+ const f = fixture({items: [item], ownerFields: {reason: payload}}); await flush();
+ const all = element => [element, ...element.children.flatMap(all)];
+ let nodes = all(f.el('feedback-list'));
+ assert.equal(nodes.filter(node => node.textContent === payload).length, 3);
+ assert.equal(nodes.find(node => node.href)?.href, '#' + item.id);
+ f.fields.nickname.value = payload; f.fields.title.value = payload; f.fields.body.value = payload;
+ f.submit(); f.finish(); await flush(); await flush();
+ nodes = all(f.el('feedback-mine'));
+ assert.equal(nodes.filter(node => node.textContent === payload).length, 4);
+});