~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0

pds-server: PAR keeps loopback client IDs as long as their parts allow

Merged chadtmiller.com asked to merge into main from chadtmiller.com/monolith:pds-server/loopback-client-id
.changeset/long-loopback-client-ids.mdnew+7−0
@@ -0,0 +1,7 @@
---
default: patch
---
# Loopback clients with long scopes can sign in
PAR keeps a `client_id` of up to 18470 bytes, not 2048: the longest loopback client ID naming a `redirect_uri` of 2048 bytes and a `scope` of 4096, every byte of both percent-encoded. A loopback client's ID carries its whole scope, so clients asking for many permissions, such as the knotless CLI (whose ID is 2857 bytes), were refused with `invalid_request: client_id is too long` although every part of their request was within its own cap.
crates/pds-server/src/oauth/tests.rs+1−1
@@ -2873,7 +2873,7 @@ fn par_field_caps() {
too_large(&Params(pairs))
};
let cases: &[(&str, usize, usize)] = &[
("client_id", 2048, 2049),
("client_id", 18470, 18471),
("state", 1024, 1025),
("redirect_uri", 2048, 2049),
("login_hint", 2048, 2049),
crates/pds-server/src/oauth.rs+6−1
@@ -1691,10 +1691,15 @@ fn par_forms(p: &Params) -> Result<(), String> {
Ok(())
}
/// The longest `client_id` PAR keeps: a loopback one naming a
/// `redirect_uri` of 2048 bytes and a `scope` of 4096, every byte of both
/// percent-encoded (spec/OAUTH.md, PAR step 17).
const CLIENT_ID_CAP: usize = "http://localhost/?redirect_uri=&scope=".len() + 3 * (2048 + 4096);
/// PAR's step 17: why a request is larger than the server keeps, if it is.
fn too_large(p: &Params) -> Option<&'static str> {
let text = |k: &str| p.text(k).ok().flatten().unwrap_or_default();
if text("client_id").len() > 2048 {
if text("client_id").len() > CLIENT_ID_CAP {
return Some("client_id is too long");
}
if text("state").len() > 1024 {
harness/checks/oauth.mjs+40−6
@@ -700,8 +700,47 @@ const PAR_FAULTS = [
// ignored parameter pads the form to exactly the size.
['a body one byte past 100 KiB', (p) => padTo(p, 102401)],
['a body of exactly 100 KiB', (p) => padTo(p, 102400)],
// Long loopback client IDs, each part within its own cap, kept
// (spec/OAUTH.md, "Pushed authorization requests", step 17).
['a loopback client_id whose scope is near 4096 bytes', (p) => {
const scope = scopeOf(4096 - below(64))
Object.assign(p, { client_id: `http://localhost?${new URLSearchParams({ redirect_uri: 'http://127.0.0.1/', scope })}`, redirect_uri: 'http://127.0.0.1/', scope: 'atproto' })
}],
['a loopback client_id of 18470 bytes', (p) => Object.assign(p, { client_id: loopbackOf(CLIENT_ID_CAP), redirect_uri: 'http://127.0.0.1/', scope: 'atproto' })],
['knotless\'s loopback client_id', (p) => Object.assign(p, { client_id: KNOTLESS_ID, redirect_uri: `http://127.0.0.1:${1024 + below(60000)}/callback`, scope: new URL(KNOTLESS_ID).searchParams.get('scope') })],
]
/// The longest loopback `client_id` kept (spec/OAUTH.md, "Pushed
/// authorization requests", step 17).
const CLIENT_ID_CAP = 18470
/// A scope of exactly `n` bytes: `atproto` and distinct `rpc` permissions.
function scopeOf(n) {
const tok = (i, pad = '') => `rpc:com.example.m${i}${pad}?aud=*`
const toks = ['atproto']
while (toks.join(' ').length + 1 + tok(toks.length).length <= n) toks.push(tok(toks.length))
const short = n - toks.join(' ').length
if (short > 0) toks[toks.length - 1] = tok(toks.length - 1, 'x'.repeat(short))
return toks.join(' ')
}
/// Every byte of `s` percent-encoded.
const encodeAll = (s) => [...Buffer.from(s)].map((b) => `%${b.toString(16).toUpperCase().padStart(2, '0')}`).join('')
/// A loopback `client_id` of exactly `n` bytes: a scope of 4096 bytes and
/// `http://127.0.0.1/`, every byte percent-encoded, then a second redirect
/// URI as long as it takes.
function loopbackOf(n) {
const head = `http://localhost/?scope=${encodeAll(scopeOf(4096))}&redirect_uri=${encodeAll('http://127.0.0.1/')}&redirect_uri=${encodeAll('http://127.0.0.1/')}`
const rest = n - head.length
const id = `${head}${encodeAll('x'.repeat(Math.floor(rest / 3)))}${'y'.repeat(rest % 3)}`
if (id.length !== n) throw new Error(`loopbackOf(${n}) is ${id.length} bytes`)
return id
}
/// The knotless CLI's loopback `client_id`, 2857 bytes.
const KNOTLESS_ID = 'http://localhost?redirect_uri=http%3A%2F%2F127.0.0.1%2Fcallback&scope=atproto+repo%3Adev.knotless.repo+repo%3Adev.knotless.ref+repo%3Adev.knotless.claim+repo%3Adev.knotless.pack+repo%3Adev.knotless.redirect+repo%3Adev.knotless.pull+repo%3Adev.knotless.issue+repo%3Adev.knotless.comment+repo%3Adev.knotless.state+repo%3Adev.knotless.hide+repo%3Adev.knotless.block+repo%3Adev.knotless.queue+repo%3Adev.knotless.label+repo%3Adev.knotless.assignment+repo%3Adev.knotless.autoMerge+repo%3Adev.knotless.emails+repo%3Adev.knotless.profile+repo%3Adev.knotless.star+repo%3Adev.knotless.follow+repo%3Adev.knotless.org+repo%3Adev.knotless.orgMember+repo%3Adev.knotless.orgAcceptance+repo%3Adev.knotless.reaction+repo%3Adev.knotless.rerun+repo%3Adev.knotless.cancel+repo%3Adev.knotless.release+repo%3Adev.knotless.releaseYank+repo%3Adev.knotless.theme+repo%3Adev.knotless.check+repo%3Adev.knotless.minutes+blob%3Aapplication%2Fx-git-packed-objects+blob%3Aapplication%2Foctet-stream+blob%3Atext%2Fplain+blob%3Aimage%2Fpng+blob%3Aimage%2Fjpeg+blob%3Aimage%2Fwebp+blob%3Aimage%2Fgif+rpc%3Flxm%3Ddev.knotless.pushRefs%26lxm%3Ddev.knotless.createCommit%26lxm%3Ddev.knotless.putDelegation%26lxm%3Ddev.knotless.getDelegation%26lxm%3Ddev.knotless.putOrgMember%26lxm%3Ddev.knotless.removeOrgMember%26lxm%3Ddev.knotless.joinOrg%26lxm%3Ddev.knotless.leaveOrg%26lxm%3Ddev.knotless.createOrgRepo%26lxm%3Ddev.knotless.getOrg%26lxm%3Ddev.knotless.getProfile%26lxm%3Dcom.atproto.moderation.createReport%26lxm%3Dcom.atproto.admin.updateSubjectStatus%26lxm%3Dcom.atproto.admin.getSubjectStatus%26lxm%3Ddev.knotless.listReports%26lxm%3Ddev.knotless.getUsage%26lxm%3Ddev.knotless.getLogs%26lxm%3Ddev.knotless.inviteRunner%26lxm%3Ddev.knotless.listRunnerInvites%26lxm%3Ddev.knotless.putRunOutput%26lxm%3Ddev.knotless.awaitSpaceWrites%26lxm%3Ddev.knotless.putSecret%26lxm%3Ddev.knotless.deleteSecret%26lxm%3Ddev.knotless.listSecrets%26lxm%3Ddev.knotless.updateRepo%26lxm%3Ddev.knotless.compactRepo%26lxm%3Ddev.knotless.archiveRepo%26lxm%3Ddev.knotless.protectBranch%26lxm%3Ddev.knotless.unprotectBranch%26lxm%3Ddev.knotless.defineLabel%26lxm%3Ddev.knotless.removeLabel%26lxm%3Ddev.knotless.shareRepo%26lxm%3Ddev.knotless.unshareRepo%26lxm%3Ddev.knotless.setTriager%26lxm%3Ddev.knotless.removeTriager%26lxm%3Ddev.knotless.addAgent%26lxm%3Ddev.knotless.removeAgent%26lxm%3Ddev.knotless.addRunner%26lxm%3Ddev.knotless.removeRunner%26lxm%3Ddev.knotless.listNotifications%26lxm%3Ddev.knotless.updateSeen%26lxm%3Ddev.knotless.getPreferences%26lxm%3Ddev.knotless.putPreferences%26lxm%3Ddev.knotless.putSubscription%26lxm%3Ddev.knotless.listSubscriptions%26lxm%3Ddev.knotless.deleteAccountData%26aud%3D*+space%3Adev.knotless.space%3Fauthority%3D*%26manage%3Dcreate%26manage%3Dupdate%26manage%3Ddelete+space%3Adev.knotless.orgSpace%3Fauthority%3D*%26manage%3Dcreate%26manage%3Dupdate%26manage%3Ddelete'
/// One PAR with one thing wrong, on both sides with a fresh nonce.
export async function oauthParRefused() {
const [name, change] = pick(own() ? PAR_FAULTS : PAR_FAULTS.filter(([n]) => !n.includes('100 KiB')))
@@ -1181,12 +1220,7 @@ const PAR_CAPS = {
'a code_challenge of 42 characters': [(p) => ({ code_challenge: p.code_challenge.slice(1) }), 400],
'ui_locales of 16 tags': [() => ({ ui_locales: Array(16).fill('en').join(' ') }), 201],
'ui_locales of 17 tags': [() => ({ ui_locales: Array(17).fill('en').join(' ') }), 400],
'a client_id over 2048 bytes': [() => {
// A loopback client of many distinct permissions, each declared once.
let id = 'http://localhost?redirect_uri=http%3A%2F%2F127.0.0.1%2F&scope=atproto'
for (let i = 0; id.length <= 2048; i++) id += `%20rpc%3Acom.example.m${i}%3Faud%3D*`
return { client_id: id, redirect_uri: 'http://127.0.0.1/', scope: 'atproto' }
}, 400],
'a loopback client_id of 18471 bytes': [() => ({ client_id: loopbackOf(CLIENT_ID_CAP + 1), redirect_uri: 'http://127.0.0.1/', scope: 'atproto' }), 400],
}
async function held(which, request, want, got) {
spec/OAUTH.md+12−2
@@ -321,13 +321,21 @@ The request is checked in this order, and the first failure answers, all
16. `state` missing or empty, or the same as one this client gave in the
last 24 hours: `invalid_request`.
17. A request larger than the server keeps: `invalid_request`. That is a
`client_id` longer than 2048 bytes, a `state` longer than 1024, a
`client_id` longer than 18470 bytes, a `state` longer than 1024, a
`code_challenge` that is not 43 characters (an S256 challenge always
is, so no verifier matches another), a `redirect_uri` or `login_hint`
longer than 2048, a `scope` longer than 4096, or `ui_locales` of more
than 16 tags. The reference keeps them whatever their length, up to its
body's 100 KiB (probed: a `state` of 100000 bytes and a
`code_challenge` of 10000 are kept).
*Note:* 18470 is the longest loopback `client_id` naming one
`redirect_uri` of 2048 bytes and a `scope` of 4096, every byte of both
percent-encoded: `http://localhost/?redirect_uri=` (31 bytes),
`&scope=` (7) and three bytes for each of their 6144. A loopback
client's ID carries its scope and redirect URIs, so a cap below that
refuses a client whose every part is within its own cap (knotless's
CLI's ID, for one, is 2857 bytes).
18. Requests already kept and pending (not yet approved, ended or past
their time) from the client address (`LIMITS.md`, "The client address";
an IPv6 address by its first 64 bits): 1000 of them, and this one is
@@ -868,7 +876,9 @@ reference's own values on the reference (3599 to 3600, 299 to 300). The
departures above are checked on the server alone: PAR's caps with a
`state` of 1024 bytes (kept) and 1025, a `code_challenge` of 42 and 44
characters, `ui_locales` of 16 tags (kept) and 17, and a loopback
`client_id` past 2048 bytes. `harness/checks/limits.mjs` times the
`client_id` of 18471 bytes. A loopback `client_id` of exactly 18470
bytes, one whose `scope` is near 4096 bytes, and knotless's CLI's own are
kept on both sides (compared, among PAR's refusals). `harness/checks/limits.mjs` times the
sign-in in every run, and its `--limits` run holds the server alone to
the sign-in's limit, PAR's 1000 pending requests from one address, and
the free space (`LIMITS.md`, "How it is checked").