Skip to content

Commit afc28aa

Browse files
committed
refactor(auth): add timeout and libc fixes
- Add AbortSignal.timeout(5000) to PAM authentication request in apps/api/src/api/auth/index.ts - Fetch projects only when authenticated and not on /login in apps/web/src/components/Layout.tsx - Update useProjects to accept options and pass through to useQuery in apps/web/src/hooks/useProjects.ts - Harden PAM Python scripts by loading libc via find_library and setting malloc/strdup restype in scripts/auth/pam-server.py - Symmetrically update pam-verify.py to use find_library for libc and set malloc/strdup restype - Remove premature success return from pam-verify.py to fix flow
1 parent caf25f2 commit afc28aa

5 files changed

Lines changed: 10 additions & 7 deletions

File tree

apps/api/src/api/auth/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const callPam = async (username: string, password: string): Promise<{ ok: boolea
1010
method: "POST",
1111
headers: { "Content-Type": "application/json" },
1212
body: JSON.stringify({ username, password }),
13+
signal: AbortSignal.timeout(5000),
1314
});
1415
const data = await res.json();
1516
return data;

apps/web/src/components/Layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function Layout({ children }: { children: React.ReactNode }) {
3131
}
3232
}, [me, authLoading, location.pathname, navigate]);
3333

34-
const { data: projects = [] } = useProjects();
34+
const { data: projects = [] } = useProjects({ enabled: !!me?.authenticated && location.pathname !== "/login" });
3535
const [projectSelectorOpen, setProjectSelectorOpen] = useState(false);
3636

3737
const { data: metricsText } = useQuery({

apps/web/src/hooks/useProjects.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
22
import { listProjects, createProject, deleteProject } from '../api/client';
33
import type { Project } from '../types';
44

5-
export function useProjects() {
6-
return useQuery({ queryKey: ['projects'], queryFn: listProjects, refetchInterval: 10_000 });
5+
export function useProjects(options?: { enabled?: boolean }) {
6+
return useQuery({ queryKey: ['projects'], queryFn: listProjects, refetchInterval: 10_000, ...options });
77
}
88

99
export function useProject(id: string) {

scripts/auth/pam-server.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@ def verify(username: str, password: str) -> dict:
4141
if not lib_path:
4242
return {"ok": False, "error": "PAM library not found on system"}
4343
libpam = ctypes.cdll.LoadLibrary(lib_path)
44-
libc = ctypes.cdll.LoadLibrary("libc.so.6")
44+
libc = ctypes.cdll.LoadLibrary(ctypes.util.find_library("c") or "libc.so.6")
4545
libpam.pam_start.restype = ctypes.c_int
4646
libpam.pam_authenticate.restype = ctypes.c_int
4747
libpam.pam_acct_mgmt.restype = ctypes.c_int
4848
libpam.pam_end.restype = ctypes.c_int
4949
libpam.pam_strerror.restype = ctypes.c_char_p
5050
libpam.pam_strerror.argtypes = [ctypes.c_void_p, ctypes.c_int]
51+
libc.malloc.restype = ctypes.c_void_p
52+
libc.strdup.restype = ctypes.c_void_p
5153

5254
password_cpy = [password]
5355

scripts/auth/pam-verify.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ def verify(username: str, password: str) -> dict:
4545
if not lib_path:
4646
return {"ok": False, "error": "PAM library not found on system"}
4747
libpam = ctypes.cdll.LoadLibrary(lib_path)
48-
libc = ctypes.cdll.LoadLibrary("libc.so.6")
48+
libc = ctypes.cdll.LoadLibrary(ctypes.util.find_library("c") or "libc.so.6")
4949
libpam.pam_start.restype = ctypes.c_int
5050
libpam.pam_authenticate.restype = ctypes.c_int
5151
libpam.pam_acct_mgmt.restype = ctypes.c_int
5252
libpam.pam_end.restype = ctypes.c_int
5353
libpam.pam_strerror.restype = ctypes.c_char_p
5454
libpam.pam_strerror.argtypes = [ctypes.c_void_p, ctypes.c_int]
55+
libc.malloc.restype = ctypes.c_void_p
56+
libc.strdup.restype = ctypes.c_void_p
5557

5658
password_cpy = [password]
5759

@@ -119,8 +121,6 @@ def conv(nmsg, msg, out_resp, appdata):
119121

120122
return {"ok": False, "error": f"User '{username}' is not in the '{GRP_NAME}' group"}
121123

122-
return {"ok": True, "username": username}
123-
124124

125125
def main():
126126
try:

0 commit comments

Comments
 (0)