diff --git a/.gitea/workflows/images.yml b/.gitea/workflows/images.yml index a07287f..8b21c0a 100644 --- a/.gitea/workflows/images.yml +++ b/.gitea/workflows/images.yml @@ -91,13 +91,25 @@ jobs: org.opencontainers.image.revision=${{ github.sha }} org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }} + - name: Build Gecko relay + uses: https://github.com/docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6.18.0 + with: + context: . + file: Dockerfile.gecko-relay + platforms: linux/amd64 + load: true + tags: ${{ env.REGISTRY }}/${{ env.IMAGE_OWNER }}/pageswiki-gecko-relay:${{ env.IMAGE_TAG }} + labels: | + org.opencontainers.image.revision=${{ github.sha }} + org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }} + # Publish only after all builds succeed. Use the host daemon for registry # access; BuildKit does not inherit the runner's /etc/hosts entries. - name: Push images shell: bash run: | set -euo pipefail - for image in pageswiki-app pageswiki-adaptor-gateway pageswiki-excalidraw-viewer; do + for image in pageswiki-app pageswiki-adaptor-gateway pageswiki-excalidraw-viewer pageswiki-gecko-relay; do docker push "$REGISTRY/$IMAGE_OWNER/$image:$IMAGE_TAG" done @@ -105,7 +117,7 @@ jobs: if: always() shell: bash run: | - for image in pageswiki-app pageswiki-adaptor-gateway pageswiki-excalidraw-viewer; do + for image in pageswiki-app pageswiki-adaptor-gateway pageswiki-excalidraw-viewer pageswiki-gecko-relay; do ref="$REGISTRY/$IMAGE_OWNER/$image:$IMAGE_TAG" if docker image inspect "$ref" >/dev/null 2>&1; then docker image rm "$ref" || echo "Could not remove local CI tag: $ref" >&2 diff --git a/cmd/gecko-relay/main.go b/cmd/gecko-relay/main.go index 200aeb6..0985ab9 100644 --- a/cmd/gecko-relay/main.go +++ b/cmd/gecko-relay/main.go @@ -66,10 +66,15 @@ func main() { if err := geckorelay.VerifyAssets(assets); err != nil { log.Fatal(err) } + distribution, err := geckorelay.VerifyDistribution(assets) + if err != nil { + log.Fatal(err) + } relay, err := geckorelay.New(secret, assets) if err != nil { log.Fatal(err) } + relay.Distribution = distribution s := &http.Server{Addr: addr, Handler: relay, ReadHeaderTimeout: 5 * time.Second, IdleTimeout: 30 * time.Second, MaxHeaderBytes: 16 << 10} log.Printf("Gecko relay listening; authenticated TCP 80/443 only") log.Fatal(s.ListenAndServe()) diff --git a/docker-compose.gecko.yml b/docker-compose.gecko.yml index 2fefde1..3721767 100644 --- a/docker-compose.gecko.yml +++ b/docker-compose.gecko.yml @@ -14,12 +14,12 @@ services: build: context: . dockerfile: Dockerfile.gecko-relay - image: pageswiki-gecko-relay:dev + image: ${PAGESWIKI_GECKO_IMAGE:-pageswiki-gecko-relay:dev} environment: PAGESWIKI_GECKO_KEY_FILE: /run/secrets/gecko-key volumes: - ${PAGESWIKI_GECKO_KEY_PATH:?Set absolute relay key path}:/run/secrets/gecko-key:ro - - ${PAGESWIKI_GECKO_ASSETS_DIR:?Set verified Gecko dist directory}:/gecko:ro + - ${PAGESWIKI_GECKO_ASSETS_DIR:?Set verified complete Gecko bundle directory}:/gecko:ro networks: gecko-front: {} gecko-egress: {} diff --git a/docs/design/ja/extensions.md b/docs/design/ja/extensions.md index 7323f18..463c80c 100644 --- a/docs/design/ja/extensions.md +++ b/docs/design/ja/extensions.md @@ -48,6 +48,8 @@ IPアドレス管理(IPAM)、DNSゾーン/レコード管理、機器・ ## CSV/表・スライド拡張(設計TODO) +初期CSV macroは実装済みです。`{{csv:/data/items.csv range=A1:H200 header=true}}` を認証済みProjectionで展開し、`rev=` で同じObjectのRevisionへ固定できます。現在のPath閲覧権限を毎回検査し、外部URL、匿名公開、asset指定、共有キャッシュ、CSV編集はこの段階に含めません。入力256 KiB・UTF-8、表示200行×32列、走査2,000行・100列、セル16 KiB、Projection当たり4種類のCSVまでに制限します。引用符・改行・BOMに対応し、セルはHTML escapeしてmacroや数式を実行しません。処理不能時は内容を漏らさない警告に置き換えます。Authoringでは元のmacroを保持します。 + CSVをPage本文へ複製せず、認可済みの別Documentをテーブルとして投影するmacroを用意します。候補構文は `{{csv:/data/items.csv range=A1:H200 header=true}}` とし、Path、Revision(またはasset hash)、列・行上限、表示モードを正規化してキャッシュキーに含めます。macroは表示時にboundedなProjectionを作るだけで、結果HTMLを正本へ逆流させません。CSV Editorは元Revisionを編集し、表投影とは別の操作・競合・権限として扱います。 交換形式はRFC 4180互換CSV(UTF-8、CRLF、引用符・改行を含むfield、BOMの扱いを明示)を第一正本とします。複数シート、型、数式、結合セル、書式が必要な場合はODSを別アダプタ/Assetとして扱い、CSVへの不可逆変換を黙って行いません。行数、列数、セル長、総バイト、ネストmacroを制限し、malformed inputは安全なテキスト表示へfallbackします。 diff --git a/docs/design/ja/feeds.md b/docs/design/ja/feeds.md index 55e2a30..b660873 100644 --- a/docs/design/ja/feeds.md +++ b/docs/design/ja/feeds.md @@ -396,11 +396,13 @@ Wiki単独は親Path配下の `rss-{request UUID}` に新規作成します。 - [x] RSS-7c:Shift+j/kによるフォルダ選択。空・未分類・端・購読選択中・検索維持・入力/モーダル抑止・連打を合成試験。 - [x] RSS-8a:Gecko端末描画+自前Wisp中継の方式決定。公開中継を使わず、独立・既定無効で導入する。 - [x] RSS-8b1:upstream v0.0.1/hash固定、最小埋め込みと親documentのisolation、Chromiumで合成記事の描画・遷移確認。 -- [ ] RSS-8b2:Safari/iPhone SE実機、IME・コピー・find・証明書エラー・HTTPS互換性、配布license/notice整備。 +- [ ] RSS-8b2:Safari/iPhone SE実機、IME・コピー・実サイト互換性。固定配布物のlicense/notice整備、HTTPSと証明書エラー、日本語findの合成試験は実施済み。 - [x] RSS-8c1:認証付きWisp TCP profile、公開IP/port制限、失効・資源上限・管理者/CLI無効化・内部status。 - [ ] RSS-8c2:運用ホストの外向きfirewall、設定UIへの中継監視統合、負荷時の長時間受入。 - [x] RSS-8d1:個人設定の選択、1ページのpopup閲覧、戻る/進む/再読込/別タブ/終了。 -- [ ] RSS-8d2:初回/再表示・低メモリ端末の性能評価、失敗/切断・複数タブのUX、本番配備。 +- [ ] RSS-8d2:初回/再表示・低メモリ端末の性能評価、実行中の切断・複数タブのUX、本番配備。起動上限と起動失敗時のiframe破棄・原文リンクへの退避は実装済み。 + +一覧には配信日と配信時刻(HH:mm)を分けて表示します。Domainの設定タイムゾーンを使い、tooltipには年・秒・タイムゾーンを含めます。配信日時不明は「—」とし、取得日時で補いません。モバイルでも時刻欄を残します。 隔離試験はRSS/Atom・FreshRSS login/read、欠落ID・巨大/不正feed、条件付きGET、失敗時cache保持、 本人分離・失効・readonly、二重送信・版競合・旧worker拒否、migration再実行/失敗rollback、 diff --git a/docs/design/ja/implementation-backlog.md b/docs/design/ja/implementation-backlog.md index 7d28763..aaf9d3d 100644 --- a/docs/design/ja/implementation-backlog.md +++ b/docs/design/ja/implementation-backlog.md @@ -103,7 +103,7 @@ P0は認可・整合性・復旧の安全条件、P1は日常利用の回復性 | SHARE-01 / P2 | 未実装:委譲share URL、期限/失効/監査、説明可能な権限総覧 | AUTH-01、PUB-01。login/adapter/federation資格情報と別目的、単一Page投影、caveat・依存物の失効を検証 | [公開](publication.md)、[Schutzaria](schutzaria.md) | | COLLAB-01 / P2 | 一部実装:Page等の変更購読、最近閲覧/編集、集約通知。コメント/反応/評価は未実装 | OUT-01。現在権限で再投影、private履歴のopt-in/保持。inline commentはRevision変更時の孤立・再接続を扱う | [拡張](extensions.md) | | BLOG-01 / P2 | 一部実装:安全なtDiary import、最近コメント、遠隔ブログ射影 | COLLAB-01、遠隔はFED-01。importは再開/重複抑止/dry-run。同一instance射影を再実装しない | [ブログ・日記](blog-diary.md) | -| CSV-01 / P2 | viewerのみ実装:別Documentを投影するCSV macro/editor、ODS境界 | 行列・セル・総量上限、Revision固定、権限、CSV引用/改行fixture。表示HTMLをCSV正本へ逆流させない | [表設計](extensions.md) | +| CSV-01 / P2 | viewerと有界CSV macroを実装。残件:CSV editor、匿名投影、asset指定、ODS境界 | 行列・セル・総量上限、Revision固定、権限、CSV引用/改行fixture。表示HTMLをCSV正本へ逆流させない | [表設計](extensions.md) | | DIAGRAM-01 / P2 | 一部実装:nwdiag/Draw.io provider、slide preview/editor | RENDER-01。安全なSVG・include拒否・renderer版固定、ticket/CAS保存、print。既存Excalidraw/Krokiとは分ける | [Kunstzeal](kunstzeal.md) | | RECIPE-01 / P2 | 未実装:構造化レシピmetadataとPage投影の初期slice | 材料・数量・未知単位を保持、人数変更は派生値、Asset認可、版競合、出典・公開確認 | [料理レシピ](recipe-management.md) | @@ -111,7 +111,7 @@ P0は認可・整合性・復旧の安全条件、P1は日常利用の回復性 | ID / 優先 | 状態・次の作業 | 着手ゲート・最初の検証 | 詳細 | | --- | --- | --- | --- | -| RSS-01 / P2 | 一部実装:内蔵簡易正本、FreshRSS読取connector、3ペインUI・HTML本文、Wiki保存・タスク化・dashboard、OPML確認付き入出力 | 残件はFreshRSS既読/star書戻し、購読/カテゴリnavigation、実接続受入、公平性/backoff。managed schema feeds v3・隔離fixture。本番未反映。二重正本・停止時の自動切替なし | [RSS](feeds.md) | +| RSS-01 / P2 | 一部実装:内蔵簡易正本、FreshRSS読取・既読/star書戻し、購読/カテゴリnavigation、3ペインUI・HTML本文、Wiki保存・タスク化・dashboard、OPML、配信時刻 | 残件は実接続受入、公平性/backoff、Gecko実機・運用受入。個別の実装/受入状況はRSS設計を参照。二重正本・停止時の自動切替なし | [RSS](feeds.md) | | IMAP-01 / P3 | 未実装・選定待ち:IMAP読取同期 | provider/認証/保持を決める。UIDVALIDITY/UID、添付量、外部画像、失効をfixture化。neomutt captureは実装済みで別 | [メール](mail.md) | | TICKET-01 / P3 | 未実装:queue/番号/workflow/外部依頼者 | 共有範囲と状態遷移を定義、同時採番・失効・監査。既存個人タスクをticketと呼び替えない | [タスクとticket](tasks-tickets.md) | | EXT-01 / P3 | built-inのみ:第三者manifest/署名/隔離runtime、停止・rollback・観測 | capability・secret・host・予算・export契約。任意コードをGoプロセス内へ直接ロードしない | [拡張](extensions.md) | diff --git a/docs/design/ja/navigation-search.md b/docs/design/ja/navigation-search.md index 511574d..95ad7c8 100644 --- a/docs/design/ja/navigation-search.md +++ b/docs/design/ja/navigation-search.md @@ -14,6 +14,14 @@ childrenは現在Pathを既定にし、表示形式は本文macroの `view` 引 `/v/tree/{path}` は範囲を限定します。展開・全展開・全折りたたみ状態の保持、複数選択、rename・移動・削除はPath操作と一致させます。一般の子一覧から予約ユーザー領域を混入させません。再帰操作は事前の全対象確認と競合検出が必要です。 +## Browserのアップロード + +`/v/browser/{path}` の一覧へファイルをドロップすると、現在のPath配下へアップロードします。フォルダ行へのドロップではそのフォルダが宛先になります。既存の内部Path移動・選択操作とは区別します。 + +右下のフローティングカードに待機・送信中・完了・失敗・取消と進捗を表示します。追加ドロップは同じキューへ積み、送信は1件ずつ、最大100件・各256 MiBまで。待機中の取消、折りたたみ、終了分のクリアに対応します。終了後は一覧だけを再取得し、キューは残します。 + +既存の認可付き `/v/upload` を使用し、`If-None-Match: *` で同名Pathの上書きを防ぎます。通信断は保存結果不明として扱い、自動再送しません。キューはページ内のみで、ページ離脱・再起動後の再開やフォルダの再帰アップロードには未対応です。新規オブジェクト・Path・初回Revisionの一体的なトランザクション化は残課題です。 + ## ホームとブックマーク 通常トップバー型、中央検索型、検索+セクションカード型を選びます。セクションは手動URL/Path、または指定親の子リンクを表示します。設定は歯車のポップアップで、閲覧画面を編集フォームで埋めません。カード列はviewportに適応します。 diff --git a/docs/design/ja/priority-ux-20261031.md b/docs/design/ja/priority-ux-20261031.md index 221a5ee..b4b2e02 100644 --- a/docs/design/ja/priority-ux-20261031.md +++ b/docs/design/ja/priority-ux-20261031.md @@ -23,6 +23,12 @@ ### 3. テーマ・アカウント・連絡先 +- [x] 信頼できる端末の長期ログイン(2026-11-08ローカル実装、本番未反映):ログイン画面の明示選択で最大30日。Domain管理者は設定→セッションで0〜30日(0で無効)を指定する。通常ログインは既存の絶対期限を維持。パスワード/TOTP、LDAP、OIDC、SMTPマジックリンクに適用し、選択は認証challengeに固定する。CookieとBearerの期限はDBの発行期限と一致し、ログアウト・管理者失効・アカウント停止で無効になる。既存セッションの延長、TOTPの省略、アクセスによる期限延長は行わない。 +- [x] 独立CLIの `pages login --remember` / `pages login --oidc --no-browser --remember`。S256 grantに選択を保存し、ブラウザ側のクエリで上書きできない。APIセッションは承認元ブラウザの残存期限を超えない。期限切れ後はlogoutなしで再認証でき、同一アカウントのcwdと未送信タスク下書きを保持する。別アカウントへの下書き移動は拒否する。旧サーバーは選択を無視する場合があるため、実際の期限は応答のexpires_inを正とする。 +- [x] タスク編集フッターの関連ページ導線(2026-11-08ローカル実装、本番未反映):アーカイブ・削除・保存と同じ並びに関連ページリンクを表示。新規タブで開き、編集中の内容を保持する。保存済みの関連Pageがない場合、解除した場合、Path変更が未保存の場合は非表示。デスクトップ/モバイル4幅のブラウザテストを追加。 + +長期ログインのDB変更はmanaged component `cli-auth` v2の `remember` 列追加のみ。既存行はfalse、既存セッションと文書・タスクデータは変更しない。端末名・一覧・個別端末の信頼登録、refresh token rotationは別設計とし、今回は既存セッション管理から失効する。 + - [x] 共通ナビの◐からプリセットを選択、リロードせず切替。host限定Cookieに保持し、Domain既定に戻せる。登録済みテーマのみ許可。Domain設定/アカウントの全端末設定ではなくブラウザ上書き。失敗時は現在テーマを保持する。 - [x] 絶対セッション期限:設定→セッション、5〜43200分、既定1440分。Domain単位で新規ブラウザ/APIログイン(OIDC/LDAPを含む)に適用。発行時にDBの期限を固定し、Cookie/APIのexpires_inにも反映。既存セッションの期限とCalDAVアプリパスワードは変更しない。管理者認可とサーバ側失効をfixture検証。 - [ ] 無操作タイムアウトとアカウント別の短縮上書き:background pollingを人間の操作とみなさないactivity契約、再認証、既存セッションへの即時適用を別途設計する。 diff --git a/docs/design/ja/remote-cli.md b/docs/design/ja/remote-cli.md index d082818..82de644 100644 --- a/docs/design/ja/remote-cli.md +++ b/docs/design/ja/remote-cli.md @@ -26,6 +26,14 @@ v0.3.0のOIDCログインは利用者の動作確認済み。v0.4.0ではOIDC承 ## 利用方法 +### 信頼できる端末でのログイン継続 + +2026-11-08ローカル実装、未リリース。独立CLIと対応サーバーを更新すると、`pages login --remember` または `pages login --oidc --no-browser --remember` で最長30日のセッションを要求できる。管理者の `auth_trusted_session_days`(既定30、0で無効)に従い、応答の `expires_in` を期限として保存する。旧サーバーでは通常の期限になる場合がある。 + +パスワードログインは必要なTOTPを維持する。選択はTOTP challenge/OIDC grantへ固定し、認証完了時の追加パラメーターで変更できない。CLI grantはmanaged component `cli-auth` v2で `remember` 列を追加し、既存行をfalseとして保持する。承認済みgrantの交換とセッション発行は従来どおり単一トランザクションで処理し、期限は承認元ブラウザの期限を超えない。 + +ローカル期限切れ後は `logout` を挟まず再ログインできる。同一アカウントならcwdと未送信タスク下書きを保持し、認証失敗時は従来のローカル状態を変更しない。ログアウト・管理者による失効・アカウント停止は長期セッションにも適用する。共有端末では使用しない。 + ```sh # 初回設定。パスワードはlogin時の非表示プロンプト、必要ならTOTPを続けて入力。 pages configure --origin https://pages.example.test --username anya diff --git a/docs/operations/gecko-redistribution.md b/docs/operations/gecko-redistribution.md index c4b8d82..30ff5dc 100644 --- a/docs/operations/gecko-redistribution.md +++ b/docs/operations/gecko-redistribution.md @@ -1,8 +1,9 @@ # Gecko連携の再配布条件 -PagesWiki v0.13.0は、自作の連携コードと中継サービスのソースを配布し、Geckoエンジンは同梱しません。 -実験機能`viewer.gecko`は既定無効のままです。既存3イメージのリリースにエンジンを追加せず、 -専用relayも上流エンジンを含まないDockerfileとして提供します。 +PagesWiki v0.13.0ではGeckoエンジンの配布を保留しました。 +現在は固定版のエンジン、対応ソース、第三者NOTICE、日本語フォントを含む配布bundleを生成できます。 +実験機能`viewer.gecko`は既定無効のままです。relayイメージとbundleは別物で、 +起動時にbundle全体を検証してから資産を配信します。 本書は今回追加した部品の配布判断と残件を整理するもので、プロジェクト全体の法務監査ではありません。 ## 部品と適用条件 @@ -17,6 +18,8 @@ PagesWiki v0.13.0は、自作の連携コードと中継サービスのソース | Emscripten runtime / WasmFS | 6.0.1と上流のpatch | Emscripten本体はMITまたはUIUC/NCSA。libc等のruntime依存には別noticeがあるため、ツールチェーンの最上位LICENSEだけでは棚卸し完了としない | | zstddec | 0.2.0 | npm metadataは`MIT AND BSD-3-Clause`。JS wrapperと組み込まれたdecoderの両noticeを保持 | | Liberation fonts | 上記Firefox commit内のPDF.js standard_fonts | SIL OFL 1.1。著作権表示・OFL本文・Reserved Font Namesを保持 | +| Rust standard libraryと依存crate | 1.95.0と`library/Cargo.lock` | RustのMIT/Apache-2.0と各crateの個別条件。lockに記載された全registry crateのsource・noticeを保持 | +| Noto Sans CJK JP | Sans2.004、`523d033d6cb47f4a80c58a35753646f5c3608a78` | SIL OFL 1.1。未改変OTF、Adobeの埋込著作権表示、OFL本文を保持 | relayのruntimeイメージは`FROM scratch`とし、Goバイナリとlicenseだけを含めます。 Alpine/BusyBoxを新たに再配布する構成にはしません。`go list -deps`でrelayにリンクされる外部Go moduleは @@ -62,9 +65,28 @@ MPLはファイル単位の義務であり、別実装のWiki全体をMPLへ変 4. 提供するsourceとnoticeの可用性・保全、配布物とのhash対応を検査する。 5. Mozillaの商標はコードlicenseとは別に扱い、Firefoxの公式配布であるとの表現やロゴの流用をしない。 -現時点では2・3・4の配布用bundleが未完成です。今回のrelease artifactにエンジンは載せず、 -本番では中継用鍵・資産mount・`viewer.gecko`の有効化を追加しません。 -エンジン非同梱の連携コードと、通常のRSSフォルダショートカットはリリース対象です。 +### 配布bundle + +`tools/gecko/sources.lock.json`に取得元とSHA-256を固定し、`tools/gecko/bundle.py`で組み立てます。 +生成物は98ファイル、合計1,196,102,561 bytes(manifest自身を除く)です。 +manifestのSHA-256は`84953114f5e393dd97d9f948a5bc51c95b9355b58461dd3355e068a96cb2e381`。 +同じ値をrelay側で固定し、列挙された全ファイルのサイズ・hash・通常ファイルであることを起動時に検査します。 +不足、改変、symlink、別版混入を許可しません。 + +元のsource archiveを変更せず保存し、license/copying/copyright/notice/authorsファイルを別の閲覧用テキストにも集約します。 +Mozillaの条件付きnoticeは**全分岐を含む保守的な上位集合**としてテキスト化し、元のHTMLはsource archiveに保持します。 +Rustの追加依存はCargo.lockのチェックサムで検証します。crateから欠けていたFortanixのMPLヘッダと +vex-sdkのMIT本文も、元ソース・固定commitから補完しています。 +Notoの未改変フォントには`© 2014-2021 Adobe (http://www.adobe.com/).`が埋め込まれ、READMEにも保持します。 + +閲覧画面のⓘは`/_/api/v1/extension/organizer.feeds/browser/distribution/v0.0.1/index.html`を開きます。 +同じ入口からNOTICE、manifest、ソース一式を取得できます。認証は必要ですが、RSS・Geckoを無効化した後も、 +認証済みの受領者はソースを取得できます。readonly利用者も取得可能です。 +relay自体を撤去する場合は、先にbundleと取得先を保全・移設してください。無効化はソース提供の撤回を意味しません。 + +CIは中継バイナリの4番目のイメージを配布しますが、約1.2GBのbundleを通常イメージへ重複同梱しません。 +検証済みbundleをread-onlyでmountする運用です。再配布条件の充足はこの固定版・未改変バイナリ・ +保持したsource/noticeを前提とします。依存更新時はlockとnoticeを再確認し、公開済みbundleを上書きしません。 ## 一次資料 @@ -75,3 +97,5 @@ MPLはファイル単位の義務であり、別実装のWiki全体をMPLへ変 - [zstddec 0.2.0 metadata](https://registry.npmjs.org/zstddec/0.2.0) - [Go LICENSE](https://go.dev/LICENSE) - [golang.org/x/net v0.35.0 LICENSE](https://go.googlesource.com/net/+/refs/tags/v0.35.0/LICENSE) +- [Noto CJK固定版OFL](https://github.com/notofonts/noto-cjk/blob/523d033d6cb47f4a80c58a35753646f5c3608a78/LICENSE) +- [vex-sdk固定版MIT](https://github.com/vexide/vex-sdk/blob/6c0b83d35b6fc31a7607efe456e387df45476314/LICENSE.md) diff --git a/docs/operations/gecko-relay.md b/docs/operations/gecko-relay.md index aef0b0a..5421264 100644 --- a/docs/operations/gecko-relay.md +++ b/docs/operations/gecko-relay.md @@ -28,7 +28,8 @@ WebSocketではOriginも検査し、grantは一回限りです。既存接続の bac28056a00ef6e5fb4345117e73d3629e75aca00dac71389fabbce39c018a73 ``` -展開後の`gecko.js/dist/`をread-onlyで`/gecko`へmountします。起動時に次の両ファイルのhashを確認し、 +エンジン単体の`dist/`ではなく、後述するsource/notice付きbundleをread-onlyで`/gecko`へmountします。 +起動時に次の両ファイルに加え、manifestに列挙された全ソース・NOTICE・フォントをhash検査し、 欠損・改変・異なる版の混在があれば中継は起動しません。 | ファイル | SHA-256 | @@ -37,13 +38,27 @@ bac28056a00ef6e5fb4345117e73d3629e75aca00dac71389fabbce39c018a73 | gecko.wasm.zst | ebd6c75179152624d9644865e11792ed25cadc714d9e0985f412baf9c637a086 | 資産URLにも`assets/v0.0.1/`を含め、版をまたいだJS/WASMのcache混在を避けます。 -初回にはJSと圧縮WASMの合計38,175,359 bytes(約36.4MiB)の転送とWASMの展開・compileが必要です。 +初回にはJS・圧縮WASM・日本語フォントの合計54,643,095 bytes(約52.1MiB)の転送とWASMの展開・compileが必要です。 +約1.2GBの対応ソースは閲覧の起動時には取得せず、ⓘから明示的にダウンロードするときだけ配信します。 本体のDocker buildにFirefoxのソースビルドを加えません。 `Dockerfile.gecko-relay`は自前の中継バイナリだけを含みます。エンジンは同梱・再配布しません。 **エンジンをイメージへ同梱する場合だけでなく、mountした資産を利用者へ配信する前にもnoticeと対応sourceの提供が必要です。** 固定版のMPL宣言、第三者依存物、今回の配布範囲は[再配布条件](gecko-redistribution.md)を参照してください。 -v0.13.0の本番配備ではエンジンを配信せず、機能の有効化も行いません。 +v0.13.0の本番配備ではエンジンを配信していません。新しいrelayは完成したbundleを必須とします。 + +### bundle生成 + +Python 3.11以降を使い、十分な空き容量のあるディスク上で実行します。ソースarchiveは展開せず保持します。 + +```sh +python tools/gecko/bundle.py --cache /srv/gecko-cache --output /srv/gecko/v0.0.1-bundle1 +# 同一入力からの再生成。出力先はまだ存在しない新しいディレクトリを指定する。 +python tools/gecko/bundle.py --cache /srv/gecko-cache --output /srv/gecko/rebuilt --offline +``` + +出力manifestのSHA-256が`84953114f5e393dd97d9f948a5bc51c95b9355b58461dd3355e068a96cb2e381`と一致することを確認します。 +稼働中のmount先を上書きせず、source/noticeの提供を含めて不変の単位で保全します。 ## 配置 @@ -55,7 +70,8 @@ v0.13.0の本番配備ではエンジンを配信せず、機能の有効化も | 変数 | 内容 | | --- | --- | | PAGESWIKI_GECKO_KEY_PATH | 32文字以上の十分ランダムな共有鍵のファイル絶対Path。appとrelayへ同じread-only mount | -| PAGESWIKI_GECKO_ASSETS_DIR | hash確認した`gecko.js/dist`の絶対Path | +| PAGESWIKI_GECKO_ASSETS_DIR | 全ファイルを検証したbundleの絶対Path。engineだけのdistは禁止 | +| PAGESWIKI_GECKO_IMAGE | Giteaで配布した`pageswiki-gecko-relay:sha-...`またはrelease tag。省略時は開発用image | | PAGESWIKI_GECKO_FRONT_SUBNET | 内部接続網。既定`172.24.0.0/27`。既存LANとの重複を避ける | | PAGESWIKI_GECKO_EGRESS_SUBNET | 中継の外向き網。既定`172.24.0.32/27` | @@ -100,7 +116,8 @@ UDP、80/443以外のTCP、単一ラベルのホスト名は拒否します。 | WebSocket受信idle | 90秒 | | 宛先接続 | 名前解決込み8秒、個別dial 2秒 | | relayコンテナ | 256MiB、0.5 CPU、64 PIDs、read-only、capabilitiesなし | -| 閲覧プロファイル | volatileな32MiB・2,048 entries、端末のOPFSへ永続化しない | +| 閲覧プロファイル | 日本語フォントを含むvolatileな32MiB・2,048 entries、端末のOPFSへ永続化しない | +| source/noticeダウンロード | 全体2接続まで。ETag・Range対応。閲覧streamの枠とは分離 | relayの稼働状態・接続数・送受信bytes・拒否数は、コンテナ内の次の操作でJSON取得できます。 鍵はファイルから読み、標準出力には含めません。counterは再起動でリセットします。 @@ -134,7 +151,10 @@ extension viewer.gecko off ```sh go test -race ./internal/geckorelay ./internal/server -run 'TestGrant|TestDestinations|TestRelay|TestAssets|TestFeedGeckoAuthorization|TestFeedGeckoWebsocket' -PAGESWIKI_BROWSER_TEST=1 PAGESWIKI_GECKO_TEST_ASSETS=/verified/gecko.js/dist go test ./internal/server -run '^TestFeedGeckoChromium$' -count=1 +PAGESWIKI_BROWSER_TEST=1 PAGESWIKI_GECKO_TEST_ASSETS=/verified/bundle go test ./internal/server -run '^TestFeedGeckoChromium$' -count=1 +PAGESWIKI_GECKO_TEST_BUNDLE=/verified/bundle go test ./internal/geckorelay -run TestDistribution +# 公開テストサイトへの実通信。実RSS・外部カレンダー・メールは使用しない。 +PAGESWIKI_BROWSER_TEST=1 PAGESWIKI_GECKO_TEST_ASSETS=/verified/bundle PAGESWIKI_GECKO_PUBLIC_HTTPS_TEST=1 go test ./internal/server -run '^TestFeedGeckoChromium$' -count=1 node internal/server/testdata/feed-gecko-profile.mjs ``` @@ -144,6 +164,12 @@ node internal/server/testdata/feed-gecko-profile.mjs 標準のhistory.back()が失敗するため、フォーム内容やスクロール位置を復元する履歴ではありません。 画面のcanvasは最大1920×1080、experimental WASM JITとGPUは無効です。 -本番の標準機能に昇格する前に、HTTPSサイトと証明書エラーの表示、Safari/iPhone SE、IME、コピー、 -find、実サイト互換性、複数タブ・起動失敗時のUX、低メモリ端末の測定、コンテナ外向きfirewallを検証します。 +HTTPSの実描画と期限切れ証明書の拒否を確認しました。上流最小GREはエラーページの部品が不足するため、 +`about:neterror`/`about:certerror`を検知してcanvasを隠し、PagesWiki側で説明します。証明書の無視・例外追加は提供しません。 +Noto CJKをメモリプロファイルのfontsに置き、エンジンを改変せず日本語描画も確認しています。 + +検索アイコンまたはCtrl/Cmd+Fからページ内検索できます。検索語は256文字まで、前後検索と一致なしの表示に対応し、合成記事で日本語検索を確認しています。起動失敗または120秒の起動上限到達時はiframeを破棄し、元記事を別タブで開く導線を残します。上流のdestroy呼出しだけではWASM資源の解放にならないため、親側のiframe破棄まで行います。`TestFeedGeckoStartupFailureChromium` はフォント取得失敗時の破棄と代替導線を検証します。 + +本番の標準機能に昇格する前に、Safari/iPhone SE、IME、コピー、実サイト互換性、 +複数タブ・低メモリ端末の測定、コンテナ外向きfirewallを検証します。 外部ページのiframe拒否とは別の仕組みですが、すべてのサイトでの動作や軽さを保証するものではありません。 diff --git a/docs/operations/gitea-images.md b/docs/operations/gitea-images.md index d6d0e64..680ee5d 100644 --- a/docs/operations/gitea-images.md +++ b/docs/operations/gitea-images.md @@ -11,6 +11,9 @@ runner の環境構築や Gitea の事前疎通診断は行わず、本番へ自 | `pageswiki-app`(app / app2 共通) | `Dockerfile` | | `pageswiki-adaptor-gateway` | `Dockerfile.adapter-gateway` | | `pageswiki-excalidraw-viewer` | `Dockerfile.excalidraw-viewer` | +| `pageswiki-gecko-relay`(任意) | `Dockerfile.gecko-relay` | + +v0.15.0以降はGecko relayを含む4イメージをビルド・昇格します。以下の「3イメージ」は通常配備するapp・gateway・Excalidrawの組を指します。Gecko relayは別overlayで明示的に配備するもので、通常の本番更新では起動しません。 Kroki、Mermaid、code-server は upstream の配布イメージを使用する。 `main` / `mk_district` への push、手動実行でビルド・公開する。 diff --git a/go.mod b/go.mod index 69549d3..7dbe417 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,14 @@ require ( modernc.org/sqlite v1.32.0 ) +require ( + github.com/google/go-cmp v0.6.0 // indirect + github.com/google/nftables v0.3.0 // indirect + github.com/mdlayher/netlink v1.7.3-0.20250113171957-fbb4dce95f42 // indirect + github.com/mdlayher/socket v0.5.0 // indirect + golang.org/x/sync v0.11.0 // indirect +) + require ( github.com/Azure/go-ntlmssp v0.1.1 // indirect github.com/SherClockHolmes/webpush-go v1.4.0 diff --git a/go.sum b/go.sum index 21e0ba6..c66c9b4 100644 --- a/go.sum +++ b/go.sum @@ -27,7 +27,10 @@ github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17w github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= github.com/gomarkdown/markdown v0.0.0-20260417124207-7d523f7318df h1:Mwihr/o+v4L5h56rwHLOE20+hh7Okhwno5BHz3zDuao= github.com/gomarkdown/markdown v0.0.0-20260417124207-7d523f7318df/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/nftables v0.3.0 h1:bkyZ0cbpVeMHXOrtlFc8ISmfVqq5gPJukoYieyVmITg= +github.com/google/nftables v0.3.0/go.mod h1:BCp9FsrbF1Fn/Yu6CLUc9GGZFw/+hsxfluNXXmxBfRM= github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd h1:gbpYu9NMq8jhDVbvlGkMFWCjLFlqqEZjEmObmhUy6Vo= github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= @@ -55,6 +58,10 @@ github.com/jcmturner/rpc/v2 v2.0.3 h1:7FXXj8Ti1IaVFpSAziCZWNzbNuZmnvw/i6CqLNdWfZ github.com/jcmturner/rpc/v2 v2.0.3/go.mod h1:VUJYCIDm3PVOEHw8sgt091/20OJjskO/YJki3ELg/Hc= github.com/mattn/go-isatty v0.0.22 h1:j8l17JJ9i6VGPUFUYoTUKPSgKe/83EYU2zBC7YNKMw4= github.com/mattn/go-isatty v0.0.22/go.mod h1:ZXfXG4SQHsB/w3ZeOYbR0PrPwLy+n6xiMrJlRFqopa4= +github.com/mdlayher/netlink v1.7.3-0.20250113171957-fbb4dce95f42 h1:A1Cq6Ysb0GM0tpKMbdCXCIfBclan4oHk1Jb+Hrejirg= +github.com/mdlayher/netlink v1.7.3-0.20250113171957-fbb4dce95f42/go.mod h1:BB4YCPDOzfy7FniQ/lxuYQ3dgmM2cZumHbK8RpTjN2o= +github.com/mdlayher/socket v0.5.0 h1:ilICZmJcQz70vrWVes1MFera4jGiWNocSkykwwoy3XI= +github.com/mdlayher/socket v0.5.0/go.mod h1:WkcBFfvyG8QENs5+hfQPl1X6Jpd2yeLIYgrGFmJiJxI= github.com/microcosm-cc/bluemonday v1.0.27 h1:MpEUotklkwCSLeH+Qdx1VJgNqLlpY2KXwXFM08ygZfk= github.com/microcosm-cc/bluemonday v1.0.27/go.mod h1:jFi9vgW+H7c3V0lb6nR74Ib/DIB5OBs92Dimizgw2cA= github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4= diff --git a/internal/geckorelay/distribution.go b/internal/geckorelay/distribution.go new file mode 100644 index 0000000..2517050 --- /dev/null +++ b/internal/geckorelay/distribution.go @@ -0,0 +1,85 @@ +package geckorelay + +import ( + "crypto/sha256" + "encoding/hex" + "encoding/json" + "fmt" + "io" + "os" + "path/filepath" + "regexp" +) + +const DistributionPrefix = "/distribution/v0.0.1/" + +// Updated only after the reproducible source/notice bundle has been reviewed. +const DistributionManifestSHA256 = "84953114f5e393dd97d9f948a5bc51c95b9355b58461dd3355e068a96cb2e381" + +type DistributionFile struct { + File string `json:"file"` + SHA256 string `json:"sha256"` + Size int64 `json:"size"` + Kind string `json:"kind"` +} + +var distributionName = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9._+-]{0,150}$`) + +func VerifyDistribution(dir string) (map[string]DistributionFile, error) { + return verifyDistribution(dir, DistributionManifestSHA256) +} + +func verifyDistribution(dir, expected string) (map[string]DistributionFile, error) { + path := filepath.Join(dir, "manifest.json") + info, err := os.Lstat(path) + if err != nil || !info.Mode().IsRegular() || info.Size() > 1<<20 { + return nil, fmt.Errorf("Gecko distribution manifest missing or invalid") + } + raw, err := os.ReadFile(path) + if err != nil { + return nil, err + } + sum := sha256.Sum256(raw) + if hex.EncodeToString(sum[:]) != expected { + return nil, fmt.Errorf("Gecko distribution manifest checksum mismatch") + } + var manifest struct { + Format string `json:"format"` + Version string `json:"version"` + Files []DistributionFile `json:"files"` + } + if json.Unmarshal(raw, &manifest) != nil || manifest.Format != "pageswiki.gecko-distribution.v1" || manifest.Version != "v0.0.1" || len(manifest.Files) > 512 { + return nil, fmt.Errorf("invalid Gecko distribution format") + } + files := map[string]DistributionFile{} + for _, entry := range manifest.Files { + if !distributionName.MatchString(entry.File) || entry.File == "manifest.json" || entry.Size < 1 || entry.Size > 2<<30 || len(entry.SHA256) != 64 { + return nil, fmt.Errorf("invalid distribution entry") + } + if _, exists := files[entry.File]; exists { + return nil, fmt.Errorf("duplicate distribution entry") + } + info, err := os.Lstat(filepath.Join(dir, entry.File)) + if err != nil || !info.Mode().IsRegular() || info.Size() != entry.Size { + return nil, fmt.Errorf("distribution file missing or invalid: %s", entry.File) + } + file, err := os.Open(filepath.Join(dir, entry.File)) + if err != nil { + return nil, err + } + hash := sha256.New() + _, err = io.Copy(hash, io.LimitReader(file, entry.Size+1)) + file.Close() + if err != nil || hex.EncodeToString(hash.Sum(nil)) != entry.SHA256 { + return nil, fmt.Errorf("distribution file checksum mismatch: %s", entry.File) + } + files[entry.File] = entry + } + for _, name := range []string{"index.html", "README.txt", "gecko.js", "gecko.wasm.zst", "firefox.tar.gz", "firefox.tar.gz.notices.txt", "wrapper.tar.gz", "wrapper-MPL.txt", "emscripten.tar.gz", "emscripten.tar.gz.notices.txt", "rust-src.tar.xz", "rust-src.tar.xz.notices.txt", "zstddec.tgz", "zstddec.tgz.notices.txt", "NotoSansCJKjp-Regular.otf", "NotoSansCJK-LICENSE.txt"} { + if _, ok := files[name]; !ok { + return nil, fmt.Errorf("distribution incomplete: %s", name) + } + } + files["manifest.json"] = DistributionFile{File: "manifest.json", SHA256: expected, Size: int64(len(raw)), Kind: "manifest"} + return files, nil +} diff --git a/internal/geckorelay/distribution_test.go b/internal/geckorelay/distribution_test.go new file mode 100644 index 0000000..18ec013 --- /dev/null +++ b/internal/geckorelay/distribution_test.go @@ -0,0 +1,83 @@ +package geckorelay + +import ( + "crypto/sha256" + "encoding/hex" + "encoding/json" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "testing" + "time" +) + +func TestDistribution(t *testing.T) { + dir := t.TempDir() + if _, err := VerifyDistribution(dir); err == nil { + t.Fatal("missing distribution accepted") + } + names := []string{"index.html", "README.txt", "gecko.js", "gecko.wasm.zst", "firefox.tar.gz", "firefox.tar.gz.notices.txt", "wrapper.tar.gz", "wrapper-MPL.txt", "emscripten.tar.gz", "emscripten.tar.gz.notices.txt", "rust-src.tar.xz", "rust-src.tar.xz.notices.txt", "zstddec.tgz", "zstddec.tgz.notices.txt", "NotoSansCJKjp-Regular.otf", "NotoSansCJK-LICENSE.txt"} + content := []byte("synthetic source and notice") + hash := sha256.Sum256(content) + var entries []DistributionFile + for _, name := range names { + if err := os.WriteFile(filepath.Join(dir, name), content, 0600); err != nil { + t.Fatal(err) + } + entries = append(entries, DistributionFile{File: name, Size: int64(len(content)), SHA256: hex.EncodeToString(hash[:])}) + } + raw, _ := json.Marshal(map[string]any{"format": "pageswiki.gecko-distribution.v1", "version": "v0.0.1", "files": entries}) + if err := os.WriteFile(filepath.Join(dir, "manifest.json"), raw, 0600); err != nil { + t.Fatal(err) + } + sum := sha256.Sum256(raw) + want := hex.EncodeToString(sum[:]) + files, err := verifyDistribution(dir, want) + if err != nil { + t.Fatal(err) + } + relay, _ := New(testKey, dir) + relay.Distribution = files + for _, name := range []string{"index.html", "firefox.tar.gz", "manifest.json", "unknown", "../manifest.json"} { + req := httptest.NewRequest("GET", DistributionPrefix+name, nil) + req.Header.Set(GrantHeader, Sign(testKey, Grant{Subject: "test:user", Nonce: "download-test-0123456789", Expires: time.Now().Add(30 * time.Second).Unix()})) + rec := httptest.NewRecorder() + relay.ServeHTTP(rec, req) + expected := 200 + if strings.Contains(name, "unknown") || strings.Contains(name, "../") { + expected = 404 + } + if rec.Code != expected { + t.Fatalf("%s: %d", name, rec.Code) + } + if expected == 200 && rec.Header().Get("ETag") == "" { + t.Fatal("no ETag") + } + } + if err := os.WriteFile(filepath.Join(dir, "README.txt"), []byte("tampered"), 0600); err != nil { + t.Fatal(err) + } + if _, err := verifyDistribution(dir, want); err == nil { + t.Fatal("tampered notice accepted") + } + if err := os.Remove(filepath.Join(dir, "README.txt")); err != nil { + t.Fatal(err) + } + if err := os.Symlink(filepath.Join(dir, "gecko.js"), filepath.Join(dir, "README.txt")); err != nil { + t.Fatal(err) + } + if _, err := verifyDistribution(dir, want); err == nil { + t.Fatal("symlink accepted") + } +} + +func TestDistributionRealBundle(t *testing.T) { + dir := os.Getenv("PAGESWIKI_GECKO_TEST_BUNDLE") + if dir == "" { + t.Skip("opt-in complete distribution") + } + if _, err := VerifyDistribution(dir); err != nil { + t.Fatal(err) + } +} diff --git a/internal/geckorelay/relay.go b/internal/geckorelay/relay.go index 5651ed8..442cd68 100644 --- a/internal/geckorelay/relay.go +++ b/internal/geckorelay/relay.go @@ -9,6 +9,7 @@ import ( "io" "net" "net/http" + "os" "path/filepath" "strings" "sync" @@ -28,6 +29,8 @@ const ( type Relay struct { Key, Assets string + Distribution map[string]DistributionFile + downloads chan struct{} mu sync.Mutex active map[string]bool used map[string]int64 @@ -40,7 +43,7 @@ func New(key, assets string) (*Relay, error) { if len(key) < 32 { return nil, errors.New("relay key must contain at least 32 bytes") } - r := &Relay{Key: key, Assets: assets, active: map[string]bool{}, used: map[string]int64{}} + r := &Relay{Key: key, Assets: assets, active: map[string]bool{}, used: map[string]int64{}, downloads: make(chan struct{}, 2)} r.dial = func(ctx context.Context, host string, port uint16) (net.Conn, error) { ctx, cancel := context.WithTimeout(ctx, 8*time.Second) defer cancel() @@ -72,6 +75,47 @@ func (r *Relay) ServeHTTP(w http.ResponseWriter, req *http.Request) { json.NewEncoder(w).Encode(map[string]any{"active_sessions": active, "received_bytes": r.rx.Load(), "sent_bytes": r.tx.Load(), "rejected": r.rejected.Load(), "profile": "tcp-80-443", "max_sessions": 4, "max_streams": maxStreams, "bytes_per_second": bytesPerSecond, "max_session_bytes": maxBytes}) return } + if strings.HasPrefix(req.URL.Path, DistributionPrefix) { + name := strings.TrimPrefix(req.URL.Path, DistributionPrefix) + entry, ok := r.Distribution[name] + if !ok { + http.NotFound(w, req) + return + } + select { + case r.downloads <- struct{}{}: + defer func() { <-r.downloads }() + default: + http.Error(w, "download limit", 429) + return + } + w.Header().Set("ETag", `"`+entry.SHA256+`"`) + w.Header().Set("Cache-Control", "private, max-age=86400, immutable") + w.Header().Set("X-Content-Type-Options", "nosniff") + w.Header().Set("Content-Security-Policy", "default-src 'none'; base-uri 'none'; frame-ancestors 'self'") + w.Header().Set("Referrer-Policy", "no-referrer") + switch { + case name == "index.html": + w.Header().Set("Content-Type", "text/html; charset=utf-8") + case strings.HasSuffix(name, ".txt"): + w.Header().Set("Content-Type", "text/plain; charset=utf-8") + case name == "manifest.json": + w.Header().Set("Content-Type", "application/json") + default: + w.Header().Set("Content-Type", "application/octet-stream") + w.Header().Set("Content-Disposition", `attachment; filename="`+name+`"`) + } + // ServeContent avoids ServeFile's /index.html -> ./ redirect, which + // would escape the explicit file allowlist at the proxy boundary. + file, err := os.Open(filepath.Join(r.Assets, name)) + if err != nil { + http.NotFound(w, req) + return + } + defer file.Close() + http.ServeContent(w, req, name, time.Time{}, file) + return + } if req.URL.Path == AssetsPrefix+"gecko.js" || req.URL.Path == AssetsPrefix+"gecko.wasm.zst" { w.Header().Set("Cross-Origin-Resource-Policy", "same-origin") w.Header().Set("Cache-Control", "private, max-age=86400, immutable") diff --git a/internal/server/auth_admin.go b/internal/server/auth_admin.go index 161d7bd..e88d8ab 100644 --- a/internal/server/auth_admin.go +++ b/internal/server/auth_admin.go @@ -35,6 +35,17 @@ func (s *Server) handleAuthAdmin(w http.ResponseWriter, r *http.Request) { } panel := "auth" switch r.FormValue("action") { + case "save_trusted_session_policy": + panel = "auth-sessions" + days, err := strconv.Atoi(r.PostForm.Get("trusted_session_days")) + if err != nil || days < 0 || days > 30 { + http.Error(w, "trusted session lifetime must be between 0 and 30 days", 400) + return + } + if err := s.store.SetConfigInDomain(domainUUID, "auth_trusted_session_days", strconv.Itoa(days)); err != nil { + http.Error(w, "could not save session policy", 500) + return + } case "save_session_policy": panel = "auth-sessions" minutes, err := strconv.Atoi(r.FormValue("session_minutes")) diff --git a/internal/server/auth_browser_test.go b/internal/server/auth_browser_test.go index ce87e6d..9d519f0 100644 --- a/internal/server/auth_browser_test.go +++ b/internal/server/auth_browser_test.go @@ -51,4 +51,8 @@ func TestBrowserCookieLoginLogoutChromium(t *testing.T) { if err := s.store.DB.QueryRow(`SELECT count(*) FROM auth_sessions WHERE revoked_at_ms>0`).Scan(&revoked); err != nil || revoked != 1 { t.Fatalf("browser revocations=%d %v", revoked, err) } + var lifetime int64 + if err := s.store.DB.QueryRow(`SELECT expires_at_ms-auth_time_ms FROM auth_sessions WHERE revoked_at_ms>0`).Scan(&lifetime); err != nil || lifetime != (30*24*time.Hour).Milliseconds() { + t.Fatalf("trusted browser lifetime=%d %v", lifetime, err) + } } diff --git a/internal/server/auth_cli.go b/internal/server/auth_cli.go index add9b40..d87a163 100644 --- a/internal/server/auth_cli.go +++ b/internal/server/auth_cli.go @@ -74,7 +74,7 @@ func (s *Server) handleCLIAuth(w http.ResponseWriter, r *http.Request, action st writeEventJSON(w, 400, map[string]string{"error": "invalid_request"}) return } - id, device, code, err := s.store.StartCLIAuth(ctx, domain, cliOrigin(r), challenge) + id, device, code, err := s.store.StartCLIAuthForLogin(ctx, domain, cliOrigin(r), challenge, r.PostForm.Get("remember") == "1") if err != nil { cliAuthError(w, err) return @@ -140,6 +140,9 @@ func (s *Server) verifyCLIAuth(w http.ResponseWriter, r *http.Request) { return } data := map[string]any{"CLIAuth": true, "CLIOrigin": grant.Origin, "CLIRequest": id} + if grant.Remember { + data["CLIRememberDays"] = s.store.TrustedSessionDays(domain) + } if grant.State != "pending" { data["CLIDone"] = true s.renderAuthPage(w, r, data) diff --git a/internal/server/auth_cli_test.go b/internal/server/auth_cli_test.go index 776e11b..9b01eea 100644 --- a/internal/server/auth_cli_test.go +++ b/internal/server/auth_cli_test.go @@ -28,6 +28,14 @@ func cliForm(s *Server, path string, form url.Values, cookie, origin string) *ht } func TestCLIAuthOIDCFlow(t *testing.T) { + for _, remember := range []bool{false, true} { + t.Run(map[bool]string{false: "normal", true: "trusted"}[remember], func(t *testing.T) { + testCLIAuthOIDCFlow(t, remember) + }) + } +} + +func testCLIAuthOIDCFlow(t *testing.T, remember bool) { s, api, domain := configFixture(t) principal := s.mustCLIPrincipal(t, domain, api) // An enrolled local factor must not add a second challenge to OIDC consent. @@ -43,7 +51,13 @@ func TestCLIAuthOIDCFlow(t *testing.T) { } verifier := strings.Repeat("x", 43) sum := sha256.Sum256([]byte(verifier)) - start := cliForm(s, "/_/auth/cli/start", url.Values{"code_challenge": {base64.RawURLEncoding.EncodeToString(sum[:])}, "code_challenge_method": {"S256"}}, "", "") + startForm := url.Values{"code_challenge": {base64.RawURLEncoding.EncodeToString(sum[:])}, "code_challenge_method": {"S256"}} + expectedSeconds := 24 * 60 * 60 + if remember { + startForm.Set("remember", "1") + expectedSeconds *= 30 + } + start := cliForm(s, "/_/auth/cli/start", startForm, "", "") if start.Code != 200 { t.Fatalf("start %d %s", start.Code, start.Body.String()) } @@ -63,7 +77,12 @@ func TestCLIAuthOIDCFlow(t *testing.T) { t.Fatalf("verify landing: %d %s", verify.Code, verify.Body.String()) } begin := httptest.NewRecorder() - s.beginOIDC(begin, httptest.NewRequest("GET", "https://localhost/_/auth/oidc?cli="+id, nil)) + // The browser query must not override the CLI's persisted choice. + tamperedRemember := "1" + if remember { + tamperedRemember = "0" + } + s.beginOIDC(begin, httptest.NewRequest("GET", "https://localhost/_/auth/oidc?cli="+id+"&remember="+tamperedRemember, nil)) if begin.Code != 302 { t.Fatalf("oidc begin %d %s", begin.Code, begin.Body.String()) } @@ -86,6 +105,9 @@ func TestCLIAuthOIDCFlow(t *testing.T) { for _, cookie := range complete.Result().Cookies() { if cookie.Name == authCookieName { browser = cookie.Value + if cookie.MaxAge != expectedSeconds { + t.Fatalf("cookie lifetime %d, want %d", cookie.MaxAge, expectedSeconds) + } } } if browser == "" { @@ -128,6 +150,7 @@ func TestCLIAuthOIDCFlow(t *testing.T) { var result struct { Token string `json:"access_token"` Username string `json:"username"` + Expires int `json:"expires_in"` } if err := json.Unmarshal(exchange.Body.Bytes(), &result); err != nil { t.Fatal(err) @@ -135,6 +158,9 @@ func TestCLIAuthOIDCFlow(t *testing.T) { if !strings.HasPrefix(result.Token, "pw_api_") || result.Username != "admin" || result.Token == browser { t.Fatal("invalid delegated credential") } + if result.Expires > expectedSeconds || result.Expires < expectedSeconds-10 { + t.Fatalf("CLI lifetime %d, want approximately %d", result.Expires, expectedSeconds) + } if _, err := s.store.PrincipalForSession(domain, result.Token); err != nil { t.Fatal(err) } diff --git a/internal/server/auth_external.go b/internal/server/auth_external.go index cae2909..8b6ba40 100644 --- a/internal/server/auth_external.go +++ b/internal/server/auth_external.go @@ -14,7 +14,10 @@ import ( "time" ) -type oidcChallenge struct{ Verifier, Nonce, Return, Issuer, ClientID, BrowserHash, CLIRequest string } +type oidcChallenge struct { + Verifier, Nonce, Return, Issuer, ClientID, BrowserHash, CLIRequest string + Remember bool +} func logOIDCFailure(stage, domainUUID, issuer string, err error) { host := "invalid" @@ -57,6 +60,7 @@ func (s *Server) beginOIDC(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithTimeout(r.Context(), 8*time.Second) defer cancel() cliRequest := r.URL.Query().Get("cli") + remember := r.URL.Query().Get("remember") == "1" returnPath := safeReturnPath(r.URL.Query().Get("return")) if cliRequest != "" { if !cliSecureTransport(r) || !cliProofPattern.MatchString(cliRequest) { @@ -68,6 +72,12 @@ func (s *Server) beginOIDC(w http.ResponseWriter, r *http.Request) { return } returnPath = cliVerifyPath(cliRequest) + grant, err := s.store.CLIAuthRequest(ctx, domainUUID, cliOrigin(r), cliRequest) + if err != nil { + http.Error(w, "invalid CLI request", 400) + return + } + remember = grant.Remember } metadata, err := fetchOIDCMetadata(ctx, config.Issuer) if err != nil { @@ -77,7 +87,7 @@ func (s *Server) beginOIDC(w http.ResponseWriter, r *http.Request) { } verifier, nonce, browser := randomURLToken(32), randomURLToken(24), randomURLToken(32) browserDigest := sha256.Sum256([]byte(browser)) - state, err := s.store.CreateAuthChallenge(domainUUID, "", "oidc", oidcChallenge{Verifier: verifier, Nonce: nonce, Return: returnPath, Issuer: config.Issuer, ClientID: config.ClientID, BrowserHash: base64.RawURLEncoding.EncodeToString(browserDigest[:]), CLIRequest: cliRequest}, 5*time.Minute) + state, err := s.store.CreateAuthChallenge(domainUUID, "", "oidc", oidcChallenge{Verifier: verifier, Nonce: nonce, Return: returnPath, Issuer: config.Issuer, ClientID: config.ClientID, BrowserHash: base64.RawURLEncoding.EncodeToString(browserDigest[:]), CLIRequest: cliRequest, Remember: remember}, 5*time.Minute) if err != nil { http.Error(w, err.Error(), 500) return @@ -205,7 +215,7 @@ func (s *Server) completeOIDC(w http.ResponseWriter, r *http.Request) { http.Error(w, "No local account matches the configured OIDC identity field", http.StatusForbidden) return } - token, err := s.store.CreateBrowserAuthSession(domainUUID, principal.UUID, 0) + token, lifetime, err := s.store.CreateLoginSession(domainUUID, principal.UUID, 0, true, challenge.Remember) if err != nil { http.Error(w, err.Error(), 500) return @@ -217,7 +227,7 @@ func (s *Server) completeOIDC(w http.ResponseWriter, r *http.Request) { return } } - s.setAuthCookie(w, r, token) + s.setAuthCookieForSeconds(w, r, token, int(lifetime.Seconds())) http.Redirect(w, r, challenge.Return, http.StatusSeeOther) } @@ -244,7 +254,7 @@ func (s *Server) requestMagicLink(w http.ResponseWriter, r *http.Request) { if principalErr == nil { select { case s.smtpSlots <- struct{}{}: - token, err := s.store.CreateAuthChallenge(domainUUID, principal.UUID, "magic-link", magicLinkChallenge{Return: safeReturnPath(r.FormValue("return")), Email: principal.Email}, 15*time.Minute) + token, err := s.store.CreateAuthChallenge(domainUUID, principal.UUID, "magic-link", magicLinkChallenge{Return: safeReturnPath(r.FormValue("return")), Email: principal.Email, Remember: r.PostForm.Get("remember") == "1"}, 15*time.Minute) if err != nil { <-s.smtpSlots break @@ -282,12 +292,12 @@ func (s *Server) verifyMagicLink(w http.ResponseWriter, r *http.Request) { http.Error(w, "This sign-in link is invalid or expired", http.StatusBadRequest) return } - token, err := s.store.CreateBrowserAuthSession(domainUUID, principalUUID, 0) + token, lifetime, err := s.store.CreateLoginSession(domainUUID, principalUUID, 0, true, payload.Remember) if err != nil { http.Error(w, "This sign-in link is invalid or expired", http.StatusBadRequest) return } - s.setAuthCookie(w, r, token) + s.setAuthCookieForSeconds(w, r, token, int(lifetime.Seconds())) http.Redirect(w, r, safeReturnPath(payload.Return), http.StatusSeeOther) } @@ -306,6 +316,8 @@ func (s *Server) renderAuthPage(w http.ResponseWriter, r *http.Request, data map data["LDAPEnabled"] = config.Enabled } data["Locale"] = s.getLocale(domainUUID) + data["TrustedSessionDays"] = s.store.TrustedSessionDays(domainUUID) + data["RememberRequested"] = r.PostForm.Get("remember") == "1" transparency := s.homeGlassTransparency(domainUUID) data["HomeGlassTransparency"] = transparency data["HomeGlassOpacity"] = 100 - transparency diff --git a/internal/server/auth_handler.go b/internal/server/auth_handler.go index e34d3f4..4611b28 100644 --- a/internal/server/auth_handler.go +++ b/internal/server/auth_handler.go @@ -62,7 +62,11 @@ func requestIsHTTPS(r *http.Request) bool { } func (s *Server) setAuthCookie(w http.ResponseWriter, r *http.Request, token string) { - http.SetCookie(w, &http.Cookie{Name: authCookieName, Value: token, Path: "/", HttpOnly: true, Secure: requestIsHTTPS(r), SameSite: http.SameSiteLaxMode, MaxAge: s.store.AuthSessionLifetimeMinutes(s.domainUUIDForRequest(r)) * 60}) + s.setAuthCookieForSeconds(w, r, token, s.store.AuthSessionLifetimeMinutes(s.domainUUIDForRequest(r))*60) +} + +func (s *Server) setAuthCookieForSeconds(w http.ResponseWriter, r *http.Request, token string, seconds int) { + http.SetCookie(w, &http.Cookie{Name: authCookieName, Value: token, Path: "/", HttpOnly: true, Secure: requestIsHTTPS(r), SameSite: http.SameSiteLaxMode, MaxAge: seconds}) } func (s *Server) authTokenFromRequest(r *http.Request) string { @@ -160,6 +164,7 @@ func (s *Server) handleAuth(w http.ResponseWriter, r *http.Request) { } var principal *store.Principal mfaLevel := 0 + remember := r.PostForm.Get("remember") == "1" if action == "setup" && setup && s.validBootstrapToken(domainUUID, r.FormValue("bootstrap_token")) { principal, err = s.store.BootstrapOwner(domainUUID, r.FormValue("username"), r.FormValue("password"), r.FormValue("display_name"), r.FormValue("email")) if err == nil { @@ -171,7 +176,7 @@ func (s *Server) handleAuth(w http.ResponseWriter, r *http.Request) { } else if action == "login" && !setup { principal, err = s.store.AuthenticatePassword(r.FormValue("username"), r.FormValue("password")) if err == nil && s.store.HasTOTP(principal.UUID) { - challenge, challengeErr := s.store.CreateTOTPLoginChallenge(domainUUID, principal.UUID) + challenge, challengeErr := s.store.CreateTOTPLoginChallengeForLogin(domainUUID, principal.UUID, remember) if challengeErr != nil { err = challengeErr } else if strings.Contains(r.Header.Get("Accept"), "application/json") { @@ -191,7 +196,7 @@ func (s *Server) handleAuth(w http.ResponseWriter, r *http.Request) { } } var principalUUID string - principalUUID, err = s.store.CompleteTOTPLoginChallenge(domainUUID, challenge, r.FormValue("code")) + principalUUID, remember, err = s.store.CompleteTOTPLoginChallengeForLogin(domainUUID, challenge, r.FormValue("code")) if err == nil { principal = &store.Principal{UUID: principalUUID} mfaLevel = 1 @@ -201,18 +206,14 @@ func (s *Server) handleAuth(w http.ResponseWriter, r *http.Request) { err = store.ErrAccessDenied } if err == nil { - var token string - if strings.Contains(r.Header.Get("Accept"), "application/json") { - token, err = s.store.CreateAuthSessionWithMFA(domainUUID, principal.UUID, mfaLevel) - } else { - token, err = s.store.CreateBrowserAuthSession(domainUUID, principal.UUID, mfaLevel) - } + token, lifetime, issueErr := s.store.CreateLoginSession(domainUUID, principal.UUID, mfaLevel, !strings.Contains(r.Header.Get("Accept"), "application/json"), remember) + err = issueErr if err == nil { if strings.Contains(r.Header.Get("Accept"), "application/json") { - writeEventJSON(w, http.StatusOK, map[string]any{"access_token": token, "token_type": "Bearer", "expires_in": s.store.AuthSessionLifetimeMinutes(domainUUID) * 60}) + writeEventJSON(w, http.StatusOK, map[string]any{"access_token": token, "token_type": "Bearer", "expires_in": int(lifetime.Seconds())}) return } - s.setAuthCookie(w, r, token) + s.setAuthCookieForSeconds(w, r, token, int(lifetime.Seconds())) http.Redirect(w, r, safeReturnPath(r.FormValue("return")), http.StatusSeeOther) return } diff --git a/internal/server/auth_ldap.go b/internal/server/auth_ldap.go index f61c93a..5a5688f 100644 --- a/internal/server/auth_ldap.go +++ b/internal/server/auth_ldap.go @@ -221,7 +221,7 @@ func (s *Server) handleLDAPLogin(w http.ResponseWriter, r *http.Request) { return } if s.store.HasTOTP(principal.UUID) { - challenge, challengeErr := s.store.CreateTOTPLoginChallenge(domainUUID, principal.UUID) + challenge, challengeErr := s.store.CreateTOTPLoginChallengeForLogin(domainUUID, principal.UUID, r.PostForm.Get("remember") == "1") if challengeErr != nil { http.Error(w, "authentication unavailable", http.StatusServiceUnavailable) return @@ -234,20 +234,15 @@ func (s *Server) handleLDAPLogin(w http.ResponseWriter, r *http.Request) { s.renderAuthPage(w, r, map[string]any{"MFA": true, "Return": returnPath}) return } - var token string - if strings.Contains(r.Header.Get("Accept"), "application/json") { - token, err = s.store.CreateAuthSession(domainUUID, principal.UUID) - } else { - token, err = s.store.CreateBrowserAuthSession(domainUUID, principal.UUID, 0) - } + token, lifetime, err := s.store.CreateLoginSession(domainUUID, principal.UUID, 0, !strings.Contains(r.Header.Get("Accept"), "application/json"), r.PostForm.Get("remember") == "1") if err != nil { http.Error(w, "authentication unavailable", http.StatusServiceUnavailable) return } if strings.Contains(r.Header.Get("Accept"), "application/json") { - writeEventJSON(w, http.StatusOK, map[string]any{"access_token": token, "token_type": "Bearer", "expires_in": s.store.AuthSessionLifetimeMinutes(domainUUID) * 60}) + writeEventJSON(w, http.StatusOK, map[string]any{"access_token": token, "token_type": "Bearer", "expires_in": int(lifetime.Seconds())}) return } - s.setAuthCookie(w, r, token) + s.setAuthCookieForSeconds(w, r, token, int(lifetime.Seconds())) http.Redirect(w, r, returnPath, http.StatusSeeOther) } diff --git a/internal/server/auth_session_policy_test.go b/internal/server/auth_session_policy_test.go index bf053d9..eee9f22 100644 --- a/internal/server/auth_session_policy_test.go +++ b/internal/server/auth_session_policy_test.go @@ -6,6 +6,7 @@ import ( "net/url" "strings" "testing" + "time" ) func TestSessionPolicyAdminUIAndCookie(t *testing.T) { @@ -48,3 +49,99 @@ func TestSessionPolicyAdminUIAndCookie(t *testing.T) { t.Fatalf("cookie policy %#v", cookies) } } + +func TestTrustedSessionPolicyAndRevocation(t *testing.T) { + s, admin, domain := configFixture(t) + p := s.mustCLIPrincipal(t, domain, admin) + for _, days := range []string{"30", "7", "0"} { + form := url.Values{"action": {"save_trusted_session_policy"}, "trusted_session_days": {days}} + w := configRequest(s, admin, "POST", "/-/sys/config/auth", form) + if w.Code != 303 { + t.Fatalf("policy %s: %d %s", days, w.Code, w.Body.String()) + } + want := 24 * time.Hour + if days == "30" { + want *= 30 + } + if days == "7" { + want *= 7 + } + for _, browser := range []bool{false, true} { + token, lifetime, err := s.store.CreateLoginSession(domain, p, 0, browser, true) + if err != nil || lifetime != want { + t.Fatalf("duration %v %v", lifetime, err) + } + if _, err = s.store.PrincipalForSession(domain, token); err != nil { + t.Fatal(err) + } + if err = s.store.RevokeAuthSession(token); err != nil { + t.Fatal(err) + } + if _, err = s.store.PrincipalForSession(domain, token); err == nil { + t.Fatal("revoked trusted session accepted") + } + } + w = httptest.NewRecorder() + s.handleAuth(w, httptest.NewRequest("GET", "https://localhost/_/auth/login", nil)) + if strings.Contains(w.Body.String(), `id="auth-remember"`) != (days != "0") { + t.Fatal("login choice does not reflect policy") + } + } + for _, bad := range []string{"", "-1", "31", "abc"} { + w := configRequest(s, admin, "POST", "/-/sys/config/auth", url.Values{"action": {"save_trusted_session_policy"}, "trusted_session_days": {bad}}) + if w.Code != 400 { + t.Fatalf("invalid policy %q accepted: %d", bad, w.Code) + } + } + user, err := s.store.CreatePasswordAccount(domain, "trusted-reader", "a long test password", "Reader", "") + if err != nil { + t.Fatal(err) + } + if err = s.store.SetConfigInDomain(domain, "auth_trusted_session_days", "30"); err != nil { + t.Fatal(err) + } + token, _, err := s.store.CreateLoginSession(domain, user.UUID, 0, false, true) + if err != nil { + t.Fatal(err) + } + w := configRequest(s, token, "POST", "/-/sys/config/auth", url.Values{"action": {"save_trusted_session_policy"}, "trusted_session_days": {"7"}}) + if w.Code != 403 { + t.Fatalf("reader changed trusted policy: %d", w.Code) + } + if err = s.store.SetPrincipalState(domain, user.UUID, "suspended"); err != nil { + t.Fatal(err) + } + if _, err = s.store.PrincipalForSession(domain, token); err == nil { + t.Fatal("suspended trusted session accepted") + } +} + +func TestTrustedSessionExpiryIsFixed(t *testing.T) { + s, admin, domain := configFixture(t) + p := s.mustCLIPrincipal(t, domain, admin) + token, lifetime, err := s.store.CreateLoginSession(domain, p, 0, false, true) + if err != nil || lifetime != 30*24*time.Hour { + t.Fatalf("trusted default: %v %v", lifetime, err) + } + var id string + var expiry int64 + if err = s.store.DB.QueryRow(`SELECT uuid,expires_at_ms FROM auth_sessions ORDER BY rowid DESC LIMIT 1`).Scan(&id, &expiry); err != nil { + t.Fatal(err) + } + if err = s.store.SetConfigInDomain(domain, "auth_trusted_session_days", "0"); err != nil { + t.Fatal(err) + } + if _, err = s.store.PrincipalForSession(domain, token); err != nil { + t.Fatal(err) + } + var after int64 + if err = s.store.DB.QueryRow(`SELECT expires_at_ms FROM auth_sessions WHERE uuid=?`, id).Scan(&after); err != nil || after != expiry { + t.Fatalf("policy/read changed expiry: %d %d %v", expiry, after, err) + } + if _, err = s.store.DB.Exec(`UPDATE auth_sessions SET expires_at_ms=? WHERE uuid=?`, time.Now().Add(-time.Second).UnixMilli(), id); err != nil { + t.Fatal(err) + } + if _, err = s.store.PrincipalForSession(domain, token); err == nil { + t.Fatal("expired trusted session accepted") + } +} diff --git a/internal/server/auth_trusted_i18n.go b/internal/server/auth_trusted_i18n.go new file mode 100644 index 0000000..e721ea8 --- /dev/null +++ b/internal/server/auth_trusted_i18n.go @@ -0,0 +1,12 @@ +package server + +func init() { + for lang, entries := range map[string]map[string]string{ + "en": {"auth.remember": "Keep me signed in on this trusted device", "auth.days": "days", "auth.remember_cli": "Keep this CLI signed in for up to", "auth.trusted_days": "Trusted device session (days; 0 disables)"}, + "ja": {"auth.remember": "信頼できる端末でログインを維持する", "auth.days": "日間", "auth.remember_cli": "CLIのログイン継続期間(最大)", "auth.trusted_days": "信頼できる端末の有効期間(日・0で無効)"}, + } { + for key, value := range entries { + translations[lang][key] = value + } + } +} diff --git a/internal/server/browser_upload_test.go b/internal/server/browser_upload_test.go new file mode 100644 index 0000000..630bfc7 --- /dev/null +++ b/internal/server/browser_upload_test.go @@ -0,0 +1,88 @@ +package server + +import ( + "context" + "net/http" + "net/http/httptest" + "os" + "os/exec" + "strings" + "sync/atomic" + "testing" + "time" +) + +func TestBrowserUploadQueueChromium(t *testing.T) { + if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" { + t.Skip("opt-in Chromium") + } + for _, width := range []string{"1280", "390"} { + t.Run(width, func(t *testing.T) { testBrowserUploadQueueChromium(t, width) }) + } +} + +func testBrowserUploadQueueChromium(t *testing.T, width string) { + s, token, domain := configFixture(t) + s.mux.HandleFunc("/v/browser/", s.handleBrowser) + s.mux.HandleFunc("/v/upload", s.handleUpload) + object, err := s.store.CreatePageAtPathInDomain(domain, "/drop/existing.txt") + if err != nil { + t.Fatal(err) + } + if _, err = s.store.SaveRevisionContent(object, []byte("original"), "", "und", "text/plain", "existing.txt"); err != nil { + t.Fatal(err) + } + if _, err = s.store.CreatePageAtPathInDomain(domain, "/drop/sub/seed"); err != nil { + t.Fatal(err) + } + script, err := os.ReadFile("internal/server/testdata/browser-upload.js") + if err != nil { + t.Fatal(err) + } + var active, peak atomic.Int32 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + r.Host = "localhost" + r.Header.Set("Authorization", "Bearer "+token) + if r.Method == "POST" && r.URL.Path == "/v/upload" { + n := active.Add(1) + defer active.Add(-1) + if n > peak.Load() { + peak.Store(n) + } + time.Sleep(100 * time.Millisecond) + } + if r.URL.Path == "/v/browser/drop" && r.URL.Query().Has("fixture") { + rec := httptest.NewRecorder() + s.withAuthorization(s.mux).ServeHTTP(rec, r) + for key, values := range rec.Header() { + w.Header()[key] = values + } + w.WriteHeader(rec.Code) + w.Write([]byte(strings.Replace(rec.Body.String(), "", "", 1))) + return + } + s.withAuthorization(s.mux).ServeHTTP(w, r) + })) + defer server.Close() + ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second) + defer cancel() + out, err := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", server.URL+"/v/browser/drop?fixture=1", t.TempDir(), "/tmp/pageswiki-browser-upload-"+width+".png", width, "850").CombinedOutput() + if err != nil { + t.Fatalf("browser: %v %s", err, out) + } + if peak.Load() != 1 { + t.Fatalf("parallel uploads: %d", peak.Load()) + } + for _, path := range []string{"/drop/日本語 #1.txt", "/drop/sub/nested.txt"} { + if _, err = s.store.ResolvePathInDomain(domain, path); err != nil { + t.Fatal(path, err) + } + } + if _, err = s.store.ResolvePathInDomain(domain, "/drop/cancel.txt"); err == nil { + t.Fatal("cancelled file uploaded") + } + revs, err := s.store.ListRevisions(object) + if err != nil || len(revs) != 1 { + t.Fatal("existing file overwritten") + } +} diff --git a/internal/server/compile_handler.go b/internal/server/compile_handler.go index bc2b634..99f2a94 100644 --- a/internal/server/compile_handler.go +++ b/internal/server/compile_handler.go @@ -45,7 +45,7 @@ func (s *Server) transpileMarkdownFromPath(r *http.Request, path string, mode ma html := latest.Body if mode == markdownProjection { - html = s.expandMacrosForRequest(r, html, path, s.domainUUIDForRequest(r)) + html = s.safeHTMLProjection(r, html, path, true) } else { return grammaratur.HTMLToMarkdownAuthoring(html) } diff --git a/internal/server/config_handler.go b/internal/server/config_handler.go index 65ce95c..b5f1b14 100644 --- a/internal/server/config_handler.go +++ b/internal/server/config_handler.go @@ -1078,49 +1078,50 @@ func (s *Server) handleConfig( s.getSystemNavItems(s.domainUUIDForRequest(r)), NavContext{}, ), - "HomePath": homePath, - "HomeWallpaperPath": homeWallpaperPath, - "HomeWallpaperOpacity": homeWallpaperOpacity, - "HomeGlassTransparency": s.homeGlassTransparency(domainUUID), - "HomePalette": s.homePalette(domainUUID), - "HomeLayout": s.homeLayout(s.domainUUIDForRequest(r)), - "HomeBookmarks": s.homeBookmarksText(s.domainUUIDForRequest(r)), - "VirtualFolderPages": s.getVirtualFolderPages(s.domainUUIDForRequest(r)), - "WikiName": wikiName, - "Locale": s.getLocale(s.domainUUIDForRequest(r)), - "Timezone": s.getTimezoneName(s.domainUUIDForRequest(r)), - "Theme": s.getTheme(s.domainUUIDForRequest(r)), - "ThemeOptions": themeOptions(), - "EditorKeymap": s.getEditorKeymap(s.domainUUIDForRequest(r)), - "EditorFormat": s.getEditorFormat(s.domainUUIDForRequest(r)), - "EditorPreviewDefault": s.getEditorPreviewDefault(s.domainUUIDForRequest(r)), - "EditorSyntax": s.getEditorSyntaxHighlight(s.domainUUIDForRequest(r)), - "EditorAutocomplete": s.getEditorAutocomplete(s.domainUUIDForRequest(r)), - "ZombieGraceHours": zombieGraceHours, - "Objects": recent, - "ZombieObjects": zombies, - "Domains": domains, - "DomainEntries": domainEntries, - "PeriodicalProjections": projections, - "PeriodicalKinds": []string{"diary", "blog"}, - "CampusLegacyPeers": legacyPeers, - "Campuses": campuses, - "CampusManaged": campusManaged, - "CampusOnline": campusOnline, - "FederationInstances": federationInstances, - "CampusTokens": campusTokens, - "AuthPrincipals": authPrincipals, - "AuthSessions": authSessions, - "AuthSessionMinutes": s.store.AuthSessionLifetimeMinutes(s.domainUUIDForRequest(r)), - "OIDCConfig": oidcConfig, - "SMTPConfig": smtpConfig, - "LDAPConfig": ldapConfig, - "LDAPTest": r.URL.Query().Get("ldap_test"), - "ObjectsCampusUUID": selectedCampusUUID, - "ObjectsDomainUUID": selectedDomain.UUID, - "ObjectsDomainHost": objectsDomainHost, - "HomeNavItemsConfig": s.getHomeNavItems(s.domainUUIDForRequest(r)), - "SystemNavItemsConfig": s.getSystemNavItems(s.domainUUIDForRequest(r)), + "HomePath": homePath, + "HomeWallpaperPath": homeWallpaperPath, + "HomeWallpaperOpacity": homeWallpaperOpacity, + "HomeGlassTransparency": s.homeGlassTransparency(domainUUID), + "HomePalette": s.homePalette(domainUUID), + "HomeLayout": s.homeLayout(s.domainUUIDForRequest(r)), + "HomeBookmarks": s.homeBookmarksText(s.domainUUIDForRequest(r)), + "VirtualFolderPages": s.getVirtualFolderPages(s.domainUUIDForRequest(r)), + "WikiName": wikiName, + "Locale": s.getLocale(s.domainUUIDForRequest(r)), + "Timezone": s.getTimezoneName(s.domainUUIDForRequest(r)), + "Theme": s.getTheme(s.domainUUIDForRequest(r)), + "ThemeOptions": themeOptions(), + "EditorKeymap": s.getEditorKeymap(s.domainUUIDForRequest(r)), + "EditorFormat": s.getEditorFormat(s.domainUUIDForRequest(r)), + "EditorPreviewDefault": s.getEditorPreviewDefault(s.domainUUIDForRequest(r)), + "EditorSyntax": s.getEditorSyntaxHighlight(s.domainUUIDForRequest(r)), + "EditorAutocomplete": s.getEditorAutocomplete(s.domainUUIDForRequest(r)), + "ZombieGraceHours": zombieGraceHours, + "Objects": recent, + "ZombieObjects": zombies, + "Domains": domains, + "DomainEntries": domainEntries, + "PeriodicalProjections": projections, + "PeriodicalKinds": []string{"diary", "blog"}, + "CampusLegacyPeers": legacyPeers, + "Campuses": campuses, + "CampusManaged": campusManaged, + "CampusOnline": campusOnline, + "FederationInstances": federationInstances, + "CampusTokens": campusTokens, + "AuthPrincipals": authPrincipals, + "AuthSessions": authSessions, + "AuthSessionMinutes": s.store.AuthSessionLifetimeMinutes(s.domainUUIDForRequest(r)), + "AuthTrustedSessionDays": s.store.TrustedSessionDays(s.domainUUIDForRequest(r)), + "OIDCConfig": oidcConfig, + "SMTPConfig": smtpConfig, + "LDAPConfig": ldapConfig, + "LDAPTest": r.URL.Query().Get("ldap_test"), + "ObjectsCampusUUID": selectedCampusUUID, + "ObjectsDomainUUID": selectedDomain.UUID, + "ObjectsDomainHost": objectsDomainHost, + "HomeNavItemsConfig": s.getHomeNavItems(s.domainUUIDForRequest(r)), + "SystemNavItemsConfig": s.getSystemNavItems(s.domainUUIDForRequest(r)), }, ) } diff --git a/internal/server/csv_macro.go b/internal/server/csv_macro.go new file mode 100644 index 0000000..4cbb405 --- /dev/null +++ b/internal/server/csv_macro.go @@ -0,0 +1,169 @@ +package server + +import ( + "encoding/csv" + "errors" + "html" + "io" + "net/http" + "net/url" + "path" + "regexp" + "strconv" + "strings" + "unicode/utf8" + + "github.com/google/uuid" +) + +// Projection only: no network fetch, shared rendered cache or write-back. +const maxCSVMacroBytes = 256 << 10 + +var csvMacroRE = regexp.MustCompile(`^\{\{csv:(?:"([^"]+)"|([^\s}]+))(.*?)\}\}$`) +var csvRangeRE = regexp.MustCompile(`^([A-Z]{1,2})([1-9][0-9]{0,3}):([A-Z]{1,2})([1-9][0-9]{0,3})$`) + +type csvMacroOptions struct { + Path, Revision string + Row, EndRow, Column, EndColumn int + Header bool +} + +func parseCSVMacro(raw string) (csvMacroOptions, error) { + o := csvMacroOptions{Row: 1, EndRow: 200, Column: 1, EndColumn: 32, Header: true} + m := csvMacroRE.FindStringSubmatch(raw) + bad := errors.New("invalid CSV macro") + if m == nil { + return o, bad + } + o.Path = m[1] + if o.Path == "" { + o.Path = m[2] + } + if !strings.HasPrefix(o.Path, "/") || strings.HasPrefix(o.Path, "//") || path.Clean(o.Path) != o.Path || strings.ContainsAny(o.Path, "\\\x00\r\n") || len(o.Path) > 2048 { + return o, bad + } + seen := map[string]bool{} + for _, field := range strings.Fields(m[3]) { + key, value, ok := strings.Cut(field, "=") + if !ok || seen[key] { + return o, bad + } + seen[key] = true + switch key { + case "header": + if value != "true" && value != "false" { + return o, bad + } + o.Header = value == "true" + case "rev": + if _, err := uuid.Parse(value); len(value) != 36 || err != nil { + return o, bad + } + o.Revision = value + case "range": + parts := csvRangeRE.FindStringSubmatch(strings.ToUpper(value)) + if parts == nil { + return o, bad + } + col := func(s string) int { + n := 0 + for _, c := range s { + n = n*26 + int(c-'A') + 1 + } + return n + } + o.Column, o.EndColumn = col(parts[1]), col(parts[3]) + o.Row, _ = strconv.Atoi(parts[2]) + o.EndRow, _ = strconv.Atoi(parts[4]) + if o.Column > o.EndColumn || o.EndColumn > 100 || o.EndColumn-o.Column >= 32 || o.Row > o.EndRow || o.EndRow > 2000 || o.EndRow-o.Row >= 200 { + return o, bad + } + default: + return o, bad + } + } + return o, nil +} + +func csvMacroTable(body []byte, o csvMacroOptions) (string, error) { + if len(body) > maxCSVMacroBytes || !utf8.Valid(body) || strings.IndexByte(string(body), 0) >= 0 { + return "", errors.New("invalid CSV input") + } + reader := csv.NewReader(strings.NewReader(strings.TrimPrefix(string(body), "\ufeff"))) + reader.FieldsPerRecord = -1 + var out strings.Builder + out.WriteString(`
`) + for row := 1; row <= o.EndRow; row++ { + record, err := reader.Read() + if err == io.EOF { + break + } + if err != nil { + return "", err + } + if len(record) > 100 { + return "", errors.New("too many CSV columns") + } + if row < o.Row { + continue + } + out.WriteString("") + cell := "td" + if o.Header && row == o.Row { + cell = "th" + } + for col := o.Column; col <= o.EndColumn && col <= len(record); col++ { + if len(record[col-1]) > 16<<10 { + return "", errors.New("CSV cell too large") + } + // Never expand macros, formulas or markup from CSV cells. + out.WriteString("<" + cell + ">" + html.EscapeString(record[col-1]) + "") + } + out.WriteString("") + } + out.WriteString("
` + html.EscapeString(path.Base(o.Path)) + `
") + return out.String(), nil +} + +func (s *Server) expandCSVMacro(r *http.Request, macro string) string { + fallback := `` + html.EscapeString(translate(s.getLocale(s.domainUUIDForRequest(r)), "viewer.csv_unavailable")) + `` + o, err := parseCSVMacro(macro) + if err != nil || r.Context().Err() != nil || s.store == nil || isAnonymousProjection(r) || !s.pathReadFilter(r)(o.Path) { + return fallback + } + domain := s.domainUUIDForRequest(r) + object, err := s.store.ResolvePathInDomainContext(r.Context(), domain, o.Path) + if err != nil { + return fallback + } + rev, err := s.store.GetLatestRevisionMetadataForLanguagesContext(r.Context(), object, s.revisionLanguageCandidates(r)) + if o.Revision != "" { + rev, err = s.store.GetRevisionMetadataContext(r.Context(), o.Revision) + } + if err != nil || rev == nil || rev.UUID == "" || rev.ObjectUUID != object { + return fallback + } + media := strings.ToLower(strings.TrimSpace(strings.Split(rev.ContentType, ";")[0])) + if media != "text/csv" && media != "application/csv" { + return fallback + } + size, err := s.store.DocumentSizeContext(r.Context(), rev.DocumentHash) + if err != nil || size > maxCSVMacroBytes { + return fallback + } + window, err := s.store.ReadDocumentWindowContext(r.Context(), rev.DocumentHash, 0, maxCSVMacroBytes) + if err != nil || window.Truncated { + return fallback + } + o.Revision = rev.UUID + table, err := csvMacroTable(window.Data, o) + if err != nil || r.Context().Err() != nil { + return fallback + } + return table +} + +func init() { + translations["en"]["viewer.csv_unavailable"] = "CSV unavailable (access, format or size limit)." + translations["ja"]["viewer.csv_unavailable"] = "CSVを表示できません(権限・形式・サイズ制限)。" +} diff --git a/internal/server/csv_macro_test.go b/internal/server/csv_macro_test.go new file mode 100644 index 0000000..41555f8 --- /dev/null +++ b/internal/server/csv_macro_test.go @@ -0,0 +1,111 @@ +package server + +import ( + "context" + "net/http/httptest" + "strings" + "testing" + + "pageswiki/internal/grammaratur" +) + +func TestCSVMacroParsingAndBounds(t *testing.T) { + for _, raw := range []string{`{{csv:/data/a.csv}}`, `{{csv:"/data/日本語 table.csv" range=B2:D4 header=false}}`} { + if _, err := parseCSVMacro(raw); err != nil { + t.Fatal(raw, err) + } + } + for _, raw := range []string{`{{csv:https://example.org/a.csv}}`, `{{csv://evil/a.csv}}`, `{{csv:/a/../secret}}`, `{{csv:/a range=A1:C201}}`, `{{csv:/a range=A1:AG2}}`, `{{csv:/a range=A2001:B2002}}`, `{{csv:/a range=D2:A1}}`, `{{csv:/a header=yes}}`, `{{csv:/a header=true header=false}}`, `{{csv:/a unsupported=1}}`, `{{csv:/a rev=bad}}`} { + if _, err := parseCSVMacro(raw); err == nil { + t.Fatal("accepted", raw) + } + } + o, _ := parseCSVMacro(`{{csv:/data/a.csv range=A1:C3}}`) + body := "\ufeffname,description,formula\r\n日本語,\"a,b\r\nnext\",=1+1\r\n` +const geckoShell = `Gecko

` func init() { translations["en"]["feeds.gecko"] = "Gecko RSS browser (experimental)" diff --git a/internal/server/feeds_gecko_browser_test.go b/internal/server/feeds_gecko_browser_test.go index 4a4e51b..1310357 100644 --- a/internal/server/feeds_gecko_browser_test.go +++ b/internal/server/feeds_gecko_browser_test.go @@ -23,17 +23,44 @@ import ( // The real engine runs against a synthetic Wisp HTTP responder. This test never // dials an article server, production source, DNS name or external calendar. func TestFeedGeckoChromium(t *testing.T) { + testFeedGeckoChromium(t, false) +} + +func TestFeedGeckoStartupFailureChromium(t *testing.T) { + testFeedGeckoChromium(t, true) +} + +func testFeedGeckoChromium(t *testing.T, failure bool) { assets := os.Getenv("PAGESWIKI_GECKO_TEST_ASSETS") if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" || assets == "" { t.Skip("opt-in real Gecko assets") } var connections atomic.Int64 + publicHTTPS := !failure && os.Getenv("PAGESWIKI_GECKO_PUBLIC_HTTPS_TEST") == "1" + realRelay, err := geckorelay.New(geckoTestKey, assets) + if err != nil { + t.Fatal(err) + } + if publicHTTPS { + realRelay.Distribution, err = geckorelay.VerifyDistribution(assets) + if err != nil { + t.Fatal(err) + } + } backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if failure && strings.HasSuffix(r.URL.Path, "/NotoSansCJKjp-Regular.otf") { + http.Error(w, "synthetic startup failure", 503) + return + } + if publicHTTPS { + realRelay.ServeHTTP(w, r) + return + } if _, err := geckorelay.Verify(geckoTestKey, r.Header.Get(geckorelay.GrantHeader), time.Now()); err != nil { http.Error(w, "bad grant", 403) return } - if strings.HasPrefix(r.URL.Path, "/assets/") { + if strings.HasPrefix(r.URL.Path, "/assets/") || strings.HasPrefix(r.URL.Path, geckorelay.DistributionPrefix) { http.ServeFile(w, r, filepath.Join(assets, filepath.Base(r.URL.Path))) return } @@ -67,7 +94,7 @@ func TestFeedGeckoChromium(t *testing.T) { if !strings.Contains(requests[id], "\r\n\r\n") { continue } - body := `Synthetic next page

Private fixture

` + body := `Synthetic next page

日本語の記事:学舎の園

` if strings.HasPrefix(requests[id], "GET /next ") { body = `

Second synthetic page

` } @@ -96,11 +123,26 @@ func TestFeedGeckoChromium(t *testing.T) { if e != nil { t.Fatal(e) } - if e = s.store.CompleteFeed(context.Background(), claim, []store.FeedItem{{ID: "synthetic", Title: "Gecko synthetic fixture", URL: "http://fixture.invalid/first", Body: "No external network access."}}, "", "", "", false); e != nil { + articleURL := "http://fixture.invalid/first" + if publicHTTPS { + articleURL = "https://example.com/" + } + if e = s.store.CompleteFeed(context.Background(), claim, []store.FeedItem{{ID: "synthetic", Title: "Gecko synthetic fixture", URL: articleURL, Body: "Synthetic test fixture."}}, "", "", "", false); e != nil { t.Fatal(e) } handler := s.handler() web := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if publicHTTPS && r.URL.Path == "/static/js/feed-gecko.js" { + js, err := os.ReadFile("static/js/feed-gecko.js") + if err != nil { + t.Error(err) + http.Error(w, "fixture missing", 500) + return + } + w.Header().Set("Content-Type", "text/javascript") + io.WriteString(w, strings.Replace(string(js), "engine=new Gecko(", "globalThis.fixtureEngine=engine=new Gecko(", 1)) + return + } if r.Header.Get("Origin") == "http://"+r.Host { r.Header.Set("Origin", "http://localhost") } @@ -111,7 +153,14 @@ func TestFeedGeckoChromium(t *testing.T) { defer web.Close() ctx, cancel := context.WithTimeout(context.Background(), 150*time.Second) defer cancel() - out, e := exec.CommandContext(ctx, "node", "internal/server/testdata/feed-gecko-browser.mjs", web.URL+"/v/feeds", t.TempDir(), "/tmp/pageswiki-feed-gecko.png").CombinedOutput() + mode := "synthetic" + if failure { + mode = "startup-failure" + } + if publicHTTPS { + mode = "public-https" + } + out, e := exec.CommandContext(ctx, "node", "internal/server/testdata/feed-gecko-browser.mjs", web.URL+"/v/feeds", t.TempDir(), "/tmp/pageswiki-feed-gecko.png", mode).CombinedOutput() if e != nil { t.Fatalf("browser: %v\n%s", e, out) } diff --git a/internal/server/feeds_gecko_test.go b/internal/server/feeds_gecko_test.go index 18825d9..a81bf59 100644 --- a/internal/server/feeds_gecko_test.go +++ b/internal/server/feeds_gecko_test.go @@ -57,7 +57,7 @@ func TestFeedGeckoAuthorizationAndHeaders(t *testing.T) { t.Error("credential/header leaked", h) } } - if r.URL.Path != "/assets/v0.0.1/gecko.js" { + if r.URL.Path != "/assets/v0.0.1/gecko.js" && r.URL.Path != "/distribution/v0.0.1/manifest.json" { t.Error("path", r.URL.Path) } w.Header().Set("Set-Cookie", "forbidden=1") @@ -133,6 +133,18 @@ func TestFeedGeckoAuthorizationAndHeaders(t *testing.T) { if w = feedRequestTest(s, token, "GET", "browser/", nil); w.Code != 404 { t.Fatal("disabled shell", w.Code) } + if err = s.setSystemExtensionEnabled(domain, "organizer.feeds", false); err != nil { + t.Fatal(err) + } + if w = feedRequestTest(s, token, "GET", "browser/distribution/v0.0.1/manifest.json", nil); w.Code != 200 { + t.Fatal("sources lost after disabling extensions", w.Code) + } + if w = feedRequestTest(s, "", "GET", "browser/distribution/v0.0.1/manifest.json", nil); w.Code == 200 { + t.Fatal("anonymous source download") + } + if err = s.setSystemExtensionEnabled(domain, "organizer.feeds", true); err != nil { + t.Fatal(err) + } if err = s.setSystemExtensionEnabled(domain, "viewer.gecko", true); err != nil { t.Fatal(err) } @@ -142,6 +154,9 @@ func TestFeedGeckoAuthorizationAndHeaders(t *testing.T) { if w = feedRequestTest(s, token, "GET", "browser/", nil); w.Code != 404 { t.Fatal("readonly browser", w.Code) } + if w = feedRequestTest(s, token, "GET", "browser/distribution/v0.0.1/manifest.json", nil); w.Code != 200 { + t.Fatal("readonly recipient lost source access", w.Code) + } } func TestFeedGeckoWebsocketRevocation(t *testing.T) { diff --git a/internal/server/feeds_test.go b/internal/server/feeds_test.go index bde42a9..a5533ff 100644 --- a/internal/server/feeds_test.go +++ b/internal/server/feeds_test.go @@ -531,6 +531,9 @@ func TestFeedsChromium(t *testing.T) { t.Skip("opt-in Chromium") } s, token, domain, actor, source := feedFixture(t) + if err := s.store.SetConfigInDomain(domain, "timezone", "Asia/Tokyo"); err != nil { + t.Fatal(err) + } prefs, err := s.store.TaskPreferences(context.Background(), domain, actor) if err != nil { t.Fatal(err) diff --git a/internal/server/html_projection.go b/internal/server/html_projection.go index 97555c2..cc2d36c 100644 --- a/internal/server/html_projection.go +++ b/internal/server/html_projection.go @@ -20,11 +20,11 @@ import ( "golang.org/x/net/html/atom" ) -const htmlProjectionVersion = "controlled-v2" +const htmlProjectionVersion = "controlled-v3" const maxProjectionInput = 1 << 20 const maxProjectionOutput = 4 << 20 -var projectionMacroRE = regexp.MustCompile(`\{\{(?:children[^}]*|(?:city-breadcrumbs|spot-pages|quartier|street|spot|nearby|city)(?:\s+[^}]*)?)\}\}`) +var projectionMacroRE = regexp.MustCompile(`\{\{(?:csv:[^}]*|children[^}]*|(?:city-breadcrumbs|spot-pages|quartier|street|spot|nearby|city)(?:\s+[^}]*)?)\}\}`) var languageClassRE = regexp.MustCompile(`^language-[a-zA-Z0-9_+-]{1,40}$`) func projectionPolicy(generated bool) *bluemonday.Policy { @@ -199,6 +199,8 @@ func (s *Server) safeHTMLProjection(r *http.Request, body, path string, expand b } var output strings.Builder macros, count := 0, 0 + csvCount := 0 + csvResults := map[string]string{} var emit func(*html.Node, int, bool) bool emit = func(n *html.Node, depth int, literal bool) bool { count++ @@ -226,6 +228,14 @@ func (s *Server) safeHTMLProjection(r *http.Request, body, path string, expand b // be reparsed as a nested children request on its raw text. if isAnonymousProjection(r) { expanded = s.expandPublicMacro(r, macro, path) + } else if strings.HasPrefix(macro, "{{csv:") { + if cached, ok := csvResults[macro]; ok { + expanded = cached + } else if csvCount < 4 { + csvCount++ + expanded = s.expandCSVMacro(r, macro) + csvResults[macro] = expanded + } } else if strings.HasPrefix(macro, "{{children") { expanded = s.expandChildrenMacros(macro, path) } else { @@ -236,6 +246,9 @@ func (s *Server) safeHTMLProjection(r *http.Request, body, path string, expand b // promote decoded entities into trusted generated elements. output.WriteString(stdhtml.EscapeString(macro)) } else { + if output.Len()+len(expanded) > maxProjectionOutput { + return false + } output.WriteString(expanded) } at = loc[1] diff --git a/internal/server/smtp_delivery.go b/internal/server/smtp_delivery.go index ed7000e..c80a856 100644 --- a/internal/server/smtp_delivery.go +++ b/internal/server/smtp_delivery.go @@ -36,8 +36,9 @@ func (c *smtpLimitedConn) Read(buffer []byte) (int, error) { } type magicLinkChallenge struct { - Return string `json:"return"` - Email string `json:"email"` + Return string `json:"return"` + Email string `json:"email"` + Remember bool `json:"remember,omitempty"` } func smtpMailbox(value string) (*mail.Address, error) { diff --git a/internal/server/testdata/auth-browser.js b/internal/server/testdata/auth-browser.js index d4be895..3131206 100644 --- a/internal/server/testdata/auth-browser.js +++ b/internal/server/testdata/auth-browser.js @@ -4,6 +4,10 @@ if (phase === 'login') { const form = document.querySelector('form[action="/_/auth/login"]') assert(form, 'Login form missing') + const remember = form.elements.remember + assert(remember && !remember.checked, 'Trusted login must be opt-in') + remember.checked = true + remember.dispatchEvent(new Event('change', { bubbles: true })) form.elements.username.value = 'admin' form.elements.password.value = 'a long test password' sessionStorage.setItem('auth-flow-phase', 'authenticated') diff --git a/internal/server/testdata/browser-upload.js b/internal/server/testdata/browser-upload.js new file mode 100644 index 0000000..585fc8d --- /dev/null +++ b/internal/server/testdata/browser-upload.js @@ -0,0 +1,17 @@ +(async()=>{ + const wait=async fn=>{for(let i=0;i<200;i++){if(fn())return;await new Promise(r=>setTimeout(r,30));}throw Error('Timeout '+fn);}; + const assert=(ok,msg)=>{if(!ok)throw Error(msg);}; + await wait(()=>document.querySelector('.browser-upload-queue')); + const drop=(target,files)=>{const transfer=new DataTransfer();for(const [name,body]of files)transfer.items.add(new File([body],name,{type:'text/plain'}));const event=new DragEvent('drop',{bubbles:true,cancelable:true,dataTransfer:transfer});target.dispatchEvent(event);assert(event.defaultPrevented,'File drop did not prevent navigation');}; + const main=document.querySelector('.browser-main'); + drop(main,[['日本語 #1.txt','new file'],['existing.txt','must not replace'],['cancel.txt','not sent']]); + const queued=[...document.querySelectorAll('.browser-upload-queue li')];queued[2].querySelector('button').click(); + drop(document.querySelector('.browser-row[data-path="/drop/sub"]'),[['nested.txt','nested']]); + await wait(()=>document.querySelectorAll('.browser-upload-queue [data-state=done]').length===2&&document.querySelectorAll('.browser-upload-queue [data-state=failed]').length===1); + await wait(()=>document.querySelector('.browser-row[data-path="/drop/日本語 #1.txt"]')); + assert(document.querySelectorAll('.browser-upload-queue [data-state=cancelled]').length===1,'Cancellation missing'); + const card=document.querySelector('.browser-upload-queue'),rect=card.getBoundingClientRect();assert(getComputedStyle(card).position==='fixed'&&rect.right<=innerWidth&&rect.bottom<=innerHeight,'Queue not floating within viewport'); + const toggle=card.querySelector('[aria-controls]');toggle.click();assert(getComputedStyle(card.querySelector('ol')).display==='none','Queue collapse failed');toggle.click(); + assert(document.querySelectorAll('.browser-upload-queue progress[value="100"]').length===2,'Completion progress missing'); + document.body.dataset.vimReady='true'; +})().catch(e=>{const p=document.createElement('pre');p.id='error';p.textContent=e.stack;document.body.append(p);}); diff --git a/internal/server/testdata/feed-gecko-browser.mjs b/internal/server/testdata/feed-gecko-browser.mjs index 3985921..77d94a3 100644 --- a/internal/server/testdata/feed-gecko-browser.mjs +++ b/internal/server/testdata/feed-gecko-browser.mjs @@ -1,7 +1,7 @@ import {spawn} from 'node:child_process'; import {writeFile} from 'node:fs/promises'; import {setTimeout as delay} from 'node:timers/promises'; -const [url,profile,screenshot]=process.argv.slice(2); +const [url,profile,screenshot,mode]=process.argv.slice(2); const browser=spawn('chromium',['--headless','--no-sandbox','--disable-gpu','--disable-background-networking','--no-proxy-server','--remote-debugging-port=0',`--user-data-dir=${profile}`,'about:blank']); process.on('SIGTERM',()=>{browser.kill();process.exit(1);}); let socket,logs=[]; @@ -17,24 +17,50 @@ try { const wait=async(expression,seconds=15)=>{for(let n=0;n{const init=Gecko.prototype.init;Gecko.prototype.init=function(){this.opts.printErr=console.warn;this.opts.print=console.log;return init.call(this);};});`}); + await call('Page.addScriptToEvaluateOnNewDocument',{source:`if(location.pathname.endsWith('/browser/'))import('/_/api/v1/extension/organizer.feeds/browser/assets/v0.0.1/gecko.js').then(({Gecko})=>{const init=Gecko.prototype.init;Gecko.prototype.init=function(){window.fixtureEngine=this;this.opts.printErr=console.warn;this.opts.print=console.log;return init.call(this);};});`}); await call('Emulation.setDeviceMetricsOverride',{width:1280,height:850,deviceScaleFactor:1,mobile:false});await call('Page.navigate',{url}); await wait(`document.querySelector('.feed-row')`); if(!await evaluate('crossOriginIsolated'))throw Error('Parent not isolated'); await evaluate(`window.geckoReady=false;addEventListener('message',e=>{if(e.data?.type==='pageswiki-gecko'&&e.data.ready)geckoReady=true;});document.querySelector('.feed-row').click()`); await wait(`document.querySelector('.feed-original')`);await evaluate(`document.querySelector('.feed-original').click()`); await wait(`document.querySelector('.feed-external iframe')?.contentWindow.crossOriginIsolated`); + if(mode==='startup-failure'){ + await wait(`document.querySelector('.feed-external [role=alert]')&&!document.querySelector('.feed-external iframe')`); + if(!await evaluate(`document.querySelector('.feed-external a[target=_blank]').href==='http://fixture.invalid/first'`))throw Error('Failure lost original fallback'); + await evaluate(`document.querySelector('.feed-external').close()`); + console.log('Gecko startup failure discarded iframe and retained original fallback'); + }else{ await wait(`geckoReady || document.querySelector('.feed-external iframe')?.contentDocument?.querySelector('#status')?.textContent.includes('Viewer failed')`,100); if(!await evaluate('geckoReady'))throw Error('Gecko startup failed\n'+logs.slice(-8).join('\n')); + if(mode==='public-https'){ + const engine=`document.querySelector('.feed-external iframe').contentWindow.fixtureEngine`; + await wait(`(async()=>/documentation examples/i.test(String(await ${engine}.evalChrome('document.body.textContent'))))()`,30); + await evaluate(`${engine}.load('https://expired.badssl.com/')`); + await wait(`(async()=>/^about:(neterror|certerror)/.test(String(await ${engine}.evalChrome('String(document.documentURI)'))))()`,30); + const location=await evaluate(`${engine}.evalChrome('String(document.documentURI)')`); + if(!String(location).startsWith('about:neterror')&&!String(location).startsWith('about:certerror'))throw Error('Expired certificate was not rejected: '+location); + await wait(`document.querySelector('.feed-external iframe').contentDocument.querySelector('canvas').hidden && document.querySelector('.feed-external iframe').contentDocument.querySelector('#status').textContent.length>20`); + await evaluate(`document.querySelector('.feed-external').close()`); + console.log('Public HTTPS example.com rendered; expired.badssl.com certificate rejected; closed'); + }else{ const pixel=`(()=>{const c=document.querySelector('.feed-external iframe').contentDocument.querySelector('canvas');return Array.from(c.getContext('2d').getImageData(400,200,1,1).data)})()`; await wait(`${pixel}[0]===255 && ${pixel}[1]===0`); + await evaluate(`document.querySelector('.feed-external button[title*="Ctrl+F"]').click();document.querySelector('.feed-gecko-find input').value='学舎の園';document.querySelector('.feed-gecko-find').requestSubmit()`); + await wait(`(async()=>String(await document.querySelector('.feed-external iframe').contentWindow.fixtureEngine.evalChrome('String(window.getSelection())'))==='学舎の園')()`); + await wait(`getComputedStyle(document.querySelector('.feed-gecko-find')).display==='flex'`); + await evaluate(`document.querySelector('.feed-gecko-find input').value='missing-synthetic-word';document.querySelector('.feed-gecko-find').requestSubmit()`); + await wait(`document.querySelector('.feed-gecko-find output').textContent.length>0`); + await evaluate(`document.querySelector('.feed-gecko-find button:last-child').click()`); + await wait(`getComputedStyle(document.querySelector('.feed-gecko-find')).display==='none'`); const rect=await evaluate(`(()=>{const r=document.querySelector('.feed-external iframe').getBoundingClientRect();return {x:r.x+100,y:r.y+50}})()`); await call('Input.dispatchMouseEvent',{type:'mousePressed',...rect,button:'left',buttons:1,clickCount:1});await call('Input.dispatchMouseEvent',{type:'mouseReleased',...rect,button:'left',buttons:0,clickCount:1}); await wait(`${pixel}[0]===0 && ${pixel}[1]===255`); await wait(`document.querySelector('.feed-external a').href==='http://fixture.invalid/next'`); - await evaluate(`document.querySelector('.feed-external button').click()`); + await evaluate(`[...document.querySelectorAll('.feed-external-bar button')].find(b=>b.textContent==='←').click()`); await wait(`${pixel}[0]===255 && ${pixel}[1]===0`); if(screenshot){const {data}=await call('Page.captureScreenshot',{format:'png'});await writeFile(screenshot,Buffer.from(data,'base64'));} await evaluate(`document.querySelector('.feed-external').close()`);await wait(`!document.querySelector('.feed-external')`); - console.log('Real Gecko: isolation, Wisp article render, click navigation, viewer-local back and teardown passed'); + console.log('Real Gecko: isolation, Japanese find/missing match, Wisp article render, click navigation, viewer-local back and teardown passed'); + } + } }finally{socket?.close();browser.kill();await new Promise(resolve=>browser.once('exit',resolve));await delay(300);} diff --git a/internal/server/testdata/feeds.js b/internal/server/testdata/feeds.js index e24df08..6b8400c 100644 --- a/internal/server/testdata/feeds.js +++ b/internal/server/testdata/feeds.js @@ -9,6 +9,7 @@ w.fetch=async(...args)=>{pendingRequests++;try{const response=await fetch(...args);await response.clone().arrayBuffer();return response;}finally{pendingRequests--;}}; for(const key of Object.keys(w.localStorage))if(key.startsWith('pageswiki.feed-folders.v1:'))w.localStorage.removeItem(key); await wait(()=>d.querySelectorAll('.feed-row').length===100); + const published=d.querySelector('.feed-published-time');assert(/^\d{2}:\d{2}$/.test(published.textContent),'Publication time missing');assert(published.title.includes('2026')&&published.title.includes('JST'),'Publication timezone/full tooltip missing');assert(w.getComputedStyle(published).display!=='none','Publication time hidden on mobile');assert(d.querySelector('.feed-row').children.length===5,'Publication time column missing'); assert(d.documentElement.scrollWidth<=w.innerWidth,'Viewport overflow'); assert(d.title.startsWith('RSS ·'),'RSS title');assert(d.getElementById('feed-status').textContent==='','Unrequested status copy');assert(w.getComputedStyle(d.getElementById('feed-status')).display==='none','Empty status takes space'); for(const a of d.querySelectorAll('.organizer-nav a')){assert(a.getAttribute('aria-label'),'Unnamed navigation');if(w.innerWidth<=700)assert(w.getComputedStyle(a.querySelector('span')).display==='none','Mobile tab label visible');} @@ -62,6 +63,7 @@ tech().querySelector('.feed-folder-toggle').click();await wait(()=>tech().querySelector('[data-feed="feed/1"]')); tech().querySelector('[data-feed="feed/1"]').click();await wait(()=>d.querySelector('.feed-row')?.textContent.includes('Catalog first')); const globalSearch=d.querySelector('#feed-toolbar input');globalSearch.value='Catalog';globalSearch.dispatchEvent(new w.Event('input',{bubbles:true}));await delay(220); + folderKey('H');await wait(()=>activeFolder()===tech().querySelector('.feed-folder-name')); folderKey('K');await wait(()=>activeFolder()===folderNames()[1]&&d.querySelector('.feed-row')?.textContent.includes('Catalog second')); assert(globalSearch.value==='Catalog'&&!d.querySelector('button[data-feed][aria-current="true"]'),'Folder navigation lost search or kept subscription filter'); d.querySelector('.feed-row').click();await wait(()=>d.querySelector('#feed-reader .feed-body')); @@ -71,14 +73,25 @@ d.querySelector('.feed-capture').close();await wait(()=>!d.querySelector('.feed-capture')); folderKey('J');await wait(()=>activeFolder()===tech().querySelector('.feed-folder-name')&&d.querySelector('.feed-row')?.textContent.includes('Catalog first')); assert(!d.querySelector('.feed-layout').classList.contains('reading')&&!d.querySelector('#feed-reader .feed-body'),'Folder navigation left old article open'); - catalogFind.value='Other';catalogFind.dispatchEvent(new w.Event('input',{bubbles:true}));assert(!activeFolder(),'Catalog filter setup'); + const currentCatalogFind=d.querySelector('.feed-catalog input');currentCatalogFind.value='Other';currentCatalogFind.dispatchEvent(new w.Event('input',{bubbles:true}));assert(!activeFolder(),'Catalog filter setup'); folderKey('K');await wait(()=>activeFolder()===folderNames()[1]&&d.querySelector('.feed-row')?.textContent.includes('Catalog second')); assert(d.querySelector('.feed-catalog input').value==='','Folder navigation left selected folder hidden by catalog filter'); globalSearch.value='';globalSearch.dispatchEvent(new w.Event('input',{bubbles:true}));await delay(220); // Rapid repeats must settle on the latest folder rather than an older request. folderKey('J');folderKey('J');folderKey('K');await wait(()=>activeFolder()===tech().querySelector('.feed-folder-name')&&d.querySelector('.feed-row')?.textContent.includes('Catalog first'));await delay(100); assert(d.querySelectorAll('.feed-row').length===1&&d.querySelector('.feed-row').textContent.includes('Catalog first'),'Stale folder request won'); - tech().querySelector('.feed-folder-toggle').click();await wait(()=>tech().querySelector('.feed-folder-toggle').getAttribute('aria-expanded')==='false'); + // Horizontal hierarchy navigation, with unchanged article filters and bounded siblings. + folderKey('L');await wait(()=>tech().querySelector('button[data-feed="feed/1"][aria-current="true"]')); + assert(tech().querySelector('.feed-folder-toggle').getAttribute('aria-expanded')==='true','L did not expand folder'); + folderKey('L');folderKey('J');folderKey('K');await delay(80); + assert(tech().querySelector('button[data-feed="feed/1"][aria-current="true"]'),'Leaf keys escaped the current folder'); + folderKey('H');await wait(()=>activeFolder()===tech().querySelector('.feed-folder-name')); + folderKey('H');await wait(()=>d.querySelector('.feed-tree-all[aria-current="true"]')); + assert(tech().querySelector('.feed-folder-toggle').getAttribute('aria-expanded')==='false','H did not collapse folder'); + folderKey('H');await delay(80);assert(d.querySelector('.feed-tree-all[aria-current="true"]'),'H escaped catalog root'); + folderKey('L');await wait(()=>activeFolder()===folderNames()[0]); + folderKey('L');await delay(80);assert(activeFolder()===folderNames()[0]&&!folderNames()[0].closest('.feed-folder').querySelector('button[data-feed]'),'L fabricated an empty-folder child'); + tech().querySelector('.feed-folder-name').click();await wait(()=>activeFolder()===tech().querySelector('.feed-folder-name')); tech().querySelector('.feed-folder-name').click();await wait(()=>d.querySelectorAll('.feed-row').length===1&&d.querySelector('.feed-row').textContent.includes('Catalog first')); assert(tech().querySelector('.feed-folder-toggle').getAttribute('aria-expanded')==='false','Selecting folder expands it unexpectedly');tech().querySelector('.feed-folder-toggle').click();await wait(()=>tech().querySelector('button[data-feed="feed/1"]')); const empty=folder('user/-/label/Empty/Folder');assert(empty.querySelector('.feed-folder-name span').textContent==='Empty/Folder','Slash label split');empty.querySelector('.feed-folder-toggle').click();assert(!empty.querySelector('button[data-feed]'),'Empty folder fabricated a feed');empty.querySelector('.feed-folder-name').click();await wait(()=>d.querySelectorAll('.feed-row').length===0); diff --git a/internal/server/testdata/tasks-browser.js b/internal/server/testdata/tasks-browser.js index 865bebc..3760919 100644 --- a/internal/server/testdata/tasks-browser.js +++ b/internal/server/testdata/tasks-browser.js @@ -132,7 +132,7 @@ window.addEventListener('load', async () => { await wait(()=>$('status').textContent.includes('ありません')); // Deletion shares the archive footer, not a UUID-search workflow. $('new').click();await wait(()=>document.querySelector('#tasks-form [name=title]')); - if(!$('delete').hidden)throw Error('Unsaved task must not offer deletion');$('close').click(); + if(!$('delete').hidden)throw Error('Unsaved task must not offer deletion');if(!$('page-open').hidden||$('page-open').hasAttribute('href'))throw Error('New task leaked a related page');$('close').click(); $('quick-title').value='Disposable deletion fixture';$('capture').requestSubmit(); await wait(()=>$('list').querySelector('[data-action=detail]'));$('list').querySelector('[data-action=detail]').click(); await wait(()=>!$('delete').hidden); @@ -142,7 +142,19 @@ window.addEventListener('load', async () => { if(!$('dialog').open)throw Error('Cancelled deletion closed the editor'); const deletionAPI='/_/api/v1/extension/organizer.tasks/items/'+deletionID; const deletionItem=(await fetch(deletionAPI).then(r=>r.json())).item; - const changed=await fetch(deletionAPI,{method:'PUT',headers:{'Content-Type':'application/json','If-Match':'"'+deletionItem.version+'"'},body:JSON.stringify({...deletionItem,title:'Updated deletion fixture'})}); + if(!$('page-open').hidden)throw Error('Unlinked task offered related page'); + // The footer link uses the saved, authorized path, never unsaved input. + value('page_path','/projects/browser');$('form').requestSubmit();await wait(()=>!$('dialog').open); + $('list').querySelector('[data-action=detail]').click();await wait(()=>!$('page-open').hidden); + const link=$('page-open');if(link.parentElement!==$('save').parentElement||link.pathname!=='/projects/browser'||link.target!=='_blank'||!link.rel.includes('noopener'))throw Error('Unsafe or misplaced related page navigation'); + value('title','Unsaved title kept');if(link.hidden||!$('dialog').open)throw Error('Editing task hid the saved page navigation'); + if(innerWidth<761&&link.getBoundingClientRect().height<44)throw Error('Related page touch target too small'); + document.querySelector('.task-editor-advanced').open=true; + value('page_path','//external.invalid/unsaved');if(!link.hidden||link.hasAttribute('href'))throw Error('Unsaved path exposed a link'); + value('page_path','/projects/browser');if(link.hidden)throw Error('Restored path did not restore related link'); + window.confirm=()=>true;$('close').click();$('list').querySelector('[data-action=detail]').click();await wait(()=>!$('page-open').hidden); + const freshDeletionItem=(await fetch(deletionAPI).then(r=>r.json())).item; + const changed=await fetch(deletionAPI,{method:'PUT',headers:{'Content-Type':'application/json','If-Match':'"'+freshDeletionItem.version+'"'},body:JSON.stringify({...freshDeletionItem,title:'Updated deletion fixture'})}); if(!changed.ok)throw Error('Deletion conflict fixture failed'); window.confirm=()=>true;$('delete').click();await wait(()=>$('error').textContent.includes('別の画面')); if(!$('dialog').open||$('delete').disabled)throw Error('Deletion failure did not keep a usable editor'); diff --git a/internal/server/upload_handler.go b/internal/server/upload_handler.go index a7a4813..3f9c6fa 100644 --- a/internal/server/upload_handler.go +++ b/internal/server/upload_handler.go @@ -39,6 +39,7 @@ func (s *Server) handleUpload(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusRequestEntityTooLarge) return } + defer r.MultipartForm.RemoveAll() file, header, err := r.FormFile("file") if err != nil { http.Error(w, "file is required", http.StatusBadRequest) @@ -83,6 +84,11 @@ func (s *Server) handleUpload(w http.ResponseWriter, r *http.Request) { } } objectUUID, err := s.store.ResolvePathInDomain(domainUUID, targetPath) + createOnly := r.Header.Get("If-None-Match") == "*" + if err == nil && createOnly { + http.Error(w, "file already exists", http.StatusPreconditionFailed) + return + } if err == nil && mode == "enforcing" { // upload and write-once grants never overwrite an existing object. if s.store.Authorize(store.AuthorizationRequest{DomainUUID: domainUUID, PrincipalUUID: principal.UUID, Capability: "write", TargetType: "path", TargetID: targetPath}) != nil { @@ -92,6 +98,13 @@ func (s *Server) handleUpload(w http.ResponseWriter, r *http.Request) { } if errors.Is(err, sql.ErrNoRows) { objectUUID, err = s.store.CreatePageAtPathInDomain(domainUUID, targetPath) + if err != nil && createOnly { + // A concurrent creator wins; never turn this into an overwrite. + if _, resolveErr := s.store.ResolvePathInDomain(domainUUID, targetPath); resolveErr == nil { + http.Error(w, "file already exists", http.StatusPreconditionFailed) + return + } + } } if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) @@ -113,17 +126,23 @@ func (s *Server) handleUpload(w http.ResponseWriter, r *http.Request) { if strings.TrimSpace(language) == "" { language = "und" } - if _, err := s.store.SaveRevisionContent( + revision, err := s.store.SaveRevisionContent( objectUUID, payload, "web upload: "+originalName, language, contentType, originalName, - ); err != nil { + ) + if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } + if strings.Contains(r.Header.Get("Accept"), "application/json") { + w.Header().Set("Cache-Control", "private, no-store") + writeEventJSON(w, http.StatusCreated, map[string]string{"path": targetPath, "object_uuid": objectUUID, "revision_uuid": revision.UUID}) + return + } http.Redirect(w, r, targetPath, http.StatusSeeOther) } diff --git a/internal/server/upload_handler_test.go b/internal/server/upload_handler_test.go index 3994a57..c8846ee 100644 --- a/internal/server/upload_handler_test.go +++ b/internal/server/upload_handler_test.go @@ -2,6 +2,7 @@ package server import ( "bytes" + "encoding/json" "mime/multipart" "net/http" "net/http/httptest" @@ -85,6 +86,51 @@ func TestWebUploadCreatesAndRevisesSpecifiedPath(t *testing.T) { } } +func TestWebUploadCreateOnlyJSON(t *testing.T) { + s, token, domain := configFixture(t) + upload := func(payload string, authenticated bool) *httptest.ResponseRecorder { + var body bytes.Buffer + writer := multipart.NewWriter(&body) + writer.WriteField("path", "/uploads/日本語 #1.txt") + file, _ := writer.CreateFormFile("file", "日本語 #1.txt") + file.Write([]byte(payload)) + writer.Close() + r := httptest.NewRequest("POST", "http://localhost/v/upload", &body) + r.Header.Set("Content-Type", writer.FormDataContentType()) + r.Header.Set("Accept", "application/json") + r.Header.Set("If-None-Match", "*") + if authenticated { + r.Header.Set("Authorization", "Bearer "+token) + } + w := httptest.NewRecorder() + s.handleUpload(w, r) + return w + } + if w := upload("denied", false); w.Code != 403 { + t.Fatalf("unauthorized: %d", w.Code) + } + w := upload("original", true) + var result map[string]string + if w.Code != 201 || json.Unmarshal(w.Body.Bytes(), &result) != nil || result["path"] != "/uploads/日本語 #1.txt" || result["revision_uuid"] == "" { + t.Fatalf("JSON upload %d %s", w.Code, w.Body.String()) + } + if w = upload("overwrite", true); w.Code != 412 { + t.Fatalf("duplicate: %d %s", w.Code, w.Body.String()) + } + object, err := s.store.ResolvePathInDomain(domain, result["path"]) + if err != nil { + t.Fatal(err) + } + revisions, err := s.store.ListRevisions(object) + if err != nil || len(revisions) != 1 { + t.Fatalf("unexpected revisions %d %v", len(revisions), err) + } + rev, err := s.store.GetRevision(revisions[0].UUID) + if err != nil || rev.Body != "original" { + t.Fatal("overwritten", err) + } +} + func mustServerDomainUUID(t *testing.T, st *store.Store) string { t.Helper() domain, err := st.GetDefaultDomain() diff --git a/internal/store/auth.go b/internal/store/auth.go index b3d0f31..6006872 100644 --- a/internal/store/auth.go +++ b/internal/store/auth.go @@ -164,6 +164,10 @@ func (s *Store) CreateBrowserAuthSession(domainUUID, principalUUID string, mfaLe } func (s *Store) createTypedAuthSession(domainUUID, principalUUID string, mfaLevel int, prefix string) (string, error) { + return s.createTypedAuthSessionForLifetime(domainUUID, principalUUID, mfaLevel, prefix, s.AuthSessionLifetime(domainUUID)) +} + +func (s *Store) createTypedAuthSessionForLifetime(domainUUID, principalUUID string, mfaLevel int, prefix string, lifetime time.Duration) (string, error) { raw, _, err := authToken() if err != nil { return "", err @@ -177,7 +181,7 @@ func (s *Store) createTypedAuthSession(domainUUID, principalUUID string, mfaLeve result, err := s.DB.Exec(`INSERT INTO auth_sessions(uuid,token_hash,principal_uuid,domain_uuid,auth_time_ms,expires_at_ms,last_seen_at_ms,mfa_level) SELECT ?,?,p.uuid,m.domain_uuid,?,?,?,? FROM principals p JOIN domain_memberships m ON m.principal_uuid=p.uuid WHERE p.uuid=? AND p.state IN ('active','readonly') AND m.domain_uuid=? AND m.state='active'`, - uuid.NewString(), hash, now, now+s.AuthSessionLifetime(domainUUID).Milliseconds(), now, mfaLevel, principalUUID, domainUUID) + uuid.NewString(), hash, now, now+lifetime.Milliseconds(), now, mfaLevel, principalUUID, domainUUID) if err != nil { return "", err } diff --git a/internal/store/auth_cli.go b/internal/store/auth_cli.go index 6696ba1..1942f17 100644 --- a/internal/store/auth_cli.go +++ b/internal/store/auth_cli.go @@ -27,7 +27,9 @@ var cliAuthMigrations = []db.SQLMigration{{Version: 1, Name: "cli_oidc_handoff", schemaUnique("auth_cli_requests", "id"), schemaUnique("auth_cli_requests", "device_hash"), schemaIndex("auth_cli_requests", "auth_cli_domain_created", "domain_uuid,created_ms"), schemaIndex("auth_cli_requests", "auth_cli_expiry", "expires_ms"), -}}} +}}, {Version: 2, Name: "cli_trusted_session_choice", Statements: []string{ + `ALTER TABLE auth_cli_requests ADD COLUMN remember INTEGER NOT NULL DEFAULT 0 CHECK(remember IN (0,1))`, +}, Assertions: []string{schemaColumns("auth_cli_requests", "remember")}}} var ( ErrCLIPending = errors.New("authorization_pending") @@ -39,11 +41,16 @@ var ( type CLIAuthRequest struct { ID, Origin, State, BrowserHash, PrincipalUUID string ExpiresMS int64 + Remember bool } // Admission is one write statement; concurrent unauthenticated requests cannot // race the caps. Requests are retained for ten minutes for issuance accounting. func (s *Store) StartCLIAuth(ctx context.Context, domain, origin, challenge string) (id, device, code string, err error) { + return s.StartCLIAuthForLogin(ctx, domain, origin, challenge, false) +} + +func (s *Store) StartCLIAuthForLogin(ctx context.Context, domain, origin, challenge string, remember bool) (id, device, code string, err error) { if s.ReadOnly { return "", "", "", ErrReadOnly } @@ -66,10 +73,10 @@ func (s *Store) StartCLIAuth(ctx context.Context, domain, origin, challenge stri return } var result sql.Result - result, err = s.DB.ExecContext(ctx, `INSERT INTO auth_cli_requests(id,domain_uuid,origin,device_hash,challenge,code_hash,created_ms,expires_ms) - SELECT ?,?,?,?,?,?,?,? WHERE (SELECT count(*) FROM auth_cli_requests)<4096 + result, err = s.DB.ExecContext(ctx, `INSERT INTO auth_cli_requests(id,domain_uuid,origin,device_hash,challenge,code_hash,created_ms,expires_ms,remember) + SELECT ?,?,?,?,?,?,?,?,? WHERE (SELECT count(*) FROM auth_cli_requests)<4096 AND (SELECT count(*) FROM auth_cli_requests WHERE domain_uuid=? AND created_ms>?)<64`, - id, domain, origin, hashAuthToken(device), challenge, hashAuthToken(code), now, now+300000, domain, now-300000) + id, domain, origin, hashAuthToken(device), challenge, hashAuthToken(code), now, now+300000, remember, domain, now-300000) if err == nil { if n, e := result.RowsAffected(); e != nil { err = e @@ -82,7 +89,7 @@ func (s *Store) StartCLIAuth(ctx context.Context, domain, origin, challenge stri func (s *Store) CLIAuthRequest(ctx context.Context, domain, origin, id string) (*CLIAuthRequest, error) { var grant CLIAuthRequest - err := s.DB.QueryRowContext(ctx, `SELECT id,origin,state,browser_hash,principal_uuid,expires_ms FROM auth_cli_requests WHERE id=? AND domain_uuid=? AND origin=? AND expires_ms>?`, id, domain, origin, time.Now().UnixMilli()).Scan(&grant.ID, &grant.Origin, &grant.State, &grant.BrowserHash, &grant.PrincipalUUID, &grant.ExpiresMS) + err := s.DB.QueryRowContext(ctx, `SELECT id,origin,state,browser_hash,principal_uuid,expires_ms,remember FROM auth_cli_requests WHERE id=? AND domain_uuid=? AND origin=? AND expires_ms>?`, id, domain, origin, time.Now().UnixMilli()).Scan(&grant.ID, &grant.Origin, &grant.State, &grant.BrowserHash, &grant.PrincipalUUID, &grant.ExpiresMS, &grant.Remember) if errors.Is(err, sql.ErrNoRows) { return nil, ErrCLIExpired } @@ -168,15 +175,17 @@ func (s *Store) ExchangeCLIAuth(ctx context.Context, domain, origin, device, ver } now := time.Now().UnixMilli() lifetime := s.AuthSessionLifetime(domain).Milliseconds() + trustedLifetime := s.LoginSessionLifetime(domain, true).Milliseconds() tx, err := s.DB.BeginTx(ctx, nil) if err != nil { return } defer tx.Rollback() var id, state, browser string + var remember bool err = tx.QueryRowContext(ctx, `UPDATE auth_cli_requests SET next_poll_ms=?,state=CASE WHEN state='approved' THEN 'consumed' ELSE state END WHERE device_hash=? AND challenge=? AND domain_uuid=? AND origin=? AND expires_ms>? AND next_poll_ms<=? AND state IN ('pending','approved','denied') - RETURNING id,state,browser_hash`, now+5000, hashAuthToken(device), hashAuthToken(verifier), domain, origin, now, now).Scan(&id, &state, &browser) + RETURNING id,state,browser_hash,remember`, now+5000, hashAuthToken(device), hashAuthToken(verifier), domain, origin, now, now).Scan(&id, &state, &browser, &remember) if errors.Is(err, sql.ErrNoRows) { var exists int e := tx.QueryRowContext(ctx, `SELECT count(*) FROM auth_cli_requests WHERE device_hash=? AND challenge=? AND domain_uuid=? AND origin=? AND expires_ms>? AND state IN ('pending','approved','denied')`, hashAuthToken(device), hashAuthToken(verifier), domain, origin, now).Scan(&exists) @@ -204,6 +213,9 @@ func (s *Store) ExchangeCLIAuth(ctx context.Context, domain, origin, device, ver return } var raw string + if remember { + lifetime = trustedLifetime + } raw, _, err = authToken() if err != nil { return diff --git a/internal/store/auth_cli_test.go b/internal/store/auth_cli_test.go index 369f9bb..c2d0f45 100644 --- a/internal/store/auth_cli_test.go +++ b/internal/store/auth_cli_test.go @@ -8,10 +8,68 @@ import ( "sync/atomic" "testing" "time" + + "pageswiki/internal/db" ) const cliTestOrigin = "https://wiki.example" +func TestCLIAuthTrustedMigrationPreservesExistingRequests(t *testing.T) { + s := newSchemaTestStore(t) + ctx := context.Background() + if err := db.ApplySQLiteMigrations(ctx, s.DB, "cli-auth", cliAuthMigrations[:1]); err != nil { + t.Fatal(err) + } + if _, err := s.DB.Exec(`INSERT INTO auth_cli_requests(id,domain_uuid,origin,device_hash,challenge,code_hash,created_ms,expires_ms) VALUES('old','domain','https://wiki.example','device','challenge','code',1,2)`); err != nil { + t.Fatal(err) + } + for i := 0; i < 2; i++ { + if err := db.ApplySQLiteMigrations(ctx, s.DB, "cli-auth", cliAuthMigrations); err != nil { + t.Fatal(err) + } + } + var remember int + var challenge string + if err := s.DB.QueryRow(`SELECT remember,challenge FROM auth_cli_requests WHERE id='old'`).Scan(&remember, &challenge); err != nil || remember != 0 || challenge != "challenge" { + t.Fatalf("old grant changed: %d %q %v", remember, challenge, err) + } + if _, err := s.DB.Exec(`UPDATE auth_cli_requests SET remember=2`); err == nil { + t.Fatal("invalid remember flag accepted") + } +} + +func TestTOTPLoginBindsTrustedChoice(t *testing.T) { + for _, remember := range []bool{false, true} { + s, p, _ := cliAuthFixture(t) + secret, err := s.BeginTOTPEnrollment(p) + if err != nil { + t.Fatal(err) + } + code, err := totpCode(secret, time.Now().Unix()/30) + if err != nil { + t.Fatal(err) + } + if err = s.ConfirmTOTPEnrollment(p, code); err != nil { + t.Fatal(err) + } + // Synthetic fixture: permit this time step once for login after enrollment. + if _, err = s.DB.Exec(`UPDATE auth_credentials SET parameters_json='{"last_counter":0}' WHERE principal_uuid=? AND kind='totp'`, p); err != nil { + t.Fatal(err) + } + challenge, err := s.CreateTOTPLoginChallengeForLogin("domain", p, remember) + if err != nil { + t.Fatal(err) + } + got, choice, err := s.CompleteTOTPLoginChallengeForLogin("domain", challenge, code) + if err != nil || got != p || choice != remember { + t.Fatalf("bound choice: %q %v %v", got, choice, err) + } + if _, _, err = s.CompleteTOTPLoginChallengeForLogin("domain", challenge, code); err == nil { + t.Fatal("challenge replay accepted") + } + } +} + func cliAuthFixture(t *testing.T) (*Store, string, string) { t.Helper() s := newSchemaTestStore(t) diff --git a/internal/store/auth_session_policy.go b/internal/store/auth_session_policy.go index 0974029..18ae714 100644 --- a/internal/store/auth_session_policy.go +++ b/internal/store/auth_session_policy.go @@ -30,3 +30,37 @@ func (s *Store) SetAuthSessionLifetimeMinutes(domain string, minutes int) error func (s *Store) AuthSessionLifetime(domain string) time.Duration { return time.Duration(s.AuthSessionLifetimeMinutes(domain)) * time.Minute } + +// Opt-in persistent sessions have a separate, administrator-controlled ceiling. +// Zero disables the option. Existing sessions retain their original expiry. +func (s *Store) TrustedSessionDays(domain string) int { + raw, err := s.GetConfigInDomain(domain, "auth_trusted_session_days") + if err != nil { + return 0 + } + if raw == "" { + return 30 + } + days, err := strconv.Atoi(raw) + if err != nil || days < 0 || days > 30 { + return 0 + } + return days +} + +func (s *Store) LoginSessionLifetime(domain string, remember bool) time.Duration { + if days := s.TrustedSessionDays(domain); remember && days > 0 { + return time.Duration(days) * 24 * time.Hour + } + return s.AuthSessionLifetime(domain) +} + +func (s *Store) CreateLoginSession(domain, principal string, mfa int, browser, remember bool) (string, time.Duration, error) { + lifetime := s.LoginSessionLifetime(domain, remember) + prefix := "pw_api_" + if browser { + prefix = "pw_browser_" + } + token, err := s.createTypedAuthSessionForLifetime(domain, principal, mfa, prefix, lifetime) + return token, lifetime, err +} diff --git a/internal/store/auth_totp.go b/internal/store/auth_totp.go index d468223..c0095a4 100644 --- a/internal/store/auth_totp.go +++ b/internal/store/auth_totp.go @@ -176,36 +176,51 @@ func TOTPURI(secret, account, issuer string) string { } func (s *Store) CreateTOTPLoginChallenge(domainUUID, principalUUID string) (string, error) { + return s.CreateTOTPLoginChallengeForLogin(domainUUID, principalUUID, false) +} + +func (s *Store) CreateTOTPLoginChallengeForLogin(domainUUID, principalUUID string, remember bool) (string, error) { token, hash, err := authToken() if err != nil { return "", err } now := time.Now().UnixMilli() - payload, _ := json.Marshal(map[string]string{"principal_uuid": principalUUID}) + payload, _ := json.Marshal(map[string]any{"principal_uuid": principalUUID, "remember": remember}) _, err = s.DB.Exec(`INSERT INTO auth_challenges(uuid,domain_uuid,principal_uuid,kind,token_hash,payload_json,expires_at_ms,created_at_ms) VALUES(?,?,?,?,?,?,?,?)`, uuid.NewString(), domainUUID, principalUUID, "totp-login", hash, string(payload), now+5*60*1000, now) return token, err } func (s *Store) CompleteTOTPLoginChallenge(domainUUID, token, code string) (string, error) { + id, _, err := s.CompleteTOTPLoginChallengeForLogin(domainUUID, token, code) + return id, err +} + +func (s *Store) CompleteTOTPLoginChallengeForLogin(domainUUID, token, code string) (string, bool, error) { sum := sha256.Sum256([]byte(token)) hash := base64.RawURLEncoding.EncodeToString(sum[:]) - var challengeUUID, principalUUID string + var challengeUUID, principalUUID, payload string var expires int64 var attempts int - err := s.DB.QueryRow(`SELECT uuid,principal_uuid,expires_at_ms,attempt_count FROM auth_challenges WHERE domain_uuid=? AND kind='totp-login' AND token_hash=? AND consumed_at_ms=0`, domainUUID, hash).Scan(&challengeUUID, &principalUUID, &expires, &attempts) + err := s.DB.QueryRow(`SELECT uuid,principal_uuid,expires_at_ms,attempt_count,payload_json FROM auth_challenges WHERE domain_uuid=? AND kind='totp-login' AND token_hash=? AND consumed_at_ms=0`, domainUUID, hash).Scan(&challengeUUID, &principalUUID, &expires, &attempts, &payload) if err != nil || expires < time.Now().UnixMilli() || attempts >= 5 { - return "", ErrAccessDenied + return "", false, ErrAccessDenied + } + var options struct { + Remember bool `json:"remember"` + } + if json.Unmarshal([]byte(payload), &options) != nil { + return "", false, ErrAccessDenied } if err = s.VerifyTOTP(principalUUID, code); err != nil { _, _ = s.DB.Exec(`UPDATE auth_challenges SET attempt_count=attempt_count+1,consumed_at_ms=CASE WHEN attempt_count+1>=5 THEN ? ELSE consumed_at_ms END WHERE uuid=? AND consumed_at_ms=0`, time.Now().UnixMilli(), challengeUUID) - return "", err + return "", false, err } result, err := s.DB.Exec(`UPDATE auth_challenges SET consumed_at_ms=? WHERE uuid=? AND consumed_at_ms=0`, time.Now().UnixMilli(), challengeUUID) if err != nil { - return "", err + return "", false, err } changed, _ := result.RowsAffected() if changed != 1 { - return "", ErrAccessDenied + return "", false, ErrAccessDenied } - return principalUUID, nil + return principalUUID, options.Remember, nil } diff --git a/internal/store/document_window.go b/internal/store/document_window.go index f98932a..3cb57b1 100644 --- a/internal/store/document_window.go +++ b/internal/store/document_window.go @@ -81,7 +81,11 @@ func minInt64(a, b int64) int64 { } func (s *Store) GetRevisionMetadata(revisionUUID string) (*Revision, error) { - row := s.DB.QueryRow(`SELECT uuid,object_uuid,COALESCE(document_hash,''),COALESCE(NULLIF(lang,''),'und'),COALESCE(NULLIF(content_type,''),'text/html; charset=utf-8'),COALESCE(filename,''),'',COALESCE(message,''),COALESCE(created_at,0),COALESCE(created_at_ms,COALESCE(created_at,0)*1000) FROM revisions WHERE uuid=?`, revisionUUID) + return s.GetRevisionMetadataContext(context.Background(), revisionUUID) +} + +func (s *Store) GetRevisionMetadataContext(ctx context.Context, revisionUUID string) (*Revision, error) { + row := s.DB.QueryRowContext(ctx, `SELECT uuid,object_uuid,COALESCE(document_hash,''),COALESCE(NULLIF(lang,''),'und'),COALESCE(NULLIF(content_type,''),'text/html; charset=utf-8'),COALESCE(filename,''),'',COALESCE(message,''),COALESCE(created_at,0),COALESCE(created_at_ms,COALESCE(created_at,0)*1000) FROM revisions WHERE uuid=?`, revisionUUID) return scanRevision(row) } diff --git a/internal/store/paths.go b/internal/store/paths.go index 4ad9ab2..dca83bd 100644 --- a/internal/store/paths.go +++ b/internal/store/paths.go @@ -1,6 +1,7 @@ package store import ( + "context" "database/sql" "encoding/json" "strings" @@ -69,6 +70,10 @@ func (s *Store) ResolvePathInDomain( domainUUID string, path string, ) (string, error) { + return s.ResolvePathInDomainContext(context.Background(), domainUUID, path) +} + +func (s *Store) ResolvePathInDomainContext(ctx context.Context, domainUUID, path string) (string, error) { path = normalizePath(path) if err := s.ensureLocalDomain(domainUUID); err != nil { return "", err @@ -76,7 +81,7 @@ func (s *Store) ResolvePathInDomain( var objectUUID string - err := s.DB.QueryRow(` + err := s.DB.QueryRowContext(ctx, ` SELECT object_uuid FROM paths WHERE domain_uuid = ? diff --git a/internal/store/revision_store.go b/internal/store/revision_store.go index 5e82095..2acaec5 100644 --- a/internal/store/revision_store.go +++ b/internal/store/revision_store.go @@ -1,6 +1,7 @@ package store import ( + "context" "crypto/sha256" "database/sql" "encoding/hex" @@ -324,6 +325,10 @@ func (s *Store) GetRevision( } func (s *Store) GetLatestRevisionMetadataForLanguages(objectUUID string, rawLanguages []string) (*Revision, error) { + return s.GetLatestRevisionMetadataForLanguagesContext(context.Background(), objectUUID, rawLanguages) +} + +func (s *Store) GetLatestRevisionMetadataForLanguagesContext(ctx context.Context, objectUUID string, rawLanguages []string) (*Revision, error) { candidates := make([]string, 0, len(rawLanguages)+1) seen := map[string]bool{} for _, rawLanguage := range rawLanguages { @@ -336,7 +341,7 @@ func (s *Store) GetLatestRevisionMetadataForLanguages(objectUUID string, rawLang candidates = append(candidates, "und") } for _, lang := range candidates { - row := s.DB.QueryRow(`SELECT uuid,object_uuid,COALESCE(document_hash,''),COALESCE(NULLIF(lang,''),'und'),COALESCE(NULLIF(content_type,''),'text/html; charset=utf-8'),COALESCE(filename,''),'',COALESCE(message,''),COALESCE(created_at,0),COALESCE(created_at_ms,COALESCE(created_at,0)*1000) FROM revisions WHERE object_uuid=? AND COALESCE(NULLIF(lang,''),'und')=? ORDER BY COALESCE(created_at_ms,created_at*1000) DESC,rowid DESC LIMIT 1`, objectUUID, lang) + row := s.DB.QueryRowContext(ctx, `SELECT uuid,object_uuid,COALESCE(document_hash,''),COALESCE(NULLIF(lang,''),'und'),COALESCE(NULLIF(content_type,''),'text/html; charset=utf-8'),COALESCE(filename,''),'',COALESCE(message,''),COALESCE(created_at,0),COALESCE(created_at_ms,COALESCE(created_at,0)*1000) FROM revisions WHERE object_uuid=? AND COALESCE(NULLIF(lang,''),'und')=? ORDER BY COALESCE(created_at_ms,created_at*1000) DESC,rowid DESC LIMIT 1`, objectUUID, lang) revision, err := scanRevision(row) if err == nil { return revision, nil diff --git a/pages-cli/pages b/pages-cli/pages index 2aa70fb..f502d51 100755 --- a/pages-cli/pages +++ b/pages-cli/pages @@ -144,9 +144,11 @@ class Client: except (ValueError, UnicodeError): raise Failure('Invalid JSON response.') from None - def login(self, username, password, otp=getpass.getpass): + def login(self, username, password, otp=getpass.getpass, *, remember=False): + form={'username':username,'password':password} + if remember:form['remember']='1' try: - result = self.json('/_/auth/login', method='POST', form={'username': username, 'password': password}) + result = self.json('/_/auth/login', method='POST', form=form) except Failure as e: if e.status != 401 or not e.payload.get('mfa_required') or not e.payload.get('challenge_token'): raise @@ -164,12 +166,13 @@ class Client: self.token = token return time.time() + duration - def login_oidc(self, no_browser=False, expected_username='', *, sleep=time.sleep, clock=time.monotonic): + def login_oidc(self, no_browser=False, expected_username='', *, remember=False, sleep=time.sleep, clock=time.monotonic): verifier = secrets.token_urlsafe(32) challenge = base64.urlsafe_b64encode(hashlib.sha256(verifier.encode()).digest()).decode().rstrip('=') try: - started = self.json('/_/auth/cli/start', method='POST', form={ - 'code_challenge': challenge, 'code_challenge_method': 'S256'}) + form={'code_challenge':challenge,'code_challenge_method':'S256'} + if remember:form['remember']='1' + started = self.json('/_/auth/cli/start', method='POST', form=form) except Failure as e: if e.status in (404, 405, 501): raise Failure('CLI OIDC login is unavailable on this server. Check the configured server URL and deploy a server version supporting /_/auth/cli/start.', e.status) from None @@ -856,6 +859,7 @@ def main(argv=None): login = argparse.ArgumentParser(prog='pages login', description='Log into the configured connection. Password and TOTP are prompted privately.') login.add_argument('--oidc', action='store_true', help='Authenticate and approve in a browser; no local password required') login.add_argument('--no-browser', action='store_true', help='Print the browser login URL without opening it (requires --oidc)') + login.add_argument('--remember', action='store_true', help='Keep this trusted device signed in for up to 30 days, subject to server policy') login_args = login.parse_args(args.args) if login_args.no_browser and not login_args.oidc: login.error('--no-browser requires --oidc') elif '--help' in args.args: @@ -874,13 +878,13 @@ def main(argv=None): print('WARNING: --insecure disables TLS certificate and hostname verification for this invocation. Credentials and data can be intercepted; this option is not saved.', file=sys.stderr) if args.command == 'login': if not login_args.oidc and not spec.get('username'): raise Failure('Set your account with pages configure --username NAME, or use pages login --oidc.') - if profile.data.get('token'): raise Failure('Log out this profile before replacing its session.') + if profile.data.get('token') and profile.data.get('expires',0)>time.time(): raise Failure('Log out this profile before replacing its active session.') client = Client(spec['origin'], lang=lang, insecure=args.insecure) if login_args.oidc: - expires, username = client.login_oidc(login_args.no_browser, spec.get('username', '')) + expires, username = client.login_oidc(login_args.no_browser, spec.get('username', ''), remember=login_args.remember) else: username = spec['username'] - expires = client.login(username, getpass.getpass('Password: ')) + expires = client.login(username, getpass.getpass('Password: '), remember=login_args.remember) same_identity = profile.data.get('origin') == client.base and profile.data.get('username') == username pending = profile.data.get('task_outbox', {}) if pending and not same_identity: diff --git a/pages-cli/test_remember.py b/pages-cli/test_remember.py new file mode 100644 index 0000000..48bee90 --- /dev/null +++ b/pages-cli/test_remember.py @@ -0,0 +1,55 @@ +import contextlib +import io +import os +from pathlib import Path +import runpy +import time +import tempfile +import unittest +from unittest.mock import patch + +m = runpy.run_path(str(Path(__file__).with_name('pages'))) + + +class RememberTests(unittest.TestCase): + def test_expired_login_retains_same_account_drafts_and_path(self): + with tempfile.TemporaryDirectory() as root, patch.dict(os.environ, {'XDG_CONFIG_HOME': root}), contextlib.redirect_stdout(io.StringIO()): + with m['Configuration']().locked() as config: + config.data = {'version': 1, 'default_profile': 'default', 'profiles': {'default': {'origin': 'https://wiki.example', 'username': 'anya', 'lang': 'auto'}}} + config.save() + with m['Profile']('default').locked() as profile: + profile.data = {'origin': 'https://wiki.example', 'username': 'anya', 'token': 'expired', 'expires': 1, + 'cwd': '/projects/demo', 'task_outbox': {'request': {'title': 'synthetic draft'}}} + profile.save() + def login(client, username, password, *, remember=False): + self.assertTrue(remember) + client.token = 'pw_api_'+'x'*43 + return time.time()+2592000 + with patch.object(m['Client'], 'login', login), patch.object(m['getpass'], 'getpass', return_value='synthetic'): + m['main'](['login', '--remember']) + with m['Profile']('default').locked() as profile: + self.assertEqual(profile.data['cwd'], '/projects/demo') + self.assertEqual(profile.data['task_outbox']['request']['title'], 'synthetic draft') + self.assertTrue(profile.data['token'].startswith('pw_api_')) + + def test_password_choice_is_explicit_and_bound_before_totp(self): + for remember in (False, True): + client = m['Client']('https://wiki.example') + challenge = m['Failure']('MFA', 401, {'mfa_required': True, 'challenge_token': 'challenge'}) + result = {'access_token': 'pw_api_' + 'x'*43, 'token_type': 'Bearer', 'expires_in': 2592000} + with patch.object(client, 'json', side_effect=[challenge, result]) as api: + expiry = client.login('anya', 'synthetic-password', lambda _: '123456', remember=remember) + self.assertEqual(api.call_args_list[0].kwargs['form'].get('remember'), '1' if remember else None) + self.assertNotIn('remember', api.call_args_list[1].kwargs['form']) + self.assertAlmostEqual(expiry - time.time(), 2592000, delta=2) + + def test_oidc_choice_is_bound_at_start(self): + for remember in (False, True): + client = m['Client']('https://wiki.example') + start = {'device_code': 'd'*43, 'verification_uri': '/_/auth/cli/verify?request='+'r'*43, + 'user_code': 'ABCD-1234', 'expires_in': 300, 'interval': 5} + result = {'access_token': 'pw_api_'+'x'*43, 'token_type': 'Bearer', 'expires_in': 2592000, 'username': 'anya'} + with patch.object(client, 'json', side_effect=[start, result]) as api, contextlib.redirect_stdout(io.StringIO()): + client.login_oidc(True, remember=remember, sleep=lambda _: None, clock=lambda: 0) + self.assertEqual(api.call_args_list[0].kwargs['form'].get('remember'), '1' if remember else None) + self.assertNotIn('remember', api.call_args_list[1].kwargs['form']) diff --git a/static/css/auth.css b/static/css/auth.css index 60331f1..3685020 100644 --- a/static/css/auth.css +++ b/static/css/auth.css @@ -191,3 +191,6 @@ backdrop-filter: none; } } +/* Keep the persistent-session control compact and touchable in all themes. */ +.auth-form label.auth-remember{display:flex;flex-direction:row;align-items:center;gap:.65rem;min-height:44px;font-size:.85rem;cursor:pointer} +.auth-form label.auth-remember input{width:1.15rem;height:1.15rem;min-height:0;flex:0 0 auto;padding:0;margin:0;accent-color:var(--accent)} diff --git a/static/css/browser-upload.css b/static/css/browser-upload.css new file mode 100644 index 0000000..c41322f --- /dev/null +++ b/static/css/browser-upload.css @@ -0,0 +1,13 @@ +.browser-upload-target{outline:2px dashed var(--accent,var(--link));outline-offset:-4px;background:color-mix(in srgb,var(--accent,var(--link)) 8%,var(--bg))} +.browser-upload-queue{position:fixed;right:1rem;bottom:1rem;z-index:950;width:min(27rem,calc(100vw - 2rem));box-sizing:border-box;background:var(--bg);color:var(--fg);border:1px solid var(--border,var(--border-soft));border-radius:.6rem;box-shadow:0 .5rem 2rem #0005;padding:.5rem} +.browser-upload-queue[hidden],.browser-upload-queue [hidden]{display:none} +.browser-upload-queue header,.browser-upload-queue footer{display:flex;align-items:center;gap:.5rem} +.browser-upload-queue header strong{flex:1}.browser-upload-queue footer{justify-content:flex-end} +.browser-upload-queue button{font:inherit;color:inherit;background:transparent;border:1px solid var(--border-soft);border-radius:.3rem;min-width:2rem;min-height:2rem;cursor:pointer} +.browser-upload-queue button:disabled{opacity:.4;cursor:default} +.browser-upload-queue ol{list-style:none;margin:.4rem 0;padding:0;max-height:40dvh;overflow:auto;overscroll-behavior:contain} +.browser-upload-queue li{display:grid;grid-template-columns:minmax(0,1fr) auto;gap:.2rem .5rem;padding:.4rem 0;border-top:1px solid var(--border-soft)} +.browser-upload-queue .upload-name{white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.browser-upload-queue .upload-state{font-size:.8rem;grid-column:1/-1;overflow-wrap:anywhere} +.browser-upload-queue progress{width:100%;height:.35rem;grid-column:1/-1;accent-color:var(--accent,var(--link))}.browser-upload-queue [data-state=failed] .upload-state{color:var(--danger,#e77373)} +.browser-upload-queue .upload-notice{margin:.3rem 0;font-size:.85rem}.browser-upload-queue .upload-notice:empty{display:none} +@media(max-width:600px){.browser-upload-queue{right:.5rem;bottom:.5rem;width:calc(100vw - 1rem)}.browser-upload-queue button{min-height:44px;min-width:44px}.browser-upload-queue ol{max-height:35dvh}} diff --git a/static/css/feed-gecko.css b/static/css/feed-gecko.css index 546c02b..c1e3007 100644 --- a/static/css/feed-gecko.css +++ b/static/css/feed-gecko.css @@ -1 +1 @@ -:root{color-scheme:light dark}html,body{margin:0;width:100%;height:100%;overflow:hidden;background:#fff;color:#222;font:14px system-ui}canvas{display:block;width:100%;height:100%;outline:none}#status{position:absolute;inset:1rem auto auto 1rem;max-width:calc(100% - 4rem);margin:0;padding:.75rem 1rem;border-radius:.4rem;background:#222;color:#fff;z-index:1}#status[hidden]{display:none} +:root{color-scheme:light dark}html,body{margin:0;width:100%;height:100%;overflow:hidden;background:#fff;color:#222;font:14px system-ui}canvas{display:block;width:100%;height:100%;outline:none}#status{position:absolute;inset:1rem auto auto 1rem;max-width:calc(100% - 4rem);margin:0;padding:.75rem 1rem;border-radius:.4rem;background:#222;color:#fff;z-index:1}#status[hidden],canvas[hidden]{display:none} diff --git a/static/css/feeds.css b/static/css/feeds.css index f64cf54..d461d74 100644 --- a/static/css/feeds.css +++ b/static/css/feeds.css @@ -1,5 +1,8 @@ .feeds-page{height:100dvh;overflow:hidden;display:flex;flex-direction:column;background:var(--bg);color:var(--fg)} -.feed-external{width:calc(100vw - 1.5rem);height:calc(100dvh - 1.5rem);max-width:none;max-height:none;box-sizing:border-box;margin:auto;padding:0;background:var(--bg,#111);color:var(--fg);border:1px solid var(--border,#8885);border-radius:.5rem;overflow:hidden}.feed-external[open]{display:flex;flex-direction:column}.feed-external::backdrop{background:#0009}.feed-external-bar{display:flex;align-items:center;gap:.5rem;padding:.3rem .5rem;flex-shrink:0}.feed-external-bar span{flex:1;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.feed-external-bar a{padding:.3rem;text-decoration:none;font-size:1.2rem}.feed-external iframe{flex:1;width:100%;min-height:0;border:0;background:#fff}@media(max-width:600px){.feed-external{width:100vw;height:100dvh;border:0;border-radius:0}} +.feed-external{width:calc(100vw - 1.5rem);height:calc(100dvh - 1.5rem);max-width:none;max-height:none;box-sizing:border-box;margin:auto;padding:0;background:var(--bg,#111);color:var(--fg);border:1px solid var(--border,#8885);border-radius:.5rem;overflow:hidden}.feed-external[open]{display:flex;flex-direction:column}.feed-external::backdrop{background:#0009}.feed-external-bar{display:flex;align-items:center;gap:.5rem;padding:.3rem .5rem;flex-shrink:0}.feed-external-bar span{flex:1;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.feed-external-bar a{padding:.3rem;text-decoration:none;font-size:1.2rem}.feed-external iframe{flex:1;width:100%;min-height:0;border:0;background:#fff}.feed-gecko-find{display:flex;align-items:center;gap:.3rem;padding:.3rem .5rem;flex-shrink:0}.feed-gecko-find[hidden]{display:none}.feed-gecko-find input{flex:1;min-width:0;max-width:30rem}.feed-gecko-find output{font-size:.85rem}.feed-gecko-find button{min-width:2rem}@media(max-width:600px){.feed-external{width:100vw;height:100dvh;border:0;border-radius:0}.feed-external-bar{gap:.15rem}.feed-external-bar button,.feed-gecko-find button{min-height:44px}} +#feeds-app form.feed-gecko-find{display:flex;gap:.3rem} +#feeds-app form.feed-gecko-find[hidden]{display:none} +#feed-columns>span{white-space:nowrap} .feed-help{display:flex;align-items:center;gap:.5rem;padding:.25rem 0;font-size:.78rem;flex-shrink:0}.feed-help[hidden]{display:none}.feed-help span{flex:1;min-width:0;overflow-wrap:anywhere}.feed-help button{flex-shrink:0} .feed-capture{width:min(42rem,calc(100vw - 2rem));max-height:calc(100dvh - 2rem);box-sizing:border-box;overflow:auto;background:var(--bg,#111);color:var(--fg);border:1px solid var(--border,#8885);border-radius:.6rem;padding:1rem;box-shadow:0 1rem 4rem #0007}.feed-capture::backdrop{background:#0008}.feed-capture-heading{display:flex;align-items:center;justify-content:space-between;gap:.5rem}.feed-capture h2{font-size:1.05rem;margin:0}.feed-capture-heading,.feed-capture-field,.feed-capture-tokens,.feed-capture-destination,.feed-capture-actions,.feed-capture .feed-warning{grid-column:1/-1}#feeds-app .feed-capture form{grid-template-columns:1fr 1fr;gap:.65rem}#feeds-app .feed-capture .feed-capture-check{display:flex;align-items:center;gap:.4rem}#feeds-app .feed-capture input[type=checkbox]{width:1rem;min-height:1rem;flex-shrink:0}.feed-capture-check:has([name=confirm]){grid-column:1/-1}.feed-capture-tokens,.feed-capture-actions{display:flex;gap:.35rem;flex-wrap:wrap}.feed-capture-actions{justify-content:flex-end}.feed-capture-destination{font-size:.75rem;overflow-wrap:anywhere;opacity:.8}.feed-capture .feed-warning:empty{display:none}.feed-capture-field:has([name=prefix]){position:relative}.feed-capture-field:has([name=prefix]) a{position:absolute;right:.1rem;top:0;text-decoration:none}@media(max-width:600px){#feeds-app .feed-capture form{grid-template-columns:1fr}.feed-capture{padding:.75rem}} .feed-folder-row{display:flex;align-items:center;min-width:0}#feeds-app .feed-folder-toggle{flex:0 0 1.7rem;border:0;padding:.15rem;min-width:1.7rem}#feeds-app .feed-folder-name{display:flex;align-items:center;gap:.3rem;flex:1;min-width:0;text-align:left;border:0;font-size:.78rem;padding:.2rem}.feed-folder-name span{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.feed-folder-name svg{width:1rem;height:1rem;flex-shrink:0;fill:none;stroke:currentColor;stroke-width:1.6;stroke-linejoin:round}.feed-folder>.feed-subscriptions{padding-left:1.1rem;border-left:1px solid var(--border,#8885);margin-left:.8rem}.feed-catalog .feed-subscriptions[hidden]{display:none}.feed-folder small{font-size:.7rem;opacity:.7;padding:.3rem}.feed-catalog button[aria-current=true]{background:color-mix(in srgb,var(--accent,#659de4) 22%,transparent)!important}.feed-tree-all{width:100%;text-align:left}.feed-folders{min-width:0} @@ -11,6 +14,6 @@ #feed-status:empty{display:none}#feed-reader .feed-body-html{white-space:normal}.feed-original{display:inline-flex;align-items:center;justify-content:center;min-width:2rem;min-height:2rem;border-radius:.2rem}.feed-original:hover{background:var(--border,#8883)}.feed-original svg{width:1.1rem;height:1.1rem;fill:none;stroke:currentColor;stroke-width:1.7;stroke-linecap:round;stroke-linejoin:round} .feeds-page .topbar,.feeds-page .organizer-nav{flex-shrink:0}.feeds-page #feeds-app{flex:1;min-height:0;display:flex;flex-direction:column;width:100%;max-width:none;margin:0;padding:0 .5rem .25rem;box-sizing:border-box} #feed-toolbar{display:flex;align-items:center;gap:.35rem;min-height:2.5rem;flex-wrap:wrap}#feed-toolbar input{flex:1;min-width:8rem;max-width:32rem}#feeds-app button,#feeds-app input,#feeds-app select{font:inherit;color:inherit;background:var(--panel,var(--bg));border:1px solid var(--border,#6666);border-radius:.2rem;padding:.2rem .45rem;min-height:1.8rem}#feeds-app button{cursor:pointer}#feeds-app button:disabled{opacity:.45;cursor:default}#feeds-app a{color:var(--accent,#659de4)}#feed-status{margin:0;min-height:1.25rem;font-size:.78rem;color:var(--muted,var(--fg));overflow-wrap:anywhere} -.feed-layout{flex:1;min-height:0;display:grid;grid-template-columns:12rem minmax(18rem,1fr) minmax(18rem,42%);border:1px solid var(--border,#6666)}#feed-sources,.feed-index,#feed-reader{min-width:0;min-height:0;overflow:auto;overscroll-behavior:contain}#feed-sources{border-right:1px solid var(--border,#6666);padding:.3rem}.feed-source{display:flex;align-items:center}.feed-source>button:first-child{flex:1;text-align:left;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;border:0!important}.feed-source small{font-size:.68rem}.feed-source[aria-current=true]{background:color-mix(in srgb,var(--accent,#659de4) 20%,transparent)}#feed-sources h3{font-size:.75rem;margin:.5rem 0 .15rem}.feed-index{display:flex;flex-direction:column}#feed-rows{flex:1;min-height:0;overflow:auto;outline-offset:-2px}#feed-columns,.feed-row{display:grid;grid-template-columns:1.8rem 5.4rem 8rem minmax(0,1fr);align-items:center;gap:.3rem;font-size:.8rem;padding:.25rem .4rem;box-sizing:border-box}#feed-columns{font-size:.7rem;border-bottom:1px solid var(--border,#6666);opacity:.7}.feed-row{cursor:pointer;min-height:1.85rem;content-visibility:auto;contain-intrinsic-size:auto 30px}.feed-row>span{white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.feed-row.unread{font-weight:650}.feed-row[aria-selected=true]{background:color-mix(in srgb,var(--accent,#659de4) 24%,var(--bg,#16191f));box-shadow:inset 3px 0 var(--accent,#659de4)}.feed-row:hover{background:color-mix(in srgb,var(--accent,#659de4) 12%,transparent)}#feed-reader{border-left:1px solid var(--border,#6666);padding:.75rem}#feed-reader h1{font-size:1.1rem;margin:0 0 .4rem;overflow-wrap:anywhere}#feed-reader .feed-body{white-space:pre-wrap;overflow-wrap:anywhere;font-family:inherit;font-size:.9rem;line-height:1.6}.feed-actions{display:flex;flex-wrap:wrap;gap:.3rem;align-items:center}.feed-meta{font-size:.75rem;opacity:.7;margin:.4rem 0}#feed-keys{font-size:.72rem;padding:.3rem;opacity:.75}#feeds-app form{display:grid;gap:.5rem}#feeds-app form label{display:grid;gap:.2rem;font-size:.85rem}#feeds-app form input,#feeds-app form select{width:100%;box-sizing:border-box}#feeds-app form .feed-actions{display:flex}#feeds-app .feed-warning{font-size:.8rem;line-height:1.5;opacity:.8}.feed-source-error{color:var(--danger,#e57a7a)} -@media(max-width:1100px){.feed-layout{grid-template-columns:10rem minmax(0,1fr)}#feed-reader{display:none}.feed-layout.reading .feed-index{display:none}.feed-layout.reading #feed-reader{display:block}#feed-columns,.feed-row{grid-template-columns:1.5rem 4.7rem 7rem minmax(0,1fr)}} -@media(max-width:600px){.feed-layout{display:flex;flex-direction:column}#feed-sources{display:none;max-height:35%;flex-shrink:0;border-bottom:1px solid var(--border,#6666)}.feed-layout.sources-open #feed-sources{display:block}.feed-index,#feed-reader{flex:1;width:100%;box-sizing:border-box}#feed-columns,.feed-row{grid-template-columns:1.5rem 3.5rem minmax(0,1fr);font-size:.85rem}.feed-row{min-height:2.75rem}#feed-columns>span:nth-child(3),.feed-row>span:nth-child(3){display:none}#feeds-app button{min-height:2.3rem}#feed-keys{display:none}#feed-reader{border-left:0}.feeds-page .organizer-nav{font-size:.8rem}} +.feed-layout{flex:1;min-height:0;display:grid;grid-template-columns:12rem minmax(18rem,1fr) minmax(18rem,42%);border:1px solid var(--border,#6666)}#feed-sources,.feed-index,#feed-reader{min-width:0;min-height:0;overflow:auto;overscroll-behavior:contain}#feed-sources{border-right:1px solid var(--border,#6666);padding:.3rem}.feed-source{display:flex;align-items:center}.feed-source>button:first-child{flex:1;text-align:left;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;border:0!important}.feed-source small{font-size:.68rem}.feed-source[aria-current=true]{background:color-mix(in srgb,var(--accent,#659de4) 20%,transparent)}#feed-sources h3{font-size:.75rem;margin:.5rem 0 .15rem}.feed-index{display:flex;flex-direction:column}#feed-rows{flex:1;min-height:0;overflow:auto;outline-offset:-2px}#feed-columns,.feed-row{display:grid;grid-template-columns:1.8rem 3.3rem 3.4rem 7rem minmax(0,1fr);align-items:center;gap:.3rem;font-size:.8rem;padding:.25rem .4rem;box-sizing:border-box}#feed-columns{font-size:.7rem;border-bottom:1px solid var(--border,#6666);opacity:.7}.feed-row{cursor:pointer;min-height:1.85rem;content-visibility:auto;contain-intrinsic-size:auto 30px}.feed-row>span{white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.feed-row.unread{font-weight:650}.feed-row[aria-selected=true]{background:color-mix(in srgb,var(--accent,#659de4) 24%,var(--bg,#16191f));box-shadow:inset 3px 0 var(--accent,#659de4)}.feed-row:hover{background:color-mix(in srgb,var(--accent,#659de4) 12%,transparent)}#feed-reader{border-left:1px solid var(--border,#6666);padding:.75rem}#feed-reader h1{font-size:1.1rem;margin:0 0 .4rem;overflow-wrap:anywhere}#feed-reader .feed-body{white-space:pre-wrap;overflow-wrap:anywhere;font-family:inherit;font-size:.9rem;line-height:1.6}.feed-actions{display:flex;flex-wrap:wrap;gap:.3rem;align-items:center}.feed-meta{font-size:.75rem;opacity:.7;margin:.4rem 0}#feed-keys{font-size:.72rem;padding:.3rem;opacity:.75}#feeds-app form{display:grid;gap:.5rem}#feeds-app form label{display:grid;gap:.2rem;font-size:.85rem}#feeds-app form input,#feeds-app form select{width:100%;box-sizing:border-box}#feeds-app form .feed-actions{display:flex}#feeds-app .feed-warning{font-size:.8rem;line-height:1.5;opacity:.8}.feed-source-error{color:var(--danger,#e57a7a)} +@media(max-width:1100px){.feed-layout{grid-template-columns:10rem minmax(0,1fr)}#feed-reader{display:none}.feed-layout.reading .feed-index{display:none}.feed-layout.reading #feed-reader{display:block}#feed-columns,.feed-row{grid-template-columns:1.5rem 3.3rem 3.4rem 6rem minmax(0,1fr)}} +@media(max-width:600px){.feed-layout{display:flex;flex-direction:column}#feed-sources{display:none;max-height:35%;flex-shrink:0;border-bottom:1px solid var(--border,#6666)}.feed-layout.sources-open #feed-sources{display:block}.feed-index,#feed-reader{flex:1;width:100%;box-sizing:border-box}#feed-columns,.feed-row{grid-template-columns:1.2rem 3.1rem 3.2rem minmax(0,1fr);font-size:.85rem}.feed-row{min-height:2.75rem}#feed-columns>span:nth-child(4),.feed-row>span:nth-child(4){display:none}#feeds-app button{min-height:2.3rem}#feed-keys{display:none}#feed-reader{border-left:0}.feeds-page .organizer-nav{font-size:.8rem}} diff --git a/static/css/main.css b/static/css/main.css index b0a5755..da3fb58 100644 --- a/static/css/main.css +++ b/static/css/main.css @@ -5,6 +5,12 @@ body { margin: 0; } +.csv-projection { max-width:100%; overflow:auto; } +.csv-projection table { border-collapse:collapse; font-size:.9rem; } +.csv-projection th,.csv-projection td { padding:.25rem .5rem; border:1px solid var(--border-soft); white-space:pre-wrap; overflow-wrap:anywhere; min-width:3rem; max-width:30rem; } +.csv-projection th { background:var(--row-hover,var(--bg)); text-align:left; } +.csv-projection caption { text-align:left; font-size:.8rem; padding:.2rem 0; } + a { color: var(--link); } diff --git a/static/css/tasks.css b/static/css/tasks.css index bfdb09d..57b032c 100644 --- a/static/css/tasks.css +++ b/static/css/tasks.css @@ -20,6 +20,9 @@ body:has(#tasks-app)>.topbar{min-width:0}#tasks-app{box-sizing:border-box} #tasks-capture{display:flex;gap:.5rem;margin:1rem 0}#tasks-quick-title{flex:1}.tasks-search{display:flex;align-items:center;gap:.6rem;margin:1rem 0}.tasks-search input{flex:1}#tasks-status:empty{display:none} #tasks-list{border-top:1px solid var(--border,#8885)}.task-row{display:flex;align-items:center;gap:.8rem;padding:.6rem .2rem;border-bottom:1px solid var(--border,#8885);content-visibility:auto;contain-intrinsic-size:auto 62px}.task-row-main{min-width:0;flex:1;display:flex;flex-direction:column;gap:.2rem}.task-row-main>a{color:var(--fg);text-decoration:none;overflow-wrap:anywhere}.task-row-main>a:hover{color:var(--accent)}.task-row[data-done=true] .task-row-main>a{text-decoration:line-through;opacity:.65}.task-row-meta{display:flex;gap:.7rem;flex-wrap:wrap}.task-row-meta .task-overdue{color:var(--danger,#da6161);opacity:1}#tasks-more{margin:1rem 0;width:100%} #tasks-app #tasks-delete{color:var(--danger,#da6161);border-color:currentColor} +#tasks-app a.task-footer-link{display:inline-flex;align-items:center;justify-content:center;box-sizing:border-box;font:inherit;line-height:normal;color:var(--fg);background:var(--panel,var(--bg));border:1px solid var(--border,#8885);border-radius:.45rem;padding:.5rem .7rem;text-decoration:none} +#tasks-app a.task-footer-link:hover{border-color:var(--accent);color:var(--accent)} +@media(max-width:760px){#tasks-form>footer>button,#tasks-form>footer>a.task-footer-link{min-height:44px}} #tasks-dialog{box-sizing:border-box;color:var(--fg);background:var(--panel,var(--bg));border:1px solid var(--border,#8885);border-radius:1rem;width:min(680px,95vw);max-height:92dvh;padding:1.5rem;box-shadow:0 1rem 4rem #0005;overflow:auto}#tasks-dialog::backdrop{background:#0007;backdrop-filter:blur(3px)}#tasks-fields{display:grid;grid-template-columns:1fr 1fr;gap:.8rem;margin:1rem 0}#tasks-fields label{display:flex;flex-direction:column;gap:.3rem;font-size:.9rem}#tasks-fields label.task-full{grid-column:1/-1}#tasks-fields input,#tasks-fields select,#tasks-fields textarea{width:100%}#tasks-fields textarea{min-height:7rem;resize:vertical}#tasks-fields a{overflow-wrap:anywhere;color:var(--accent)}#tasks-form footer{justify-content:flex-end;margin-top:1rem}#tasks-error{color:var(--danger,#d55)}#tasks-history{font-size:.8rem}#tasks-app [hidden]{display:none!important} @media(max-width:760px){#tasks-app{display:block;padding:.75rem}#tasks-nav{flex-direction:row;overflow:auto;padding:0 0 .7rem;gap:.1rem;white-space:nowrap}#tasks-nav a{padding:.5rem}.tasks-toolbar h1{font-size:1.3rem}#tasks-dialog{width:100%;height:100dvh;max-height:100dvh;margin:0;border-radius:0;padding:1rem}#tasks-fields{gap:.7rem}.task-row{gap:.5rem}} @media(max-width:760px){body:has(#tasks-app)>.topbar{flex-wrap:wrap;padding:.65rem;gap:.5rem}body:has(#tasks-app)>.topbar>.brand{flex:1;min-width:0}body:has(#tasks-app)>.topbar>.brand>a{display:block;overflow:hidden;text-overflow:ellipsis}body:has(#tasks-app)>.topbar>nav{order:2;width:100%}body:has(#tasks-app)>.topbar>.city-map-launch{margin-inline:0;flex-shrink:0}#tasks-fields{grid-template-columns:minmax(0,1fr) minmax(0,1fr)}} diff --git a/static/js/auth-remember.js b/static/js/auth-remember.js new file mode 100644 index 0000000..9b1bd52 --- /dev/null +++ b/static/js/auth-remember.js @@ -0,0 +1,24 @@ +(() => { + 'use strict'; + const choice=document.getElementById('auth-remember'); + if(!choice)return; + const sync=()=>{ + for(const form of document.querySelectorAll('.auth-form')){ + if(form===choice.form)continue; + const path=new URL(form.action,location.href).pathname; + if(!['/_/auth/ldap','/_/auth/magic-link'].includes(path))continue; + let input=form.querySelector('input[name=remember]'); + if(!input){input=document.createElement('input');input.type='hidden';input.name='remember';form.append(input);} + input.value=choice.checked?'1':'0'; + } + for(const link of document.querySelectorAll('a.auth-provider-button')){ + const url=new URL(link.href,location.href); + if(url.origin!==location.origin||url.pathname!=='/_/auth/oidc')continue; + if(choice.checked)url.searchParams.set('remember','1');else url.searchParams.delete('remember'); + link.href=url.pathname+url.search; + } + }; + choice.addEventListener('change',sync); + window.addEventListener('pageshow',sync); + sync(); +})(); diff --git a/static/js/browser-thumbnails.js b/static/js/browser-thumbnails.js index 291ed68..0b296a0 100644 --- a/static/js/browser-thumbnails.js +++ b/static/js/browser-thumbnails.js @@ -56,15 +56,17 @@ drain(); }, {rootMargin: '240px 0px'}); - document.querySelectorAll('.browser-thumbnail [data-preview-src]').forEach(image => { + const observeImages = () => { document.querySelectorAll('.browser-thumbnail [data-preview-src]').forEach(image => { + if(image.dataset.previewQueued || image.dataset.previewLoaded)return; observer.observe(image); const rect = image.getBoundingClientRect(); if (rect.bottom >= -240 && rect.top <= innerHeight + 240) { observer.unobserve(image); enqueue(image); } - }); - drain(); + });drain(); }; + observeImages(); + document.addEventListener('pageswiki:browser-list-updated',observeImages); document.addEventListener('visibilitychange', drain); window.addEventListener('pagehide', () => { stopped = true; diff --git a/static/js/browser-upload.js b/static/js/browser-upload.js new file mode 100644 index 0000000..985e9f3 --- /dev/null +++ b/static/js/browser-upload.js @@ -0,0 +1,82 @@ +(() => { + 'use strict'; + const main=document.querySelector('[data-upload-directory]');if(!main)return; + const ja=document.documentElement.lang.startsWith('ja'),t=(en,jp)=>ja?jp:en; + const maxFiles=100,maxBytes=256*1024*1024,queue=[]; + let running=false,dragDepth=0,dirty=false,refreshGeneration=0; + const el=(tag,text,cls)=>{const n=document.createElement(tag);if(text!==undefined)n.textContent=text;if(cls)n.className=cls;return n;}; + const button=(text,label,action)=>{const b=el('button',text);b.type='button';b.title=b.ariaLabel=label;b.onclick=action;return b;}; + const card=el('aside',undefined,'browser-upload-queue');card.hidden=true;card.ariaLabel=t('Uploads','アップロード'); + const header=el('header'),title=el('strong',t('Uploads','アップロード')),summary=el('span'),list=el('ol'),notice=el('p',undefined,'upload-notice'),footer=el('footer'); + notice.role='status';summary.setAttribute('aria-live','polite');list.id='browser-upload-items'; + const toggle=button('−',t('Collapse queue','キューを折りたたむ'),()=>{list.hidden=!list.hidden;toggle.textContent=list.hidden?'+':'−';toggle.setAttribute('aria-expanded',String(!list.hidden));});toggle.setAttribute('aria-expanded','true');toggle.setAttribute('aria-controls',list.id); + const clear=button('✓',t('Clear finished entries','完了・失敗を一覧から除く'),()=>{for(let i=queue.length-1;i>=0;i--)if(queue[i].state!=='queued'&&queue[i].state!=='uploading'){queue[i].row.remove();queue.splice(i,1);}update();}); + const refresh=button('⟳',t('Refresh file list','ファイル一覧を更新'),()=>{if(!running)refreshList();}); + const close=button('×',t('Close queue','キューを閉じる'),()=>{if(!running)card.hidden=true;}); + header.append(title,summary,toggle,close);footer.append(clear,refresh);card.append(header,notice,list,footer);document.body.append(card); + const labels={queued:t('Queued','待機'),uploading:t('Uploading','送信中'),done:t('Complete','完了'),failed:t('Failed','失敗'),cancelled:t('Cancelled','取消')}; + function update(){ + const completed=queue.filter(x=>x.state==='done').length,pending=queue.filter(x=>x.state==='queued'||x.state==='uploading').length; + summary.textContent=`${completed} / ${queue.length}`;summary.title=t('Completed / total','完了 / 全件'); + close.disabled=refresh.disabled=running||pending>0;refresh.hidden=!dirty; + clear.disabled=!queue.some(x=>!['queued','uploading'].includes(x.state)); + } + function state(item,value,message){item.state=value;item.row.dataset.state=value;item.status.textContent=message||labels[value];item.cancel.disabled=value!=='queued';if(value==='done')item.progress.value=100;update();} + function send(item){return new Promise(resolve=>{ + const xhr=new XMLHttpRequest();let settled=false; + const finish=(ok,message)=>{if(settled)return;settled=true;item.file=null;dirty=true;state(item,ok?'done':'failed',message);resolve();}; + xhr.open('POST','/v/upload');xhr.timeout=10*60*1000;xhr.setRequestHeader('Accept','application/json');xhr.setRequestHeader('If-None-Match','*'); + xhr.upload.onprogress=e=>{if(e.lengthComputable){item.progress.value=Math.min(99,e.loaded/e.total*100);item.progress.setAttribute('aria-valuetext',`${Math.round(item.progress.value)}%`);}}; + xhr.onload=()=>{let result;try{result=JSON.parse(xhr.responseText);}catch{} + if(xhr.status===201&&result?.path)finish(true); + else finish(false,xhr.status===412?t('Already exists — not overwritten','同名あり・上書きしません'):xhr.status===401||xhr.status===403||xhr.responseURL.includes('/_/auth/')?t('Sign in / permission required','ログインまたは権限が必要'):t('Upload failed; check file list','送信失敗・一覧を確認してください')); + }; + xhr.onerror=xhr.ontimeout=()=>finish(false,t('Connection lost; check result before retrying','通信断・再送前に保存結果を確認してください')); + const form=new FormData();form.append('path',item.path);form.append('file',item.file,item.name); + try{xhr.send(form);}catch{finish(false,t('Could not send file','ファイルを送信できません'));} + });} + async function refreshList(){ + const generation=++refreshGeneration; + refresh.disabled=true; + try{ + const response=await fetch(location.href,{credentials:'same-origin',cache:'no-store'}); + if(!response.ok||response.redirected)throw Error('refresh'); + const page=new DOMParser().parseFromString(await response.text(),'text/html'),grid=page.getElementById('browser-grid'),current=document.getElementById('browser-grid'); + if(generation!==refreshGeneration)return; + if(!grid||page.querySelector('[data-upload-directory]')?.dataset.uploadDirectory!==main.dataset.uploadDirectory)throw Error('refresh'); + window.clearBrowserSelection?.();current.replaceChildren(...Array.from(grid.children).map(row=>document.importNode(row,true))); + main.querySelector('.browser-empty')?.remove();const empty=page.querySelector('.browser-empty');if(empty)current.before(document.importNode(empty,true)); + document.dispatchEvent(new Event('pageswiki:browser-list-updated')); + }catch{if(generation===refreshGeneration)notice.textContent=t('Could not refresh list. Reload after checking the queue.','一覧を更新できません。キューを確認してから再読み込みしてください。');} + finally{update();} + } + async function pump(){if(running)return;running=true;update();try{let item;while((item=queue.find(x=>x.state==='queued'))){state(item,'uploading');await send(item);}}finally{running=false;update();if(dirty)refreshList();}} + function add(files,directory){ + ++refreshGeneration; + card.hidden=false;notice.textContent=''; + for(const file of files){ + if(queue.length>=maxFiles){notice.textContent=t('Queue limit: 100 files. Clear finished entries.','キューは100件までです。完了分を一覧から除いてください。');break;} + const name=file.name,target=directory.replace(/\/+$/,'')+'/'+name; + if(!name||name==='.'||name==='..'||/[\\/\x00-\x1f]/.test(name)){notice.textContent=t('Invalid file name','無効なファイル名です');continue;} + const row=el('li'),label=el('span',name,'upload-name'),status=el('span',labels.queued,'upload-state'),progress=el('progress');row.dataset.state='queued';progress.max=100;progress.value=0;progress.ariaLabel=name;label.title=target; + const item={file,name,path:target,state:'queued',row,status,progress};item.cancel=button('×',t('Cancel queued file','待機中のファイルを取り消す'),()=>{if(item.state==='queued'){item.file=null;state(item,'cancelled');}}); + row.append(label,item.cancel,status,progress);list.append(row);queue.push(item); + if(file.size>maxBytes){item.file=null;state(item,'failed',t('Maximum 256 MiB per file','1ファイル256 MiBまで'));} + else if(queue.some(x=>x!==item&&x.path===target&&['queued','uploading'].includes(x.state))){item.file=null;state(item,'failed',t('Already queued','すでにキューにあります'));} + } + update();pump(); + } + const external=e=>Array.from(e.dataTransfer?.types||[]).includes('Files'); + document.addEventListener('dragenter',e=>{if(!external(e))return;e.preventDefault();if(++dragDepth===1)main.classList.add('browser-upload-target');},true); + document.addEventListener('dragover',e=>{if(!external(e))return;e.preventDefault();e.stopImmediatePropagation();e.dataTransfer.dropEffect=e.target.closest?.('.browser-main')?'copy':'none';},true); + document.addEventListener('dragleave',e=>{if(!external(e))return;if(--dragDepth<=0){dragDepth=0;main.classList.remove('browser-upload-target');}},true); + document.addEventListener('drop',e=>{ + if(!external(e))return;e.preventDefault();e.stopImmediatePropagation();dragDepth=0;main.classList.remove('browser-upload-target'); + if(!e.target.closest?.('.browser-main'))return; + // Folder traversal is deliberately not guessed from zero-byte File objects. + const items=Array.from(e.dataTransfer.items||[]);if(items.some(x=>x.webkitGetAsEntry?.()?.isDirectory)){card.hidden=false;notice.textContent=t('Drop files, not folders','フォルダではなくファイルをドロップしてください');return;} + const folder=e.target.closest?.('.browser-row[data-is-dir="true"]'); + add(Array.from(e.dataTransfer.files||[]),folder?.dataset.path||main.dataset.uploadDirectory||'/'); + },true); + addEventListener('beforeunload',e=>{if(running||queue.some(x=>x.state==='queued')){e.preventDefault();e.returnValue='';}}); +})(); diff --git a/static/js/feed-gecko.js b/static/js/feed-gecko.js index 8902a39..1ef06ba 100644 --- a/static/js/feed-gecko.js +++ b/static/js/feed-gecko.js @@ -3,12 +3,23 @@ const api = '/_/api/v1/extension/organizer.feeds/browser/'; const status = document.getElementById('status'), canvas = document.getElementById('screen'); let engine, started = false, ready = false, stopped = false, navigating = false, poll, resizeTimer; let history = [], position = 0; +const startup = new AbortController(); +let deadline, finding=false; const ja = parent.document.documentElement.lang.startsWith('ja'); const text = (en,jp) => ja ? jp : en; function report(message) { status.textContent=message; status.hidden=!message; } function url(value) { try { const u=new URL(value); return ['https:','http:'].includes(u.protocol)&&!u.username&&!u.password&&u.href.length<=8192 ? u.href : ''; } catch {return '';} } function send(data) { parent.postMessage({type:'pageswiki-gecko',...data},location.origin); } function navigationState() { send({href:history[position],back:position>0,forward:position256)return; + finding=true; + try { + const found=await engine.evalChrome(`String(window.find(${JSON.stringify(query)},false,${backwards===true},true,false,false,false))`); + if(!stopped)send({findResult:found==='true',query}); + } catch {if(!stopped)send({findError:true});} + finally {finding=false;} +} async function start(href) { started=true; - if(!crossOriginIsolated || typeof SharedArrayBuffer==='undefined') { report(text('This browser cannot run the isolated viewer. Use ↗ to open the original.','このブラウザでは分離ビューアを実行できません。↗から原文を開いてください。'));return; } + if(!crossOriginIsolated || typeof SharedArrayBuffer==='undefined') {fail();return;} report(text('Starting Gecko…','Geckoを起動中…')); const timer=setTimeout(()=>{if(!ready&&!stopped)report(text('Startup is taking longer than expected. Close the viewer to stop, or use ↗.','起動に時間がかかっています。閉じると停止します。↗から原文も開けます。'));},45000); + deadline=setTimeout(fail,120000); try { const {Gecko}=await import(api+'assets/v0.0.1/gecko.js'); if(stopped)return; + const font=await fetch(api+'distribution/v0.0.1/NotoSansCJKjp-Regular.otf',{credentials:'same-origin',signal:startup.signal}); + if(!font.ok)throw Error('Bundled font unavailable'); + const data=new Uint8Array(await font.arrayBuffer());if(stopped)return; + if(data.byteLength!==16467736)throw Error('Bundled font size mismatch'); + const profile=memoryProfile();await profile.mkdir('fonts');await profile.writeFile('fonts/NotoSansCJKjp-Regular.otf',data); const socket=new URL(api+'wisp/',location.href);socket.protocol=location.protocol==='https:'?'wss:':'ws:'; - engine=new Gecko({canvas,wasm:{url:api+'assets/v0.0.1/gecko.wasm.zst',compressed:true},wispUrl:socket.href,profile:memoryProfile(),width:Math.min(innerWidth,1920),height:Math.min(innerHeight,1080),env:{GECKO_NOWASMJIT:'1'},print:()=>{},printErr:()=>{}}); + engine=new Gecko({canvas,wasm:{url:api+'assets/v0.0.1/gecko.wasm.zst',compressed:true},wispUrl:socket.href,profile,width:Math.min(innerWidth,1920),height:Math.min(innerHeight,1080),env:{GECKO_NOWASMJIT:'1'},print:()=>{},printErr:()=>{}}); await engine.init();if(stopped)return; await engine.load(href);if(stopped)return; history=[url(await engine.evalChrome('String(location.href)'))||href];position=0; - ready=true;report('');canvas.focus();send({ready:true}); + ready=true;clearTimeout(deadline);await pageState();canvas.focus();send({ready:true}); navigationState(); - const update=async()=>{if(stopped)return;try{if(!navigating){const href=url(await engine.evalChrome('String(location.href)'));if(href&&!navigating&&href!==history[position]){history=history.slice(0,position+1);history.push(href);if(history.length>64)history.shift();position=history.length-1;navigationState();}}}catch{/* Keep the last validated URL. */}if(!stopped)poll=setTimeout(update,2000);};update(); - } catch { report(text('Viewer failed. Close it and retry, or use ↗ to open the original.','ビューアを開始できませんでした。閉じて再試行するか、↗から原文を開いてください。')); } + const update=async()=>{if(stopped)return;try{if(!navigating){await pageState();const href=url(await engine.evalChrome('String(location.href)'));if(href&&!navigating&&href!==history[position]){history=history.slice(0,position+1);history.push(href);if(history.length>64)history.shift();position=history.length-1;navigationState();}}}catch{/* Keep the last validated URL. */}if(!stopped)poll=setTimeout(update,2000);};update(); + } catch {fail();} finally {clearTimeout(timer);} } addEventListener('message',event=>{ if(event.source!==parent||event.origin!==location.origin||event.data?.type!=='pageswiki-gecko')return; if(!started){const href=url(event.data.href);if(href)start(href);return;} if(['back','forward','reload'].includes(event.data.command))navigate(event.data.command); + if(event.data.command==='find')find(event.data.query,event.data.backwards); }); addEventListener('resize',()=>{clearTimeout(resizeTimer);resizeTimer=setTimeout(()=>{if(ready&&!stopped)engine.resize(Math.min(innerWidth,1920),Math.min(innerHeight,1080)).catch(()=>{});},150);}); -addEventListener('keydown',e=>{if(e.key==='Escape'){e.preventDefault();send({close:true});}},true); +addEventListener('keydown',e=>{if(e.key==='Escape'){e.preventDefault();send({close:true});}if((e.ctrlKey||e.metaKey)&&e.key.toLowerCase()==='f'){e.preventDefault();e.stopImmediatePropagation();send({find:true});}},true); addEventListener('pagehide',stop); send({listening:true}); diff --git a/static/js/feeds.js b/static/js/feeds.js index da15e72..8b5eca1 100644 --- a/static/js/feeds.js +++ b/static/js/feeds.js @@ -28,12 +28,14 @@ const transfer=button('⇄','OPML',opmlForm);transfer.id='feed-opml';toolbar.append(transfer); const history=button('◷',t('Operation history','操作履歴'),actionHistory);history.id='feed-history';toolbar.append(history); const helpButton=button('?',t('Keyboard shortcuts','キー操作'),showHelp),help=el('div',undefined,'feed-help');help.id='feed-help';help.hidden=true;helpButton.setAttribute('aria-controls',help.id);helpButton.setAttribute('aria-expanded','false'); - help.append(el('span',t('j/k articles · Shift+j/k folders · Enter read · Esc index · / search · n unread · m read/unread · * star · r refresh · ? / Esc close help','j/k 記事移動 · Shift+j/k フォルダ移動 · Enter 本文 · Esc 一覧 · / 検索 · n 次の未読 · m 既読/未読 · * star · r 更新 · ? / Esc ヘルプを閉じる')),button('×',t('Close help','ヘルプを閉じる'),hideHelp));status.before(help);toolbar.append(helpButton); + help.append(el('span',t('j/k articles · Shift+j/k folders / subscriptions · Shift+h parent · Shift+l enter · Enter read · Esc index · / search · n unread · m read/unread · * star · r refresh · ? / Esc close help','j/k 記事移動 · Shift+j/k フォルダ・購読元移動 · Shift+h 親へ · Shift+l 配下へ · Enter 本文 · Esc 一覧 · / 検索 · n 次の未読 · m 既読/未読 · * star · r 更新 · ? / Esc ヘルプを閉じる')),button('×',t('Close help','ヘルプを閉じる'),hideHelp));status.before(help);toolbar.append(helpButton); let debounce;searchbox.addEventListener('input',()=>{clearTimeout(debounce);debounce=setTimeout(()=>{search=searchbox.value.trim();load(false).catch(report);},180);}); - const columns=document.getElementById('feed-columns');for(const s of ['',t('Date','日付'),t('Feed','配信元'),t('Subject','件名')])columns.append(el('span',s)); + const columns=document.getElementById('feed-columns');for(const s of ['',t('Date','日付'),t('Time','時刻'),t('Feed','配信元'),t('Subject','件名')])columns.append(el('span',s)); function hideHelp(){if(help.contains(document.activeElement))helpButton.focus();help.hidden=true;helpButton.setAttribute('aria-expanded','false');} function showHelp(){if(!help.hidden){hideHelp();return;}help.hidden=false;helpButton.setAttribute('aria-expanded','true');} - function date(ms){return ms?new Intl.DateTimeFormat(ja?'ja-JP':'en-GB',{month:'2-digit',day:'2-digit'}).format(new Date(ms)):'—';} + const dateOptions={timeZone:root.dataset.timezone||undefined},dateFormat=new Intl.DateTimeFormat(ja?'ja-JP':'en-GB',{...dateOptions,month:'2-digit',day:'2-digit'}),timeFormat=new Intl.DateTimeFormat(ja?'ja-JP':'en-GB',{...dateOptions,hour:'2-digit',minute:'2-digit',hourCycle:'h23'}),fullDateFormat=new Intl.DateTimeFormat(ja?'ja-JP':'en-GB',{...dateOptions,year:'numeric',month:'2-digit',day:'2-digit',hour:'2-digit',minute:'2-digit',second:'2-digit',hourCycle:'h23',timeZoneName:'short'}); + function validDate(ms){return typeof ms==='number'&&ms>0&&Number.isFinite(new Date(ms).getTime());} + function date(ms){return validDate(ms)?dateFormat.format(new Date(ms)):'—';} function closeReader(){editing=false;detailSeq++;detailController?.abort();layout.classList.remove('reading','action-history');rows.focus();} function drawSources(){sourcesView.replaceChildren();const all=button(t('All sources','すべての購読元'),'',()=>chooseSource(''));sourcesView.append(all);let folder=null; for(const source of sources){if(folder!==source.folder){folder=source.folder;sourcesView.append(el('h3',folder||t('Sources','購読元')));}const row=el('div',undefined,'feed-source');row.setAttribute('aria-current',String(currentSource===source.uuid)); @@ -54,16 +56,36 @@ remoteCategory=id;categoryMode=mode;remoteFeed=feed;markCatalog();layout.classList.remove('sources-open','reading','action-history'); detailSeq++;detailController?.abort();clearTimeout(actionTimer);selected=-1;editing=false;reader.replaceChildren();await load(false); } + const sortedSubscriptions=subs=>[...subs].sort((a,b)=>a.title.localeCompare(b.title)||a.id.localeCompare(b.id)); function moveFolder(delta){ if(!catalogLoaded||sources.find(s=>s.uuid===currentSource)?.provider!=='freshrss')return; const groups=catalogGroups();if(!groups.length)return; const current=groups.findIndex(g=>g.id===remoteCategory&&g.mode===categoryMode); + if(remoteFeed){const g=groups[current],subs=sortedSubscriptions(g?g.subs:catalog),index=subs.findIndex(s=>s.id===remoteFeed)+delta;if(index<0||index>=subs.length)return;return navigateCatalog(g?.id||'',g?.mode||'',subs[index].id);} const index=current<0?(delta>0?0:groups.length-1):current+delta;if(index<0||index>=groups.length)return; - // Folder search must not leave the newly selected folder invisible. - if(catalogSearch){catalogSearch='';drawSources();} - const group=groups[index],result=chooseCatalog(group.id,group.mode); - const selectedFolder=sourcesView.querySelector('.feed-folder-name[aria-current="true"]'); - selectedFolder?.scrollIntoView({block:'nearest'});rows.focus({preventScroll:true});return result; + const group=groups[index];return navigateCatalog(group.id,group.mode); + } + function navigateCatalog(id,mode,feed=''){ + // Clear only the tree filter, never article search or unread/star filters. + catalogSearch=''; + const result=chooseCatalog(id,mode,feed);drawSources(); + sourcesView.querySelector('.feed-catalog button[aria-current="true"]')?.scrollIntoView({block:'nearest'});rows.focus({preventScroll:true});return result; + } + function enterFolder(enter){ + if(!catalogLoaded||sources.find(s=>s.uuid===currentSource)?.provider!=='freshrss')return; + const groups=catalogGroups(),group=groups.find(g=>g.id===remoteCategory&&g.mode===categoryMode),state=folderState(currentSource); + if(enter){ + if(remoteFeed)return; + if(!group){if(groups.length)return navigateCatalog(groups[0].id,groups[0].mode);return;} + state.add(JSON.stringify([group.mode,group.id])); + }else{ + if(remoteFeed)return navigateCatalog(group?.id||'',group?.mode||''); + if(!group)return; + state.delete(JSON.stringify([group.mode,group.id])); + } + try{localStorage.setItem('pageswiki.feed-folders.v1:'+currentSource,JSON.stringify([...state]));}catch{} + if(!enter)return navigateCatalog('',''); + const sub=sortedSubscriptions(group.subs)[0];return navigateCatalog(group.id,group.mode,sub?.id||''); } function drawCatalog(parent){ if(!catalogLoaded)return; @@ -74,8 +96,8 @@ const all=select(t('All subscriptions','すべての購読'),{id:'',mode:''});all.className='feed-tree-all';nav.append(all); const find=el('input');find.type='search';find.placeholder=find.ariaLabel=t('Find subscription / folder','購読・フォルダを検索');find.value=catalogSearch;nav.append(find); const list=el('div',undefined,'feed-folders');nav.append(list);parent.append(nav); - function subscriptions(target,subs,g){let count=0;const sorted=[...subs].sort((a,b)=>a.title.localeCompare(b.title)||a.id.localeCompare(b.id));const more=button('↓',t('More subscriptions','購読元をさらに表示'),append); - function append(){more.remove();for(const sub of sorted.slice(count,count+50)){const group=g||{id:'',mode:''},b=select(sub.title||sub.id,group,sub.id);b.title=(sub.title||sub.id)+(sub.categories.length?' · '+sub.categories.map(c=>c.label).join(' / '):'');target.append(b);}count+=50;if(counts.id===remoteFeed)+1);for(const sub of sorted.slice(count,end)){const group=g||{id:'',mode:''},b=select(sub.title||sub.id,group,sub.id);b.title=(sub.title||sub.id)+(sub.categories.length?' · '+sub.categories.map(c=>c.label).join(' / '):'');target.append(b);}count=end;if(count{list.replaceChildren();const query=find.value.trim().toLocaleLowerCase();catalogSearch=find.value; if(query){const matches=catalog.filter(s=>s.title.toLocaleLowerCase().includes(query)||s.categories.some(c=>c.label.toLocaleLowerCase().includes(query)));const results=el('div',undefined,'feed-subscriptions');list.append(results);subscriptions(results,matches,null);if(!matches.length)results.append(el('small',t('No subscriptions','購読なし')));return;} @@ -92,7 +114,15 @@ const data=await request('catalog?'+new URLSearchParams({source:currentSource}),{signal:catalogController.signal});if(version!==catalogSeq)return;catalog=data.subscriptions;catalogFolders=data.folders||[];catalogLoaded=true;drawSources(); } async function chooseSource(id){currentSource=id;remoteFeed=remoteCategory=categoryMode=catalogSearch='';catalog=[];catalogFolders=[];catalogLoaded=false;drawSources();layout.classList.remove('reading');editing=false;await Promise.all([loadCatalog(),load(false)]);if(sources.find(s=>s.uuid===id)?.provider!=='freshrss')layout.classList.remove('sources-open');} - function rowNode(item,index){const row=el('div',undefined,'feed-row'+(item.read?'':' unread'));row.id='feed-row-'+index;row.setAttribute('role','option');row.setAttribute('aria-selected',String(index===selected));for(const text of [(item.starred?'★':item.read?' ':'●'),date(item.published_ms),item.feed||sources.find(s=>s.uuid===item.source)?.title||'',item.title||t('(Untitled)','(無題)')])row.append(el('span',text));row.title=item.title;row.onclick=()=>open(index).catch(report);return row;} + function rowNode(item,index){ + const row=el('div',undefined,'feed-row'+(item.read?'':' unread')); + row.id='feed-row-'+index;row.setAttribute('role','option');row.setAttribute('aria-selected',String(index===selected)); + const known=validDate(item.published_ms),published=el('span',known?timeFormat.format(new Date(item.published_ms)):'—','feed-published-time'); + published.title=published.ariaLabel=t('Published: ','配信日時: ')+(known?fullDateFormat.format(new Date(item.published_ms)):t('Unknown','不明')); + row.append(el('span',item.starred?'★':item.read?' ':'●'),el('span',date(item.published_ms)),published, + el('span',item.feed||sources.find(s=>s.uuid===item.source)?.title||''),el('span',item.title||t('(Untitled)','(無題)'))); + row.title=item.title;row.onclick=()=>open(index).catch(report);return row; + } function drawRows(){rows.replaceChildren(...items.map(rowNode));more.hidden=!next;more.textContent='↓';more.title=more.ariaLabel=t('More articles','続きを表示');if(!items.length)rows.append(el('p',t('No articles','記事なし'),'feed-warning'));} async function load(append){if(append&&(!next||loading))return;controller?.abort();controller=new AbortController();const version=++seq;loading=true;more.disabled=true; try{const q=new URLSearchParams({source:currentSource,filter,q:search,feed:remoteFeed,category:remoteCategory,category_mode:categoryMode});if(append){q.set('before',next.ms);q.set('key',next.key);}const data=await request('items?'+q,{signal:controller.signal});if(version!==seq)return; @@ -115,13 +145,21 @@ const gecko=root.dataset.linkMode==='gecko', controller=new AbortController(); const close=button('×',t('Close preview','プレビューを閉じる'),()=>dialog.close());bar.append(name,external,close);frame.title=dialog.ariaLabel;frame.referrerPolicy='no-referrer'; if(gecko){ + const licenses=el('a','ⓘ');licenses.href=api+'browser/distribution/v0.0.1/index.html';licenses.target='_blank';licenses.rel='noopener noreferrer';licenses.title=licenses.ariaLabel=t('Engine sources and licenses','エンジンのソースとライセンス');bar.insertBefore(licenses,close); frame.src=api+'browser/';frame.allow='cross-origin-isolated'; - const send=command=>frame.contentWindow?.postMessage({type:'pageswiki-gecko',command},location.origin); + const send=(command,options={})=>frame.contentWindow?.postMessage({type:'pageswiki-gecko',command,...options},location.origin); + const findbar=el('form',undefined,'feed-gecko-find'),query=el('input'),findStatus=el('output');findbar.hidden=true;query.type='search';query.maxLength=256;query.placeholder=query.ariaLabel=t('Find in article','記事内を検索');findStatus.role='status'; + const findButton=button('⌕',t('Find in article (Ctrl+F)','記事内検索 (Ctrl+F)'),()=>{findbar.hidden=!findbar.hidden;if(!findbar.hidden)query.focus();});findButton.disabled=true;bar.insertBefore(findButton,name); + const search=backwards=>{if(query.value.trim())send('find',{query:query.value,backwards});}; + findbar.append(query,button('↑',t('Previous match','前の一致'),()=>search(true)),button('↓',t('Next match','次の一致'),()=>search(false)),findStatus,button('×',t('Close search','検索を閉じる'),()=>{findbar.hidden=true;findButton.focus();})); + findbar.onsubmit=e=>{e.preventDefault();search(false);};query.oninput=()=>findStatus.textContent='';query.addEventListener('keydown',e=>{if(e.key==='Escape'){e.preventDefault();findbar.hidden=true;findButton.focus();}else if(e.key==='Enter'&&e.shiftKey){e.preventDefault();search(true);}}); + dialog.addEventListener('keydown',e=>{if((e.ctrlKey||e.metaKey)&&e.key.toLowerCase()==='f'&&!findButton.disabled){e.preventDefault();findbar.hidden=false;query.focus();}}); + dialog.append(findbar); const nav={};for(const [icon,label,command]of [['←',t('Back','戻る'),'back'],['→',t('Forward','進む'),'forward'],['⟳',t('Reload','再読込'),'reload']]){const b=button(icon,label,()=>send(command));b.disabled=true;nav[command]=b;bar.insertBefore(b,name);} - addEventListener('message',e=>{if(e.origin!==location.origin||e.source!==frame.contentWindow||e.data?.type!=='pageswiki-gecko')return;if(e.data.listening)frame.contentWindow.postMessage({type:'pageswiki-gecko',href:url.href},location.origin);if(e.data.close)dialog.close();if(e.data.ready)nav.reload.disabled=false;if(typeof e.data.back==='boolean')nav.back.disabled=!e.data.back;if(typeof e.data.forward==='boolean')nav.forward.disabled=!e.data.forward;if(e.data.href){try{const u=new URL(e.data.href);if(['http:','https:'].includes(u.protocol)&&!u.username&&!u.password){name.textContent=u.hostname;name.title=external.href=u.href;}}catch{}}},{signal:controller.signal}); + addEventListener('message',e=>{if(e.origin!==location.origin||e.source!==frame.contentWindow||e.data?.type!=='pageswiki-gecko')return;if(e.data.listening)frame.contentWindow.postMessage({type:'pageswiki-gecko',href:url.href},location.origin);if(e.data.close)dialog.close();if(e.data.ready)nav.reload.disabled=findButton.disabled=false;if(e.data.find&&!findButton.disabled){findbar.hidden=false;query.focus();}if(typeof e.data.findResult==='boolean'&&e.data.query===query.value)findStatus.textContent=e.data.findResult?'':t('No match','一致なし');if(e.data.findError)findStatus.textContent=t('Search unavailable','検索できません');if(e.data.failed){for(const b of Object.values(nav))b.disabled=true;findButton.disabled=true;findbar.hidden=true;const error=el('p',t('Viewer unavailable. Close and retry, or open the original with ↗.','ビューアを開始できません。閉じて再試行するか、↗から原文を開いてください。'),'feed-warning');error.role='alert';frame.src='about:blank';frame.replaceWith(error);return;}if(typeof e.data.back==='boolean')nav.back.disabled=!e.data.back;if(typeof e.data.forward==='boolean')nav.forward.disabled=!e.data.forward;if(e.data.href){try{const u=new URL(e.data.href);if(['http:','https:'].includes(u.protocol)&&!u.username&&!u.password){name.textContent=u.hostname;name.title=external.href=u.href;}}catch{}}},{signal:controller.signal}); let hiddenTimer;document.addEventListener('visibilitychange',()=>{clearTimeout(hiddenTimer);if(document.hidden)hiddenTimer=setTimeout(()=>dialog.close(),60000);},{signal:controller.signal});dialog.addEventListener('close',()=>clearTimeout(hiddenTimer),{once:true}); }else{frame.setAttribute('sandbox','allow-scripts');frame.src=url.href;} - dialog.append(bar,frame);root.append(dialog); + dialog.prepend(bar);dialog.append(frame);root.append(dialog); dialog.addEventListener('keydown',e=>e.stopPropagation());dialog.addEventListener('close',()=>{controller.abort();frame.src='about:blank';dialog.remove();editing=wasEditing;const item=items[selected];if(item&&!editing)pollAction(item);});dialog.showModal();close.focus(); } function renderItem(item){reader.replaceChildren();const actions=el('div',undefined,'feed-actions');actions.append(button('←',t('Back to list','一覧へ戻る'),closeReader)); @@ -133,7 +171,7 @@ const original=actions.querySelector('.feed-original');if(original)original.addEventListener('click',e=>openArticleLink(e,original.href)); if(item.url&&root.dataset.shareX==='true'){const share=el('a',undefined,'feed-share-x feed-original');share.title=share.ariaLabel=t('Share original article on X','原文の記事をXでシェア');share.href='https://twitter.com/intent/tweet?'+new URLSearchParams({text:item.title||'',url:item.url});share.target='_blank';share.rel='noopener noreferrer';const svg=document.createElementNS('http://www.w3.org/2000/svg','svg'),path=document.createElementNS(svg.namespaceURI,'path');svg.setAttribute('viewBox','0 0 24 24');svg.setAttribute('aria-hidden','true');path.setAttribute('d','M18 3h3l-7 8 8 10h-6l-5-7-6 7H2l7-9L2 3h6l5 6z');svg.append(path);share.append(svg);actions.append(share);} if(writable)actions.append(button(t('+ Wiki / Task','+ Wiki・タスク'),t('Capture with attribution','出典付きで取り込む'),()=>captureForm(item))); - reader.append(actions,el('h1',item.title||t('(Untitled)','(無題)')),el('p',(item.feed||'')+' · '+(item.published_ms?new Date(item.published_ms).toLocaleString():t('Date unknown','日付不明')),'feed-meta')); + reader.append(actions,el('h1',item.title||t('(Untitled)','(無題)')),el('p',(item.feed||'')+' · '+(validDate(item.published_ms)?fullDateFormat.format(new Date(item.published_ms)):t('Date unknown','日付不明')),'feed-meta')); if(item.provider==='freshrss'&&!canWrite)read.title=star.title=t('FreshRSS · read only','FreshRSS · 読取専用'); const body=el('div',undefined,'feed-body'); if(item.body_html){body.classList.add('feed-body-html');const shadow=body.attachShadow({mode:'open'}),style=el('style',':host{display:block;overflow-wrap:anywhere;line-height:1.6}p{margin:.6em 0}h1,h2,h3{line-height:1.3}a{color:var(--link,var(--accent,#659de4))}pre{white-space:pre-wrap;overflow-wrap:anywhere;padding:.5em;background:color-mix(in srgb,currentColor 5%,transparent)}blockquote{margin:.5em 0;padding-left:1em;border-left:3px solid var(--border,#8885)}table{border-collapse:collapse;max-width:100%;display:block;overflow:auto}th,td{padding:.3em .6em;border:1px solid var(--border,#8885)}');const content=el('div');content.innerHTML=item.body_html;shadow.append(style,content);} @@ -240,7 +278,7 @@ }; } document.addEventListener('keydown',event=>{if(event.defaultPrevented||event.isComposing||event.ctrlKey||event.altKey||event.metaKey||document.querySelector('dialog[open]'))return;if(event.key==='Escape'&&!help.hidden){hideHelp();event.preventDefault();return;}const input=event.target.closest?.('input,textarea,select,[contenteditable]:not([contenteditable=false]),[role=textbox]');if(input||event.target.isContentEditable){if(event.key==='Escape'){event.target.blur();event.preventDefault();}return;}if(event.key==='?'&&!help.hidden){hideHelp();event.preventDefault();return;}if(event.key==='Enter'&&event.target.closest?.('button,a'))return;if(editing){if(event.key==='Escape'){closeReader();event.preventDefault();}return;} - if(event.shiftKey&&['j','k'].includes(event.key.toLowerCase())){event.preventDefault();Promise.resolve().then(()=>moveFolder(event.key.toLowerCase()==='j'?1:-1)).catch(report);return;} + if(event.shiftKey&&['h','j','k','l'].includes(event.key.toLowerCase())){event.preventDefault();const key=event.key.toLowerCase();Promise.resolve().then(()=>key==='h'||key==='l'?enterFolder(key==='l'):moveFolder(key==='j'?1:-1)).catch(report);return;} let action;switch(event.key){case 'j':case 'ArrowDown':action=()=>open(Math.min(items.length-1,selected+1));break;case 'k':case 'ArrowUp':action=()=>open(Math.max(0,selected-1));break;case 'Enter':case 'l':action=()=>open(Math.max(0,selected));break;case 'Escape':case 'h':action=closeReader;break;case '/':action=()=>searchbox.focus();break;case 'r':if(writable)action=queueRefresh;break;case 'm':action=()=>setState(!items[selected]?.read,!!items[selected]?.starred);break;case '*':action=()=>setState(!!items[selected]?.read,!items[selected]?.starred);break;case 'n':action=()=>{const i=items.findIndex((x,i)=>i>selected&&!x.read);if(i>=0)return open(i);};break;case '?':action=showHelp;break;}if(action){event.preventDefault();Promise.resolve().then(action).catch(report);} }); (async()=>{const q=new URLSearchParams(location.search);currentSource=q.get('source')||'';await reload();if(q.has('settings')&&writable){sourceForm();return;}if(q.has('id')&¤tSource){let index=items.findIndex(x=>x.source===currentSource&&x.id===q.get('id'));if(index<0){const data=await request('item?'+new URLSearchParams({source:currentSource,id:q.get('id')}));items.unshift(data.item);index=0;drawRows();}await open(index);}})().catch(report); diff --git a/static/js/tasks.js b/static/js/tasks.js index 6701ed7..392ab8e 100644 --- a/static/js/tasks.js +++ b/static/js/tasks.js @@ -121,6 +121,7 @@ } async function open(id,kind='task') { if($('dialog').open&&!closeDialog())return; + $('page-open').hidden=true;$('page-open').removeAttribute('href'); const ticket=++detailGeneration;dialogController?.abort();dialogController=new AbortController();$('error').textContent='';editing=null;dirty=false;$('fields').replaceChildren();Object.keys(fields).forEach(k=>delete fields[k]);$('history').open=false;$('history').querySelector('ol').replaceChildren();$('dialog-title').textContent=t('loading');$('save').disabled=true;$('archive').hidden=$('delete').hidden=true;$('history').hidden=true;$('project-open').hidden=true;$('dialog').showModal(); try { if(!id&&kind==='task')await refreshDefaults(); @@ -147,9 +148,11 @@ } if(editing.kind==='project'){const folder=field('notes_subpath',t('notes_subpath'),'text',editing.notes_subpath||'tasks',true);folder.maxLength=500;folder.placeholder='tasks';$('fields').append(el('small',ja?'例:関連Wikiが /projects/app、サブ階層が tasks → /projects/app/tasks/件名。Wikiの公開範囲に従います。':'Example: project Wiki /projects/app + subfolder tasks → /projects/app/tasks/title. Wiki permissions apply.','task-full'));} field('page_path',t('page'),'text',data.page_path,true).placeholder='/lab/project'; - if(data.page_path){const a=el('a',t('linked')+' ↗');a.href='/'+data.page_path.replace(/^\/+/, '').split('/').map(encodeURIComponent).join('/');$('fields').append(a);}else if(editing.page_uuid)$('fields').append(el('small',t('hiddenLink'))); - if(editing.page_uuid){const unlink=el('button',t('unlink'));unlink.type='button';unlink.disabled=!writable;unlink.addEventListener('click',()=>{editing.page_uuid='';fields.page_path.value='';fields.page_path.dataset.changed='true';dirty=true;unlink.disabled=true;});$('fields').append(unlink);} - fields.page_path.addEventListener('input',()=>{fields.page_path.dataset.changed='true';}); + const updatePageLink=()=>{const a=$('page-open');a.hidden=!data.page_path||!editing.page_uuid||fields.page_path.value!==data.page_path;a.removeAttribute('href');if(!a.hidden)a.href='/'+data.page_path.replace(/^\/+/, '').split('/').map(encodeURIComponent).join('/');}; + $('page-open').textContent=t('linked')+' ↗';$('page-open').title=ja?'関連ページを新しいタブで開く':'Open related page in a new tab';updatePageLink(); + if(!data.page_path&&editing.page_uuid)$('fields').append(el('small',t('hiddenLink'))); + if(editing.page_uuid){const unlink=el('button',t('unlink'));unlink.type='button';unlink.disabled=!writable;unlink.addEventListener('click',()=>{editing.page_uuid='';fields.page_path.value='';fields.page_path.dataset.changed='true';dirty=true;unlink.disabled=true;updatePageLink();});$('fields').append(unlink);} + fields.page_path.addEventListener('input',()=>{fields.page_path.dataset.changed='true';updatePageLink();}); if(id&&editing.kind==='task'){ if(writable&&!editing.archived&&!['done','cancelled'].includes(editing.status)){const focus=el('button',ja?'◷ このタスクに集中':'◷ Focus on this task','task-full');focus.type='button';focus.addEventListener('click',()=>{if(dirty){$('error').textContent=ja?'先にタスクの変更を保存してください。':'Save your task changes first.';return;}const task={...editing};if(closeDialog())app.dispatchEvent(new CustomEvent('task-focus',{detail:task}));});$('fields').append(focus);} const plan=el('button',ja?'▦ 時間枠をカレンダーへ':'▦ Calendar time blocks','task-full');plan.type='button';plan.id='tasks-plan-open'; diff --git a/templates/pages/auth.gohtml b/templates/pages/auth.gohtml index 8d66cc2..92b1bf8 100644 --- a/templates/pages/auth.gohtml +++ b/templates/pages/auth.gohtml @@ -11,8 +11,9 @@ {{ if .Setup }}{{ end }} +{{ if and (not .Setup) .TrustedSessionDays }}{{ end }} {{ if and (not .Setup) (not .MFA) .OIDCEnabled }}
{{ T "auth.or" }}
{{ T "auth.login_oidc" }}{{ end }} {{ if and (not .Setup) (not .MFA) .LDAPEnabled }}
{{ T "auth.or" }}
{{ end }} {{ if and (not .Setup) (not .MFA) .MagicEnabled }}{{ if .MagicSent }}

{{ T "auth.magic_sent" }}

{{ else }}
{{ end }}{{ end }} -{{ end }}{{ end }} +{{ end }}{{ end }} diff --git a/templates/pages/browser.gohtml b/templates/pages/browser.gohtml index acf9746..2186ee4 100644 --- a/templates/pages/browser.gohtml +++ b/templates/pages/browser.gohtml @@ -455,7 +455,7 @@ onmousedown="startResize(event, 'sidebar')" > -
+
@@ -1112,6 +1112,8 @@ restoreViewMode() restorePanes() + +

{{ T "browser.new" }}

diff --git a/templates/pages/config.gohtml b/templates/pages/config.gohtml index c2895f5..22a8e8c 100644 --- a/templates/pages/config.gohtml +++ b/templates/pages/config.gohtml @@ -1169,6 +1169,7 @@ Vim

{{ T "auth.sessions" }}

{{ T "auth.session_policy_help" }}

+
{{ range .AuthSessions }}{{ else }}{{ end }}
{{ T "auth.account" }}{{ T "auth.auth_time" }}{{ T "auth.last_seen" }}{{ T "auth.expires" }}MFA
{{ .DisplayName }}{{ formatUnixMS .AuthTimeMS }}{{ formatUnixMS .LastSeenAtMS }}{{ formatUnixMS .ExpiresAtMS }}{{ .MFALevel }}
—

{{ T "config.event_definitions" }}

diff --git a/templates/pages/feeds.gohtml b/templates/pages/feeds.gohtml index 013d98e..ad64835 100644 --- a/templates/pages/feeds.gohtml +++ b/templates/pages/feeds.gohtml @@ -1,9 +1,9 @@ {{ define "feeds.gohtml" }} {{ T "feeds.title" }} · {{ .WikiName }} - +
{{ template "auth-menu" . }}
{{ template "organizer-nav" . }} -
+

-
{{ end }} +
{{ end }} diff --git a/templates/pages/tasks.gohtml b/templates/pages/tasks.gohtml index f35a63b..0ddc591 100644 --- a/templates/pages/tasks.gohtml +++ b/templates/pages/tasks.gohtml @@ -10,5 +10,5 @@

-

    +

      {{ end }} diff --git a/templates/partials/auth_cli.gohtml b/templates/partials/auth_cli.gohtml index a48d1ee..dbb2b67 100644 --- a/templates/partials/auth_cli.gohtml +++ b/templates/partials/auth_cli.gohtml @@ -4,6 +4,7 @@ {{ else }}

      {{ T "auth.cli_warning" }}

      {{ .CLIOrigin }}

      +{{ if .CLIRememberDays }}

      {{ T "auth.remember_cli" }}: {{ .CLIRememberDays }} {{ T "auth.days" }}

      {{ end }} {{ if .CLILogin }} {{ T "auth.login_oidc" }} {{ else }} diff --git a/tools/ci/release-images.mjs b/tools/ci/release-images.mjs index cbfff71..8e6a4af 100644 --- a/tools/ci/release-images.mjs +++ b/tools/ci/release-images.mjs @@ -1,7 +1,7 @@ import {spawnSync} from 'node:child_process'; import {pathToFileURL} from 'node:url'; -export const images = ['pageswiki-app', 'pageswiki-adaptor-gateway', 'pageswiki-excalidraw-viewer']; +export const images = ['pageswiki-app', 'pageswiki-adaptor-gateway', 'pageswiki-excalidraw-viewer', 'pageswiki-gecko-relay']; // SemVer 2.0.0: numeric prerelease identifiers cannot have leading zeroes. const number = '(0|[1-9][0-9]*)'; const identifier = '(?:0|[1-9][0-9]*|[0-9]*[A-Za-z-][0-9A-Za-z-]*)'; diff --git a/tools/ci/release-images.test.mjs b/tools/ci/release-images.test.mjs index 66fabb0..d5e81ff 100644 --- a/tools/ci/release-images.test.mjs +++ b/tools/ci/release-images.test.mjs @@ -37,19 +37,19 @@ function registry() { return {client, entries, copies}; } -test('promotes three sources by digest and retry does not rewrite', () => { +test('promotes all sources by digest and retry does not rewrite', () => { const {client, copies} = registry(); const plan = promote(options, client, () => {}); - assert.equal(copies.length, 3); + assert.equal(copies.length, images.length); for (const item of plan) assert.ok(item.source.endsWith(`@${item.digest}`)); promote(options, client, () => {}); - assert.equal(copies.length, 3); + assert.equal(copies.length, images.length); }); test('missing source, wrong revision or existing release conflict prevents all writes', () => { for (const failure of ['missing', 'revision', 'conflict']) { const {client, entries, copies} = registry(); - const last = `${options.prefix}/${images[2]}`; + const last = `${options.prefix}/${images.at(-1)}`; if (failure === 'missing') entries.delete(`${last}:sha-${revision}`); if (failure === 'revision') client.revision = () => 'b'.repeat(40); if (failure === 'conflict') entries.set(`${last}:v0.1.0`, `sha256:${'f'.repeat(64)}`); @@ -68,7 +68,7 @@ test('partial promotion is safely resumable', () => { assert.throws(() => promote(options, client, () => {}), /unavailable/); client.copy = copy; promote(options, client, () => {}); - assert.equal(copies.length, 3); + assert.equal(copies.length, images.length); }); test('destination digest mismatch and concurrent overwrite are failures', () => { @@ -79,7 +79,7 @@ test('destination digest mismatch and concurrent overwrite are failures', () => const get = other.client.digest; let reads = 0; other.client.digest = (ref, optional) => { - if (ref.endsWith(':v0.1.0') && ++reads > 3) return `sha256:${'f'.repeat(64)}`; + if (ref.endsWith(':v0.1.0') && ++reads > images.length) return `sha256:${'f'.repeat(64)}`; return get(ref, optional); }; assert.throws(() => promote(options, other.client), /concurrently/); diff --git a/tools/gecko/README.txt b/tools/gecko/README.txt new file mode 100644 index 0000000..258e902 --- /dev/null +++ b/tools/gecko/README.txt @@ -0,0 +1,46 @@ +Gecko WASM v0.0.1 distribution for PagesWiki + +The gecko.js and gecko.wasm.zst binaries are unmodified upstream release files. +PagesWiki's relay and UI are separate software. This is not an official Mozilla +Firefox distribution and no Mozilla trademarks or logos are licensed here. + +Corresponding source and build recipe: + wrapper.tar.gz: HeyPuter/firefox-wasm eadcb5fa828d7e8b54ae2df13b6b3208f6553944 + firefox.tar.gz: MercuryWorkshop/firefox 2e1e835a5da5907e5ceef19aade58a7493f1e34d + emscripten.tar.gz: Emscripten 6.0.1 (runtime and bundled libc sources/notices) + rust-src.tar.xz: Rust 1.95.0 standard library; .crate files are all registry + dependencies of library/Cargo.lock, checked against that lock's checksums. + zstddec.tgz: zstddec 0.2.0, MIT and BSD-3-Clause notices. + wrapper-MPL.txt: MPL-2.0 declaration added by upstream at + ac40f56c85060bd49dfc36ca60490f97dd270c50. The wrapper's core source and + Emscripten patches were unchanged between these commits. + vex-sdk-LICENSE.txt: MIT notice from the crate's source commit, omitted from + the published crate. Fortanix's in-source copyright/MPL header is retained. + NotoSansCJKjp-Regular.otf: unmodified Noto Sans CJK JP Sans2.004, commit + 523d033d6cb47f4a80c58a35753646f5c3608a78. Embedded copyright notice: + © 2014-2021 Adobe (http://www.adobe.com/). + SIL Open Font License 1.1, reproduced in NotoSansCJK-LICENSE.txt. No font + names or embedded notices are modified. Source repository: notofonts/noto-cjk. + +Extract wrapper.tar.gz into a working directory, put the extracted Firefox +source at firefox/, and follow the included README and Makefile. Use the fixed +Firefox revision above, Emscripten 6.0.1, Rust 1.95.0 and its rust-src, and the +included pnpm-lock.yaml. make emsdk applies the included WasmFS patches; +vendor-std-deps.py verifies the standard library crates. The upstream Makefile, +mozconfig and build scripts describe the release build. Building is +not performed by the PagesWiki runtime. The upstream build timestamp is not +fixed; byte-for-byte reproducibility is not claimed. + +The source archives preserve their original licenses, copyrights and patches. +The *.notices.txt files collect license, copying, copyright, notice and author +files from those archives. Mozilla's license.html is converted to inert readable +text with ALL conditional sections included, a conservative superset. Original +HTML and build conditionals remain in firefox.tar.gz. Liberation fonts (SIL OFL +1.1, reserved names retained) are covered by that archive's PDF.js font notice. +Gecko's source archive includes its vendored third-party/Rust source and notices. + +Upstream source URLs and exact hashes/sizes are recorded in manifest.json. +Keep this complete bundle available to recipients whenever the engine is served, +and preserve the source offer after disabling the feature. Do not deploy a pair +of engine files alone. Do not remove notices because a component is believed +unused without reviewing the actual build. No additional warranty is provided. diff --git a/tools/gecko/bundle.py b/tools/gecko/bundle.py new file mode 100644 index 0000000..323ad43 --- /dev/null +++ b/tools/gecko/bundle.py @@ -0,0 +1,163 @@ +#!/usr/bin/env python3 +"""Build an offline, hash-locked Gecko distribution without extracting source trees. + +Python 3.11+. Sources and notices remain available alongside the engine. The +output is a new directory: never replace a bundle mounted by a running relay. +""" +import argparse +import hashlib +import html +from html.parser import HTMLParser +import json +from pathlib import Path, PurePosixPath +import re +import shutil +import tarfile +import tomllib +import urllib.request + +ROOT = Path(__file__).resolve().parent + + +def digest(path): + with path.open('rb') as stream: + return hashlib.file_digest(stream, 'sha256').hexdigest() + + +def obtain(cache, name, url, expected, offline=False): + path = cache / name + if not path.exists(): + if offline: + raise ValueError('missing offline input: ' + name) + temporary = path.with_suffix(path.suffix + '.part') + with urllib.request.urlopen(url, timeout=60) as response, temporary.open('wb') as target: + shutil.copyfileobj(response, target, 1 << 20) + if digest(temporary) != expected: + temporary.unlink() + raise ValueError('download checksum mismatch: ' + name) + temporary.rename(path) + if digest(path) != expected: + raise ValueError('cached checksum mismatch: ' + name) + return path + + +class NoticeText(HTMLParser): + """Readable superset of Mozilla's conditional notice, without active HTML.""" + def __init__(self): + super().__init__(convert_charrefs=True) + self.parts = [] + self.skip = 0 + + def handle_starttag(self, tag, attrs): + if tag in ('script', 'style'): + self.skip += 1 + if not self.skip and tag in ('p', 'li', 'pre', 'h1', 'h2', 'h3', 'br', 'div'): + self.parts.append('\n') + if not self.skip and tag == 'a': + href = dict(attrs).get('href', '') + if href.startswith(('https://', 'http://')): + self.parts.append(' [' + href + '] ') + + def handle_endtag(self, tag): + if tag in ('script', 'style') and self.skip: + self.skip -= 1 + + def handle_data(self, value): + if not self.skip: + self.parts.append(value) + + +def notices(archive): + chunks = [] + with tarfile.open(archive) as source: + for member in source: + name = PurePosixPath(member.name).name + if not member.isfile() or not re.match(r'(?i)^(licen[cs]e|copying|copyright|notice|authors)([._-].*|$)', name): + continue + if member.size > 8 << 20: + raise ValueError('oversized notice: ' + member.name) + raw = source.extractfile(member).read().decode('utf-8', errors='replace') + if name == 'license.html': + parser = NoticeText() + # Include ALL conditional branches (a conservative superset). + parser.feed(re.sub(r'^#.*$', '', raw, flags=re.M)) + raw = ''.join(parser.parts) + chunks.append('\n\n===== ' + member.name + ' =====\n\n' + raw) + if not chunks: + if archive.name == 'fortanix-sgx-abi-0.6.1.crate': + raw = member_bytes(archive, 'src/lib.rs').decode() + return raw.split('*/', 1)[0] + '*/\nFull MPL-2.0 text: wrapper-MPL.txt\n' + if archive.name == 'vex-sdk-0.27.1.crate': + return 'MIT license and copyright notice: vex-sdk-LICENSE.txt\nOriginal metadata:\n' + member_bytes(archive, 'Cargo.toml').decode() + raise ValueError('no notices found: ' + archive.name) + return ''.join(chunks) + + +def member_bytes(archive, suffix): + with tarfile.open(archive) as source: + matches = [m for m in source if m.isfile() and m.name.endswith('/' + suffix)] + if len(matches) != 1 or matches[0].size > 64 << 20: + raise ValueError('ambiguous/missing member: ' + suffix) + return source.extractfile(matches[0]).read() + + +def build(cache, output, offline=False): + if output.exists(): + raise ValueError('output already exists; choose a new immutable directory') + lock = json.loads((ROOT / 'sources.lock.json').read_text()) + cache.mkdir(parents=True, exist_ok=True) + inputs = {entry['file']: obtain(cache, entry['file'], entry['url'], entry['sha256'], offline) for entry in lock['inputs']} + # Rust build-std vendors its own Cargo.lock packages, not just Gecko crates. + cargo = tomllib.loads(member_bytes(inputs['rust-src.tar.xz'], 'library/Cargo.lock').decode()) + crates = [] + for package in cargo['package']: + if 'source' not in package: + continue + if package['source'] != 'registry+https://github.com/rust-lang/crates.io-index': + raise ValueError('unsupported Rust dependency source') + name = package['name'] + '-' + package['version'] + '.crate' + url = 'https://static.crates.io/crates/' + package['name'] + '/' + name + crates.append((name, obtain(cache, name, url, package['checksum'], offline), url)) + output.mkdir(parents=True) + records = [] + + def record(name, origin, kind): + records.append(dict(file=name, sha256=digest(output / name), size=(output / name).stat().st_size, origin=origin, kind=kind)) + + for entry in lock['inputs']: + name = entry['file'] + shutil.copyfile(inputs[name], output / name) + record(name, entry['url'], 'notice' if name.endswith('.txt') else 'source' if name != 'engine.tar.gz' else 'upstream-binary') + if name.endswith(('.tar.gz', '.tar.xz', '.tgz')) and name not in ('engine.tar.gz', 'wrapper.tar.gz'): + target = name + '.notices.txt' + (output / target).write_text(notices(inputs[name]), encoding='utf-8') + record(target, name, 'notice') + for name, source, url in crates: + shutil.copyfile(source, output / name) + record(name, url, 'source') + target = name + '.notices.txt' + (output / target).write_text(notices(source), encoding='utf-8') + record(target, name, 'notice') + for name in ('gecko.js', 'gecko.wasm.zst'): + (output / name).write_bytes(member_bytes(inputs['engine.tar.gz'], 'dist/' + name)) + record(name, 'engine.tar.gz', 'engine') + if records[-1]['sha256'] != lock['engine'][name]: + raise ValueError('engine payload mismatch: ' + name) + # This file describes preservation of originals and source/build relationships. + shutil.copyfile(ROOT / 'README.txt', output / 'README.txt') + record('README.txt', 'PagesWiki tools/gecko/README.txt', 'notice') + rows = ''.join('' + html.escape(r['file']) + '' + r['kind'] + '' + str(r['size']) + '' + r['sha256'] + '' for r in records) + (output / 'index.html').write_text('Gecko sources and licenses

      Gecko sources and licenses

      Unmodified upstream Gecko WASM v0.0.1. Not an official Mozilla Firefox distribution. Source and build instructions. SHA-256 manifest.

      Notices include all build-conditional sections and vendored source notices; some components may not be linked. Originals remain in the source archives. License texts and copyright notices are not replaced by this index.

      ' + rows + '
      FileKindBytesSHA-256
      ', encoding='utf-8') + record('index.html', 'generated index', 'index') + (output / 'manifest.json').write_text(json.dumps(dict(format='pageswiki.gecko-distribution.v1', version='v0.0.1', files=records), indent=2, sort_keys=True) + '\n') + print('manifest SHA-256:', digest(output / 'manifest.json'), flush=True) + print('files:', len(records), 'bytes:', sum(r['size'] for r in records), flush=True) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--cache', type=Path, required=True) + parser.add_argument('--output', type=Path, required=True) + parser.add_argument('--offline', action='store_true') + args = parser.parse_args() + build(args.cache, args.output, args.offline) diff --git a/tools/gecko/sources.lock.json b/tools/gecko/sources.lock.json new file mode 100644 index 0000000..e54aa3e --- /dev/null +++ b/tools/gecko/sources.lock.json @@ -0,0 +1,18 @@ +{ + "engine": { + "gecko.js": "e5aeebffd21106eccf23b1219316221add5e95085bc5a1bfb941117cfcd75ef7", + "gecko.wasm.zst": "ebd6c75179152624d9644865e11792ed25cadc714d9e0985f412baf9c637a086" + }, + "inputs": [ + {"file":"engine.tar.gz","url":"https://github.com/HeyPuter/firefox-wasm/releases/download/v0.0.1/gecko.js-v0.0.1.tar.gz","sha256":"bac28056a00ef6e5fb4345117e73d3629e75aca00dac71389fabbce39c018a73"}, + {"file":"wrapper.tar.gz","url":"https://codeload.github.com/HeyPuter/firefox-wasm/tar.gz/eadcb5fa828d7e8b54ae2df13b6b3208f6553944","sha256":"60078bb4c68ed97e95b5720b9833a9454aac0668fb069aa53393e96628e845e1"}, + {"file":"firefox.tar.gz","url":"https://codeload.github.com/MercuryWorkshop/firefox/tar.gz/2e1e835a5da5907e5ceef19aade58a7493f1e34d","sha256":"d3d3907c34e498e2a1ef2ada1d11797a4e8075b49ab45b1968aa49d5cddc3096"}, + {"file":"emscripten.tar.gz","url":"https://codeload.github.com/emscripten-core/emscripten/tar.gz/refs/tags/6.0.1","sha256":"0187d75d8ff8c5345fcf37deb92c24b7ebd2eb1fe04339c21ea2bcd9d81cc681"}, + {"file":"rust-src.tar.xz","url":"https://static.rust-lang.org/dist/rust-src-1.95.0.tar.xz","sha256":"67b09138c8db96afc4bbfc69ea771ac9a091fd777698acb43f6dfd9fb7dea363"}, + {"file":"zstddec.tgz","url":"https://registry.npmjs.org/zstddec/-/zstddec-0.2.0.tgz","sha256":"4708fc2e97b5ae6e8c2545ceabdd965bfe8858a5064fce30380eeb4c6ba3f41d"}, + {"file":"wrapper-MPL.txt","url":"https://raw.githubusercontent.com/HeyPuter/firefox-wasm/ac40f56c85060bd49dfc36ca60490f97dd270c50/LICENSE","sha256":"3f3d9e0024b1921b067d6f7f88deb4a60cbe7a78e76c64e3f1d7fc3b779b9d04"}, + {"file":"vex-sdk-LICENSE.txt","url":"https://raw.githubusercontent.com/vexide/vex-sdk/6c0b83d35b6fc31a7607efe456e387df45476314/LICENSE.md","sha256":"7bc7d5de2a0b793d4e1adb7ffcb99387776adc1f15f055bc3b667ea449249e6a"}, + {"file":"NotoSansCJKjp-Regular.otf","url":"https://raw.githubusercontent.com/notofonts/noto-cjk/523d033d6cb47f4a80c58a35753646f5c3608a78/Sans/OTF/Japanese/NotoSansCJKjp-Regular.otf","sha256":"68a3fc98800b2a27b371f2fb79991daf3633bd89309d4ffaa6946fd587f375b5"}, + {"file":"NotoSansCJK-LICENSE.txt","url":"https://raw.githubusercontent.com/notofonts/noto-cjk/523d033d6cb47f4a80c58a35753646f5c3608a78/LICENSE","sha256":"6a73f9541c2de74158c0e7cf6b0a58ef774f5a780bf191f2d7ec9cc53efe2bf2"} + ] +} diff --git a/tools/gecko/test_bundle.py b/tools/gecko/test_bundle.py new file mode 100644 index 0000000..62f22a1 --- /dev/null +++ b/tools/gecko/test_bundle.py @@ -0,0 +1,50 @@ +import hashlib +import io +from pathlib import Path +import tarfile +import tempfile +import unittest + +import bundle + + +class BundleTest(unittest.TestCase): + def test_notice_superset_is_inert(self): + p = bundle.NoticeText() + p.feed('

      Licenses

      Copyright & authors

      Terms') + result = ''.join(p.parts) + self.assertIn('Copyright & authors', result) + self.assertIn('https://example.org/license', result) + self.assertNotIn('bad()', result) + self.assertNotIn('hidden', result) + + def test_hash_rejects_changed_cached_source(self): + with tempfile.TemporaryDirectory() as name: + cache = Path(name) + (cache / 'source').write_bytes(b'changed') + with self.assertRaisesRegex(ValueError, 'checksum'): + bundle.obtain(cache, 'source', 'https://invalid.test', '0' * 64, True) + self.assertEqual(bundle.obtain(cache, 'source', '', hashlib.sha256(b'changed').hexdigest(), True), cache / 'source') + + def test_archive_is_read_not_extracted(self): + with tempfile.TemporaryDirectory() as name: + path = Path(name) / 'source.tar.gz' + with tarfile.open(path, 'w:gz') as archive: + member = tarfile.TarInfo('../../LICENSE') + data = b'Copyright owner. Permission notice.' + member.size = len(data) + archive.addfile(member, io.BytesIO(data)) + member = tarfile.TarInfo('src/license.html') + data = b'#ifdef OPTIONAL\n

      Optional terms

      \n#else\n

      Other terms

      \n#endif' + member.size = len(data) + archive.addfile(member, io.BytesIO(data)) + notice = bundle.notices(path) + self.assertIn('Copyright owner', notice) + self.assertIn('Optional terms', notice) + self.assertIn('Other terms', notice) + self.assertNotIn('#ifdef', notice) + self.assertEqual(list(Path(name).iterdir()), [path]) + + +if __name__ == '__main__': + unittest.main()