425ca29 / push-auth.mjs
diff --git a/push-auth.mjs b/push-auth.mjs
index 1cd9720..8e57089 100644
--- a/push-auth.mjs
+++ b/push-auth.mjs
@@ -1,9 +1,10 @@
-import { spawnSync } from 'node:child_process'
+import { execFileSync, spawnSync } from 'node:child_process'
import { readFileSync } from 'node:fs'
import {
- base64Decode, base64Encode, importAuthorKey, parseJson, requireThat, signBytes,
+ base64Decode, base64Encode, createRecord, importAuthorKey, parseJson, requireThat, signBytes,
utf8Encode, verifyBytes
} from './core.mjs'
+import { verifyPushAnnouncement } from './social.mjs'
const challengeLifetime = 60_000
const tokenLifetime = 5 * 60_000
@@ -12,14 +13,14 @@ const identifier = () => base64Encode(globalThis.crypto.getRandomValues(new Uint
const message = (origin, repoId, nonce) =>
utf8Encode(JSON.stringify(['anbot-git-push-v1', origin, repoId, nonce]))
-function store(map, identity, lifetime) {
+function store(map, identity, lifetime, announcement) {
const now = Date.now()
for (const [key, entry] of map) {
if (entry.expires <= now) map.delete(key)
}
requireThat(map.size < maxEntries, 'Too many pending authentications')
const key = identifier()
- map.set(key, { identity, expires: now + lifetime })
+ map.set(key, { identity, expires: now + lifetime, announcement })
return key
}
@@ -28,7 +29,7 @@ async function readBody(request) {
let bytes = 0
for await (const chunk of request) {
bytes += chunk.length
- requireThat(bytes <= 1024, 'Authentication request too large')
+ requireThat(bytes <= 16384, 'Authentication request too large')
chunks.push(chunk)
}
return parseJson(Buffer.concat(chunks))
@@ -56,7 +57,7 @@ export function pushAuth(allowlist, origin, repoId) {
json(response, 200, { repoId, nonce })
return
}
- const { author, nonce, proof } = await readBody(request)
+ const { author, nonce, proof, announcement } = await readBody(request)
const entry = challenges.get(nonce)
challenges.delete(nonce)
requireThat(entry?.expires > Date.now() && author === entry.identity &&
@@ -68,7 +69,8 @@ export function pushAuth(allowlist, origin, repoId) {
const authorized = allowed(author)
console.log(`ANBot push identity verified: ${author} (${authorized ? 'allowed' : 'not allowlisted'})`)
requireThat(authorized, 'Identity is not allowlisted')
- json(response, 200, { token: store(tokens, author, tokenLifetime) })
+ if (announcement !== undefined) await verifyPushAnnouncement(announcement, author, repoId)
+ json(response, 200, { token: store(tokens, author, tokenLifetime, announcement) })
} catch {
json(response, 403, { error: 'Push access denied' })
}
@@ -76,7 +78,7 @@ export function pushAuth(allowlist, origin, repoId) {
authorized(request) {
const token = /^Bearer ([A-Za-z0-9_-]{43})$/.exec(request.headers.authorization)?.[1]
const entry = tokens.get(token)
- return entry?.expires > Date.now() && allowed(entry.identity)
+ return entry?.expires > Date.now() && allowed(entry.identity) ? entry : null
}
}
}
@@ -94,16 +96,20 @@ export async function pushWithIdentity(identity, remote, source, branch) {
})
requireThat(challenge.ok, 'Push access denied or unavailable for ' + identity.author)
const { repoId, nonce } = await challenge.json()
+ const ref = `refs/heads/${branch}`
+ const commit = execFileSync('git', ['-C', source, 'show-ref', '--verify', '--hash', ref],
+ { encoding: 'utf8' }).trim()
+ const announcement = await createRecord(identity, { anbot: 1, type: 'git-push', repoId, ref, commit })
const proof = base64Encode(await signBytes(identity.privateKey, message(url.origin, repoId, nonce)))
const accepted = await fetch(authUrl, {
...options, method: 'POST', headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ author: identity.author, nonce, proof })
+ body: JSON.stringify({ author: identity.author, nonce, proof, announcement })
})
requireThat(accepted.ok, 'Push access denied for ' + identity.author)
const { token } = await accepted.json()
requireThat(/^[A-Za-z0-9_-]{43}$/.test(token), 'Invalid push token')
const result = spawnSync('git', ['-C', source, 'push', '--', url.href,
- `refs/heads/${branch}:refs/heads/${branch}`], {
+ `${commit}:${ref}`], {
stdio: 'inherit',
env: {
...process.env, GIT_CONFIG_COUNT: '3', GIT_TERMINAL_PROMPT: '0',
Git ยท Social feed