앱 로그인: 늦게 도착한 이전 확인 응답이 잠금을 풀지 못하게

- 확인 요청마다 세대 번호를 붙이고, 새 확인 · 잠금(등록 해제) · 로그아웃 · 새 로그인 뒤에 도착한
  이전 응답(성공 · 실패 모두)은 버린다. 보낸 토큰과 지금 토큰이 다르면 역시 버린다
- 로그아웃은 서버 응답을 기다리기 전에 로컬에서 먼저 지우고 잠근다
- 회귀 테스트: 등록 해제 뒤 / 로그아웃 뒤 늦은 200 (이전 코드에서 둘 다 실패 확인)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
EJClaw
2026-10-15 08:23:48 +09:00
parent c2a5547a68
commit a2487e6241
2 changed files with 94 additions and 2 deletions

View File

@@ -154,3 +154,81 @@ test('관리자 전용 설정: 일반 유저가 바꾸면 안 되는 경로만
assert.deepEqual(adminOnlyPaths({ finetune: { schedule: 'nightly' } }), ['finetune']);
assert.deepEqual(adminOnlyPaths({ firstRunDone: true, general: { sfx: false } }), []);
});
/** /app/me 응답을 서버가 처리한 뒤 앱에 늦게 도착하게 붙잡는다 (hold() 로 다음 요청 하나를 잡고, 반환값으로 놓아준다) */
function holdMe() {
const real = globalThis.fetch;
let gate: { promise: Promise<void>; release: () => void } | null = null;
globalThis.fetch = (async (input: any, init?: any) => {
const r = await real(input, init);
if (gate && String(input).endsWith('/app/me')) {
const g = gate;
gate = null;
await g.promise;
}
return r;
}) as typeof fetch;
return {
hold: () => {
let release!: () => void;
const promise = new Promise<void>((r) => (release = r));
gate = { promise, release };
return release;
},
restore: () => (globalThis.fetch = real),
};
}
test('늦은 확인 응답: 등록 해제로 잠긴 뒤 이전 확인의 200 이 와도 다시 열리지 않는다', async () => {
const s = await stack();
const h = holdMe();
try {
const admin = new AppAuth(s.settingsFor(), { version: 't', openUrl: s.browser(SUPER), pollMs: 20 });
await admin.login();
await admin.adminAddUser(FRIEND, 'user');
const st = s.settingsFor();
const fr = new AppAuth(st, { version: 't', openUrl: s.browser(FRIEND), pollMs: 20 });
assert.equal((await fr.login()).status, 'ok');
// A: 서버는 200 을 줬지만 응답이 늦게 온다
const release = h.hold();
const a = fr.check();
await new Promise((r) => setTimeout(r, 100));
// 그 사이 등록 해제 → B: 403 → 잠김
await admin.adminRemoveUser(FRIEND);
assert.equal((await fr.check()).status, 'not_registered');
assert.equal(st.activeBotToken(), null);
// A 의 늦은 200 도착
release();
await a;
assert.equal(fr.state.status, 'not_registered', '늦은 200 이 잠금을 풀면 안 된다');
assert.equal(st.activeBotToken(), null, '지운 봇 토큰이 되살아나면 안 된다');
assert.equal(st.hasSecret('appToken'), false);
} finally {
h.restore();
await s.close();
}
});
test('늦은 확인 응답: 로그아웃 뒤 이전 확인의 200 이 와도 다시 열리지 않는다', async () => {
const s = await stack();
const h = holdMe();
try {
const st = s.settingsFor();
const auth = new AppAuth(st, { version: 't', openUrl: s.browser(SUPER), pollMs: 20 });
assert.equal((await auth.login()).status, 'ok');
const release = h.hold();
const a = auth.check();
await new Promise((r) => setTimeout(r, 100));
await auth.logout();
assert.equal(auth.state.status, 'signed_out');
release();
await a;
assert.equal(auth.state.status, 'signed_out', '늦은 200 이 로그아웃을 되돌리면 안 된다');
assert.equal(st.activeBotToken(), null);
assert.equal(st.hasSecret('auth'), false);
} finally {
h.restore();
await s.close();
}
});