From 2edbc398055f1c629a2278ef64fe22fb8a887556 Mon Sep 17 00:00:00 2001 From: Bkk1234 Date: Tue, 24 Nov 2026 15:10:32 +0700 Subject: [PATCH] feat(privacy): privacy page, security headers, Chrome fallback for Samsung installs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The 'privacy' refusal on the S23 is Google Play Protect blocking the WebAPK that Samsung Internet mints (old targetSdkVersion) — a browser-side bug that also hits other PWAs; Chrome on the same device installs fine. So: - ui/install-tip.js: detect Samsung Internet on Android and offer the same URL in Chrome (intent:// link, copy-link fallback, dismissable, remembered) - ui/views/privacy.js + #/privacy route + footer link: Thai privacy policy (what is stored locally, no cookies/analytics/third-party scripts, how to delete) - server/publish.mjs: CSP (script-src 'self', style-src 'self' 'unsafe-inline'), Referrer-Policy, Permissions-Policy, X-Content-Type-Options on every response - ui/boot-motion.js: the head inline script moved out so CSP can forbid inline JS - ui/data.js: admin publish token now lives in sessionStorage (was localStorage) - theme.css: .notice / .legal components (44px targets), DESIGN.md + README + AGENTS Verified: installabilityErrors [], fonts load under CSP, all requests same-origin, notice renders under the header at 360px with 44px buttons and stays dismissed, privacy page renders 5 sections. VERSION 2026.11.24.6. --- AGENTS.md | 5 +++ DESIGN.md | 2 + README.md | 13 ++++++ assets/css/theme.css | 88 ++++++++++++++++++++++++++++++++++++++++ index.html | 19 +++------ server/publish.mjs | 29 ++++++++++++- sw.js | 5 ++- ui/app.js | 6 ++- ui/boot-motion.js | 15 +++++++ ui/data.js | 10 +++-- ui/install-tip.js | 97 ++++++++++++++++++++++++++++++++++++++++++++ ui/views/privacy.js | 77 +++++++++++++++++++++++++++++++++++ 12 files changed, 346 insertions(+), 20 deletions(-) create mode 100644 ui/boot-motion.js create mode 100644 ui/install-tip.js create mode 100644 ui/views/privacy.js diff --git a/AGENTS.md b/AGENTS.md index 07e2955..68eeab2 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -45,6 +45,9 @@ PUBLISH_PUSH=0 node server/publish.mjs # เขียน+commit แต่ไ | `ui/views/admin.js` | หลังบ้าน: ล็อกอิน → ฟอร์มแก้ไข, เรียงลำดับ, ลบ, เผยแพร่, ดาวน์โหลด JSON | | `server/publish.mjs` | static server + `GET/PUT api/projects` + git commit/push | | `server/db.mjs` | ชั้น SQLite (`node:sqlite`): schema, `readData`/`saveData` (ทรานแซกชัน), `backupDatabase` (VACUUM INTO), `exportJson`, `seedFromJson` | +| `ui/views/privacy.js` | หน้านโยบายความเป็นส่วนตัว (`#/privacy`) — ลิงก์ในฟุตเตอร์ | +| `ui/install-tip.js` | ตรวจ Samsung Internet บน Android แล้วพาไปติดตั้งผ่าน Chrome (การติดตั้งจาก Samsung Internet ถูก Play Protect บล็อก) | +| `ui/boot-motion.js` | สคริปต์ head (sync) ใส่คลาส `fx-motion` ก่อนเฟรมแรก — แยกออกมาเพราะ CSP ปิด inline script | | `scripts/bump-version.mjs` | bump เวอร์ชันคู่ (`sw.js` VERSION + ``) · `--check` ใช้ใน `npm run check` | | `data/hobbypj.sqlite` | ฐานข้อมูลจริง (ตาราง `site` 1 แถว + `projects`) — **ไม่ขึ้น git** | | `data/projects.json` | สำเนา export ของฐานข้อมูล (ขึ้น git, service worker precache, ใช้อ่านตอนไม่มีเซิร์ฟเวอร์) | @@ -112,6 +115,8 @@ curl -s localhost:8090/api/projects | head -5 - `data/hobbypj.sqlite` + `-wal` + `-shm` ไม่ขึ้น git — ย้ายเครื่องให้ commit `data/projects.json` แล้วปล่อยให้ seed ทำงาน หรือคัดลอกไฟล์ `.sqlite` ไปเอง - **`.dockerignore` ต้องตัด `data/*.sqlite*`, `data/backups`, `data/projects.migrated.json`** ไม่งั้นฐานข้อมูลของเครื่อง dev ถูกอบลง image และ container ใหม่จะไม่ seed จาก `projects.json` (เจอมาแล้วรอบหนึ่ง) - **container ต้องมี `ADMIN_TOKEN`** — คำขอผ่านพอร์ต mapping มาจาก bridge network ไม่ใช่ loopback จึงตกกติกา "เฉพาะเครื่องนี้" แล้วได้ 403 +- **Samsung Internet ติดตั้ง PWA ไม่ได้บน Android 14+** — Play Protect ขึ้น “แอปสร้างสำหรับ Android รุ่นเก่า และไม่มีการป้องกันความเป็นส่วนตัวล่าสุด” เพราะ WebAPK ที่ Samsung สร้างมี `targetSdkVersion` เก่า (บั๊กฝั่งเบราว์เซอร์ ไม่ใช่ manifest) · Chrome บนเครื่องเดียวกันติดตั้งได้ → `ui/install-tip.js` ตรวจ UA แล้วพาผู้ใช้ไป Chrome ห้ามไปไล่แก้ manifest +- **CSP `script-src 'self'` ห้าม inline script** — สคริปต์ head ต้องอยู่ในไฟล์ (`ui/boot-motion.js`) · `style-src` ยังต้องมี `'unsafe-inline'` เพราะ view ใส่ CSS custom property ใน `style="…"` - **`docker compose up` ในเครื่องต้องไม่ได้รัน dev server พร้อมกันบน `./data` เดียวกัน** — SQLite ขึ้น `disk I/O error` เพราะสองโปรเซสล็อกไฟล์ข้าม bind mount (ปิด `node server/publish.mjs` ก่อน หรือเปลี่ยน `DB_PATH`) - **override ต้องใช้ `volumes: !override`** ไม่งั้น compose ต่อท้าย named volume ของ base → `/app/data` มีสอง mount ซ้อน แล้ว sqlite เปิดไม่ได้ - **base compose (`docker-compose.yml`) ห้าม publish พอร์ต** — Dokploy รัน `-f ./docker-compose.yml` อย่างเดียว; ต้อง `expose` + `traefik.docker.network=dokploy-network` และให้ Container Port ในแท็บ Domains = `PORT` diff --git a/DESIGN.md b/DESIGN.md index 08e15ac..7c243b9 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -53,6 +53,8 @@ - **คำว่า `hobbyproject` ในฮีโร่**: แบ่งสีตามตัวอักษร — `hobbyp` + `j` ใช้ `--brand-900` (#3D241A, 12.21:1) และ `ro` + `ect` ใช้ `--brand-500` (#C4703C, 3.12:1 บนพื้นฮีโร่เข้มสุด) จึงต้องมีขนาด ≥24px ตัวหนาขึ้นไป (ตอนนี้ clamp 24–32px) ถ้าลดต่ำกว่านั้นต้องเปลี่ยนไปใช้ `--brand-600` (#A85A2C, 4.30:1) เพื่อให้ผ่าน 4.5:1 ของตัวอักษรปกติ - **ฮีโร่**: พื้นไล่เฉดอุ่นอ่อน + การ์ดลอย 3 ใบ **สุ่มจากโปรเจคทั้งหมดใหม่ทุกครั้งที่โหลดหน้า** (Fisher–Yates, ไม่ซ้ำ) แสดงเฉพาะ ≥1080px — **กดได้จริง** ลิงก์ไป `project_url` เดียวกับการ์ดในกริด (`target="_blank"` + `rel="noopener noreferrer"`) จึงต้องไม่ใส่ `aria-hidden` และต้องโฟกัสด้วยคีย์บอร์ดได้ - **หลังบ้าน**: พาเนล + การ์ดรายการ, ปุ่มเลื่อนขึ้น/ลง/ลบ, แถบบันทึกแบบ sticky ด้านล่าง, `` ยืนยันการลบ/ทิ้งการแก้ไข +- **แถบคำแนะนำ (`.notice`)**: พื้น `--brand-50`, ขอบซ้าย 4px `--brand-600`, อยู่ใต้ header ก่อนเนื้อหา, ปุ่มทุกตัวสูง ≥44px, ปิดแล้วจำใน `localStorage` (`hobbypj.install.tip.dismissed.v1`) +- **หน้าข้อความกฎหมาย (`.legal`)**: ความกว้างสูงสุด 72ch, หัวข้อ `--brand-800`, ใช้ได้กับหน้า `#/privacy` ## 6. สิ่งที่ห้าม diff --git a/README.md b/README.md index 58d92ec..4a09b73 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,19 @@ npm run version # bump VERSION (sw.js) + (index.html) npm run check # มี --check ตรวจสองค่านี้ไม่ให้หลุดกัน ``` +## ความเป็นส่วนตัว (`#/privacy`) + +หน้าเว็บนี้ไม่มีการติดตามใด ๆ และประกาศไว้เป็นหน้าในเว็บ (`#/privacy`, ลิงก์ในฟุตเตอร์): + +| เรื่อง | สถานะ | +|---|---| +| คุกกี้ / เครื่องมือวิเคราะห์ / สคริปต์บุคคลที่สาม | ไม่มี (ตรวจแล้ว: request ทั้งหน้าอยู่ origin เดียว) | +| ข้อมูลในเครื่อง | `localStorage` = แคชรายการ + ฉบับร่างหลังบ้าน · `sessionStorage` = รหัสเผยแพร่ (หายเมื่อปิดแท็บ) | +| สิทธิ์อุปกรณ์ | ไม่ขอ (กล้อง/ไมโครโฟน/ตำแหน่ง ถูกปิดด้วย `Permissions-Policy`) | +| Header ความปลอดภัย | `Content-Security-Policy` (`script-src 'self'`), `Referrer-Policy: strict-origin-when-cross-origin`, `X-Content-Type-Options: nosniff`, `frame-ancestors 'none'` | + +> รหัสเผยแพร่หลังบ้านย้ายไป `sessionStorage` แล้ว (เดิมค้างใน `localStorage`) — ไม่เก็บ credential ถาวรบนเครื่อง + ## ระบบหลังบ้านทำงานอย่างไร ``` diff --git a/assets/css/theme.css b/assets/css/theme.css index d14aa25..337292a 100644 --- a/assets/css/theme.css +++ b/assets/css/theme.css @@ -472,3 +472,91 @@ select { transition-duration: 0.001ms !important; } } + +/* ------------------------------------------------------- notice (คำแนะนำ) */ +.notice { + display: flex; + gap: var(--space-3); + margin-block: var(--space-4); + padding: var(--space-4); + border: 1px solid var(--border-strong); + border-inline-start: 4px solid var(--brand-600); + border-radius: var(--radius-md); + background: var(--brand-50); + color: var(--foreground); + font-size: 14.5px; +} +.notice__body { + display: grid; + gap: var(--space-2); +} +.notice__title { + margin: 0; + font-weight: 700; + color: var(--brand-800); +} +.notice__text { + margin: 0; + max-width: 62ch; + color: var(--muted-foreground); +} +.notice__actions { + display: flex; + flex-wrap: wrap; + gap: var(--space-2); + margin-top: var(--space-2); +} + +/* --------------------------------------------------------- หน้า legal/ข้อมูล */ +.crumbs { + display: flex; + gap: 8px; + margin-bottom: var(--space-4); + color: var(--muted-foreground); + font-size: 13.5px; +} +.crumbs a { + color: var(--brand-700); +} +.legal__body { + display: grid; + gap: var(--space-3); + max-width: 72ch; + line-height: 1.75; +} +.legal__meta { + margin: 0; + color: var(--muted-foreground); + font-size: 13.5px; +} +.legal__body h2 { + margin: var(--space-4) 0 0; + font-size: 1.05rem; + color: var(--brand-800); +} +.legal__body ul { + margin: 0; + padding-inline-start: 1.2em; + display: grid; + gap: 6px; +} +.legal__body p { + margin: 0; +} +.legal__back { + margin-top: var(--space-4); +} + +/* ------------------------------------------------------- footer link */ +.site-footer__inner { + gap: var(--space-4); + flex-wrap: wrap; +} +.site-footer__links a { + color: var(--brand-700); +} + +/* ปุ่มในแถบคำแนะนำต้องกดง่ายบนมือถือ (≥44px) */ +.notice__actions .btn { + min-height: 44px; +} diff --git a/index.html b/index.html index e3a0833..17dc96e 100644 --- a/index.html +++ b/index.html @@ -2,19 +2,7 @@ - + โปรเจคอดิเรกของเรา - + @@ -72,11 +60,14 @@ + +
diff --git a/server/publish.mjs b/server/publish.mjs index ccc9224..5a1ce06 100644 --- a/server/publish.mjs +++ b/server/publish.mjs @@ -69,11 +69,38 @@ const NO_CACHE = { "access-control-allow-origin": "*", }; +/** + * Header ด้านความเป็นส่วนตัว/ความปลอดภัย — ติดทุกคำตอบ + * CSP ไม่เปิด 'unsafe-inline' ให้ script (inline script ถูกย้ายไป ui/boot-motion.js แล้ว) + * แต่ยังเปิดให้ style เพราะทั้งแอปใช้ CSS custom property ใน style attribute (--fx-delay, --i) + */ +const SECURITY_HEADERS = { + "referrer-policy": "strict-origin-when-cross-origin", + "x-content-type-options": "nosniff", + "permissions-policy": + "camera=(), microphone=(), geolocation=(), payment=(), usb=(), accelerometer=(), gyroscope=(), magnetometer=()", + "content-security-policy": [ + "default-src 'self'", + "script-src 'self'", + "style-src 'self' 'unsafe-inline'", + "img-src 'self' data:", + "font-src 'self'", + "connect-src 'self'", + "manifest-src 'self'", + "worker-src 'self'", + "object-src 'none'", + "base-uri 'self'", + "form-action 'self'", + "frame-ancestors 'none'", + ].join("; "), +}; + function sendJson(res, status, body) { const payload = JSON.stringify(body, null, 2); res.writeHead(status, { "content-type": "application/json; charset=utf-8", "content-length": Buffer.byteLength(payload), + ...SECURITY_HEADERS, ...NO_CACHE, }); res.end(payload); @@ -259,7 +286,7 @@ async function serveStatic(req, res, pathname) { return; } const ext = path.extname(filePath).toLowerCase(); - const headers = { "content-type": MIME[ext] || "application/octet-stream" }; + const headers = { "content-type": MIME[ext] || "application/octet-stream", ...SECURITY_HEADERS }; if ([".html", ".css", ".js", ".mjs", ".json"].includes(ext) || ext === "") { Object.assign(headers, NO_CACHE); } else { diff --git a/sw.js b/sw.js index a081eb3..ef8846f 100644 --- a/sw.js +++ b/sw.js @@ -2,7 +2,7 @@ กติกา: bump VERSION เฉพาะเมื่อแก้ไฟล์นี้หรือรายการ PRECACHE เนื้อหา (HTML/CSS/JS/JSON) ใช้ network-first จึงไม่ต้อง bump */ -const VERSION = "2026.11.24.5"; +const VERSION = "2026.11.24.6"; const CACHE = `hobbypj-${VERSION}`; const PRECACHE = [ @@ -19,9 +19,12 @@ const PRECACHE = [ "./ui/data.js", "./ui/auth.js", "./ui/pwa.js", + "./ui/boot-motion.js", + "./ui/install-tip.js", "./ui/motion.js", "./ui/views/home.js", "./ui/views/admin.js", + "./ui/views/privacy.js", "./data/projects.json", "./config/admin-credentials.json", "./icons/icon-192.png", diff --git a/ui/app.js b/ui/app.js index 3c1391b..e6a49cb 100644 --- a/ui/app.js +++ b/ui/app.js @@ -6,6 +6,8 @@ import { defineRoutes, markActiveNav, onRouteChange, startRouter } from "./route import { countUpInside, initMotion, initReveal } from "./motion.js"; import { homeView } from "./views/home.js"; import { adminView } from "./views/admin.js"; +import { privacyView } from "./views/privacy.js"; +import { initInstallTip } from "./install-tip.js"; import "./pwa.js"; const main = document.getElementById("main"); @@ -39,6 +41,7 @@ let activeView = null; const TITLES = { "/": "โปรเจคอดิเรกของเรา", "/admin": "จัดการข้อมูล · โปรเจคอดิเรก", + "/privacy": "ความเป็นส่วนตัว · โปรเจคอดิเรก", }; function renderRoute({ path, view }) { @@ -73,9 +76,10 @@ async function boot() { state.data = result.data; state.source = result.source; - defineRoutes({ "/": homeView, "/admin": adminView }, "/"); + defineRoutes({ "/": homeView, "/admin": adminView, "/privacy": privacyView }, "/"); onRouteChange(renderRoute); startRouter(); + initInstallTip(); if (result.source === "cache") { toast("เปิดแบบออฟไลน์ — แสดงข้อมูลที่บันทึกไว้ในเครื่อง"); diff --git a/ui/boot-motion.js b/ui/boot-motion.js new file mode 100644 index 0000000..8d58153 --- /dev/null +++ b/ui/boot-motion.js @@ -0,0 +1,15 @@ +/* boot-motion.js — ใส่คลาส fx-motion ก่อนวาดเฟรมแรก (สคริปต์ใน head, โหลดแบบ synchronous) + * + * แยกออกมาจาก inline script ใน index.html เพื่อให้ page ใช้ Content-Security-Policy + * แบบ script-src 'self' ได้ (ไม่ต้องเปิด 'unsafe-inline') + * JS ปิด หรือผู้ใช้ตั้ง reduced-motion → ไม่ใส่คลาส → เนื้อหาเห็นปกติทันที + */ +(function () { + try { + if (!window.matchMedia("(prefers-reduced-motion: reduce)").matches) { + document.documentElement.classList.add("fx-motion"); + } + } catch (error) { + /* ถ้า matchMedia ใช้ไม่ได้ ก็ไม่ต้องมีอนิเมชัน */ + } +})(); diff --git a/ui/data.js b/ui/data.js index 0870749..dc9a303 100644 --- a/ui/data.js +++ b/ui/data.js @@ -152,9 +152,11 @@ export function clearDraft() { } /* ------------------------------------------------------------------ token */ +/** รหัสเผยแพร่เก็บเฉพาะในแท็บที่ใช้อยู่ (sessionStorage) — ปิดแท็บแล้วหาย + * ไม่เก็บถาวรใน localStorage เพราะเป็น credential ไม่ควรค้างอยู่บนเครื่อง */ export function readToken() { try { - return localStorage.getItem(TOKEN_KEY) || ""; + return sessionStorage.getItem(TOKEN_KEY) || ""; } catch { return ""; } @@ -162,8 +164,10 @@ export function readToken() { export function writeToken(token) { try { - if (token) localStorage.setItem(TOKEN_KEY, token); - else localStorage.removeItem(TOKEN_KEY); + if (token) sessionStorage.setItem(TOKEN_KEY, token); + else sessionStorage.removeItem(TOKEN_KEY); + // เก็บกวาดค่าที่เคยค้างจากเวอร์ชันก่อน (localStorage) + localStorage.removeItem(TOKEN_KEY); } catch { /* ignore */ } diff --git a/ui/install-tip.js b/ui/install-tip.js new file mode 100644 index 0000000..098e474 --- /dev/null +++ b/ui/install-tip.js @@ -0,0 +1,97 @@ +/* install-tip.js — แนะนำทางติดตั้งที่ผ่านการตรวจของระบบ + * + * ทำไมต้องมี: Samsung Internet สร้าง WebAPK ด้วย targetSdkVersion เก่า ทำให้ Android 14+ + * (One UI 9 / Android 16) ขึ้นกล่อง Google Play Protect ว่า "แอปสร้างสำหรับ Android รุ่นเก่า + * และไม่มีการป้องกันความเป็นส่วนตัวล่าสุด" แล้วบล็อกการติดตั้ง — เป็นปัญหาของตัวเบราว์เซอร์ + * ไม่ใช่ของเว็บ (Chrome บนเครื่องเดียวกันติดตั้งได้ปกติ) + * → ตรวจ UA แล้วพาไปติดตั้งผ่าน Chrome แทน พร้อมคัดลอกลิงก์ไว้เปิดเอง + */ + +const DISMISS_KEY = "hobbypj.install.tip.dismissed.v1"; + +function isSamsungInternet() { + return /SamsungBrowser\//i.test(navigator.userAgent); +} + +function isStandalone() { + return ( + window.matchMedia("(display-mode: standalone)").matches || + window.matchMedia("(display-mode: minimal-ui)").matches || + navigator.standalone === true + ); +} + +function isAndroid() { + return /Android/i.test(navigator.userAgent); +} + +/** intent เปิดลิงก์เดียวกันด้วย Chrome (ถ้าเครื่องมี Chrome) */ +function chromeIntent(url) { + const clean = url.replace(/^https?:\/\//i, ""); + return `intent://${clean}#Intent;scheme=https;package=com.android.chrome;end`; +} + +function dismissed() { + try { + return localStorage.getItem(DISMISS_KEY) === "1"; + } catch { + return false; + } +} + +function rememberDismissed() { + try { + localStorage.setItem(DISMISS_KEY, "1"); + } catch { + /* เก็บไม่ได้ก็ไม่เป็นไร */ + } +} + +/** เรียกตอนบูต — แสดงคำแนะนำเฉพาะ Samsung Internet ที่ยังไม่ได้ติดตั้ง */ +export function initInstallTip() { + if (!isAndroid() || !isSamsungInternet() || isStandalone() || dismissed()) return null; + const host = document.getElementById("install-tip"); + if (!host) return null; + + const currentUrl = location.href; + host.innerHTML = ` +
+
+

ติดตั้งผ่าน Samsung Internet ยังถูกบล็อก

+

+ Android ขึ้นเตือนว่า “ไม่มีการป้องกันความเป็นส่วนตัวล่าสุด” เพราะ Samsung Internet + สร้างแพ็กเกจติดตั้งเวอร์ชันเก่า (ไม่ใช่ปัญหาของเว็บนี้) — เปิดลิงก์เดียวกันด้วย Chrome + แล้วเลือก “ติดตั้งแอป” จะติดตั้งได้ปกติ +

+
+ เปิดด้วย Chrome + + +
+
+
`; + + host.hidden = false; + + host.addEventListener("click", async (event) => { + const action = event.target.closest("[data-act]")?.dataset.act; + if (!action) return; + if (action === "dismiss") { + rememberDismissed(); + host.hidden = true; + host.replaceChildren(); + return; + } + if (action === "copy") { + try { + await navigator.clipboard.writeText(currentUrl); + event.target.textContent = "คัดลอกแล้ว"; + } catch { + /* Safari/เบราว์เซอร์ที่ไม่ให้ clipboard — ผู้ใช้คัดลอกจาก address bar เองได้ */ + window.prompt("คัดลอกลิงก์นี้ไปเปิดใน Chrome", currentUrl); + } + } + }); + + return host; +} diff --git a/ui/views/privacy.js b/ui/views/privacy.js new file mode 100644 index 0000000..c107e7d --- /dev/null +++ b/ui/views/privacy.js @@ -0,0 +1,77 @@ +/* views/privacy.js — นโยบายความเป็นส่วนตัว (หน้าเดียวใน SPA, route #/privacy) */ + +import { escapeHtml } from "../ui.js"; + +export function privacyView(ctx) { + const ownerName = escapeHtml(ctx.data.owner_name || ""); + const updated = new Date().toLocaleDateString("th-TH", { + year: "numeric", + month: "long", + day: "numeric", + }); + + return { + mount(root) { + root.innerHTML = ` +
+ + + +
`; + }, + }; +}