@posissue/server 0.0.0-stage → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +49 -2
- package/dist/github.d.ts +34 -0
- package/dist/github.js +96 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +455 -0
- package/dist/pages.d.ts +56 -0
- package/dist/pages.js +599 -0
- package/dist/session.d.ts +12 -0
- package/dist/session.js +71 -0
- package/package.json +46 -4
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare const SESSION_TTL_SEC: number;
|
|
2
|
+
/** 変更してよいリポジトリと、そのときのインストール ID の組 */
|
|
3
|
+
export type Grant = [repo: string, installationId: number];
|
|
4
|
+
export declare function signSession(secret: string, repos: Grant[], now?: number): Promise<string>;
|
|
5
|
+
/** 署名と期限が正しければ、触ってよいリポジトリとインストール ID の組の一覧。そうでなければ null。 */
|
|
6
|
+
export declare function verifySession(secret: string, token: unknown, now?: number): Promise<Grant[] | null>;
|
|
7
|
+
export declare const MAX_ORIGINS = 10;
|
|
8
|
+
export declare const DAILY_LIMIT_MAX = 1000;
|
|
9
|
+
/** 「埋め込みを許すサイト」の入力を検証して正規化する。不正なものが 1 つでもあれば null。 */
|
|
10
|
+
export declare function normalizeOrigins(input: unknown): string[] | null;
|
|
11
|
+
/** 1 日の上限。null は既定値に戻す。 */
|
|
12
|
+
export declare function normalizeDailyLimit(input: unknown): number | null | undefined;
|
package/dist/session.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// 仕様: docs/spec/04-http-api.html 4.6(作者の設定変更)
|
|
2
|
+
// キー確認画面に埋め込む短命の署名付きトークン。中身は「このユーザーが触ってよいリポジトリ」と期限だけ。
|
|
3
|
+
// クッキーではなくリクエスト本文で送るので CSRF の対象にならない。
|
|
4
|
+
const enc = new TextEncoder();
|
|
5
|
+
const b64url = (b) => btoa(String.fromCharCode(...b)).replace(/=+$/, "").replace(/\+/g, "-").replace(/\//g, "_");
|
|
6
|
+
const unb64url = (s) => Uint8Array.from(atob(s.replace(/-/g, "+").replace(/_/g, "/")), (c) => c.charCodeAt(0));
|
|
7
|
+
const hmacKey = (secret, use) => crypto.subtle.importKey("raw", enc.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, use);
|
|
8
|
+
export const SESSION_TTL_SEC = 15 * 60;
|
|
9
|
+
export async function signSession(secret, repos, now = Math.floor(Date.now() / 1000)) {
|
|
10
|
+
const body = b64url(enc.encode(JSON.stringify({ r: repos, e: now + SESSION_TTL_SEC })));
|
|
11
|
+
const sig = new Uint8Array(await crypto.subtle.sign("HMAC", await hmacKey(secret, ["sign"]), enc.encode(body)));
|
|
12
|
+
return `${body}.${b64url(sig)}`;
|
|
13
|
+
}
|
|
14
|
+
/** 署名と期限が正しければ、触ってよいリポジトリとインストール ID の組の一覧。そうでなければ null。 */
|
|
15
|
+
export async function verifySession(secret, token, now = Math.floor(Date.now() / 1000)) {
|
|
16
|
+
if (typeof token !== "string" || token.length > 8000)
|
|
17
|
+
return null;
|
|
18
|
+
const [body, sig] = token.split(".");
|
|
19
|
+
if (!body || !sig)
|
|
20
|
+
return null;
|
|
21
|
+
try {
|
|
22
|
+
const ok = await crypto.subtle.verify("HMAC", await hmacKey(secret, ["verify"]), unb64url(sig), enc.encode(body));
|
|
23
|
+
if (!ok)
|
|
24
|
+
return null;
|
|
25
|
+
const d = JSON.parse(new TextDecoder().decode(unb64url(body)));
|
|
26
|
+
if (typeof d.e !== "number" || d.e < now || !Array.isArray(d.r))
|
|
27
|
+
return null;
|
|
28
|
+
return d.r.filter((x) => Array.isArray(x) && typeof x[0] === "string" && typeof x[1] === "number");
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export const MAX_ORIGINS = 10;
|
|
35
|
+
export const DAILY_LIMIT_MAX = 1000;
|
|
36
|
+
/** 「埋め込みを許すサイト」の入力を検証して正規化する。不正なものが 1 つでもあれば null。 */
|
|
37
|
+
export function normalizeOrigins(input) {
|
|
38
|
+
if (!Array.isArray(input) || input.length > MAX_ORIGINS)
|
|
39
|
+
return null;
|
|
40
|
+
const out = [];
|
|
41
|
+
for (const raw of input) {
|
|
42
|
+
if (typeof raw !== "string")
|
|
43
|
+
return null;
|
|
44
|
+
const s = raw.trim();
|
|
45
|
+
if (!s)
|
|
46
|
+
continue;
|
|
47
|
+
let u;
|
|
48
|
+
try {
|
|
49
|
+
u = new URL(s);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
// CSP の frame-ancestors にそのまま入るので、オリジン(スキーム + ホスト + ポート)だけを許す
|
|
55
|
+
if ((u.protocol !== "https:" && u.protocol !== "http:") || u.username || u.password)
|
|
56
|
+
return null;
|
|
57
|
+
if (u.pathname !== "/" || u.search || u.hash || s.replace(/\/$/, "") !== u.origin)
|
|
58
|
+
return null;
|
|
59
|
+
if (!out.includes(u.origin))
|
|
60
|
+
out.push(u.origin);
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
/** 1 日の上限。null は既定値に戻す。 */
|
|
65
|
+
export function normalizeDailyLimit(input) {
|
|
66
|
+
if (input === null)
|
|
67
|
+
return null;
|
|
68
|
+
if (typeof input !== "number" || !Number.isInteger(input) || input < 1 || input > DAILY_LIMIT_MAX)
|
|
69
|
+
return undefined;
|
|
70
|
+
return input;
|
|
71
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@posissue/server",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
"description": "Self-host PosIssue on Cloudflare Workers: a feedback form and API that turns reports into GitHub issues.",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"posissue",
|
|
6
|
+
"github",
|
|
7
|
+
"issues",
|
|
8
|
+
"feedback",
|
|
9
|
+
"cloudflare-workers",
|
|
10
|
+
"github-app"
|
|
11
|
+
],
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/ninjin0802/posissue.git",
|
|
15
|
+
"directory": "packages/server"
|
|
16
|
+
},
|
|
17
|
+
"version": "1.3.0",
|
|
18
|
+
"homepage": "https://posissue.2nj.lol",
|
|
19
|
+
"bugs": "https://github.com/ninjin0802/posissue/issues",
|
|
20
|
+
"author": "ninjin0802",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"type": "module",
|
|
23
|
+
"main": "./dist/index.js",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"import": "./dist/index.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist",
|
|
33
|
+
"README.md",
|
|
34
|
+
"LICENSE"
|
|
35
|
+
],
|
|
36
|
+
"sideEffects": false,
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@posissue/core": "^1.3.0",
|
|
42
|
+
"posissue": "^1.3.0"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json && cp ../../LICENSE .",
|
|
46
|
+
"prepublishOnly": "npm run build"
|
|
47
|
+
}
|
|
48
|
+
}
|