From 972710509bfcd96970052617acc6275045346d53 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Wed, 18 Nov 2026 00:03:12 +0000 Subject: [PATCH] Find the unnamed Saldeo username field on the current login form. The Angular login input has no name or formcontrolname (those sit on ds-input) and an unstable mat-input id, so the old selectors never matched it. Use the visible text field in the password form, including qa-id="username", and accept the English "Log in" button. Credentials still come only from the 0600 login file. Co-authored-by: wydrox --- README.md | 2 +- scripts/saldeo-login.js | 255 +++++++++++++++++++++++++++++++---- scripts/test_saldeo_login.py | 139 +++++++++++++++++++ src/credentials_tests.rs | 15 +++ 4 files changed, 384 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 262949a..dbab7d7 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ Sam test (bez kreatora): jdg-cli onboard --check ``` -Pobranie auth do Saldeo przez Playwright: przy wygasłej sesji jdg-cli samo loguje zapisanym `SALDEO_USERNAME`/`SALDEO_PASSWORD` w Helium i czeka na formularz po starcie SPA. Używa Helium Browser (`/Applications/Helium.app/Contents/MacOS/Helium`); można nadpisać `HELIUM_EXECUTABLE`. Timeout: `SALDEO_AUTH_TIMEOUT_MS` (domyślnie 180000). Headless tylko przy `SALDEO_AUTH_HEADLESS=1`. 2FA i Captcha nadal wymagają Ciebie. Skrypt `scripts/saldeo-auth.sh` jdg-cli uruchamia tylko z zaufanego miejsca: ścieżka bezwzględna w `SALDEO_AUTH_SCRIPT`, katalogi nad plikiem `jdg-cli` (po rozwinięciu dowiązań) albo bieżący katalog, gdy jest pakietem `jdg-cli` — nigdy z katalogów nadrzędnych bieżącego katalogu. Skrypt musi należeć do Ciebie i nie może być zapisywalny dla grupy ani innych; przed uruchomieniem `jdg-cli onboard` pokazuje jego pełną ścieżkę. Hasło Saldeo idzie do skryptu plikiem 600 (`JDG_SALDEO_LOGIN_FILE`). Przy `JDG_NONINTERACTIVE=1` jdg-cli nie otwiera Helium i nie instaluje Playwright: wygasła sesja kończy się błędem „Saldeo session expired”. Tymczasowy profil przeglądarki jest usuwany po każdym zakończeniu logowania. +Pobranie auth do Saldeo przez Playwright: przy wygasłej sesji jdg-cli samo loguje zapisanym `SALDEO_USERNAME`/`SALDEO_PASSWORD` w Helium i czeka na formularz po starcie SPA. Pole loginu to widoczne pole tekstowe w tym samym formularzu co hasło, także gdy nie ma atrybutu `name` (aktualny panel Saldeo); przycisk „Log in” i „Zaloguj” są rozpoznawane. Używa Helium Browser (`/Applications/Helium.app/Contents/MacOS/Helium`); można nadpisać `HELIUM_EXECUTABLE`. Timeout: `SALDEO_AUTH_TIMEOUT_MS` (domyślnie 180000). Headless tylko przy `SALDEO_AUTH_HEADLESS=1`. 2FA i Captcha nadal wymagają Ciebie. Skrypt `scripts/saldeo-auth.sh` jdg-cli uruchamia tylko z zaufanego miejsca: ścieżka bezwzględna w `SALDEO_AUTH_SCRIPT`, katalogi nad plikiem `jdg-cli` (po rozwinięciu dowiązań) albo bieżący katalog, gdy jest pakietem `jdg-cli` — nigdy z katalogów nadrzędnych bieżącego katalogu. Skrypt musi należeć do Ciebie i nie może być zapisywalny dla grupy ani innych; przed uruchomieniem `jdg-cli onboard` pokazuje jego pełną ścieżkę. Hasło Saldeo idzie do skryptu plikiem 600 (`JDG_SALDEO_LOGIN_FILE`). Przy `JDG_NONINTERACTIVE=1` jdg-cli nie otwiera Helium i nie instaluje Playwright: wygasła sesja kończy się błędem „Saldeo session expired”. Tymczasowy profil przeglądarki jest usuwany po każdym zakończeniu logowania. ```bash ./scripts/saldeo-auth.sh diff --git a/scripts/saldeo-login.js b/scripts/saldeo-login.js index 71ebf0b..88e253b 100644 --- a/scripts/saldeo-login.js +++ b/scripts/saldeo-login.js @@ -5,17 +5,6 @@ const fs = require("fs"); const os = require("os"); const path = require("path"); -const USER_SELECTORS = [ - 'input[name="username"]', - 'input[name="login"]', - 'input[name="j_username"]', - 'input[formcontrolname="username"]', - 'input[formcontrolname="login"]', - "input#username", - "input#login", - 'input[type="email"]', - 'input[autocomplete="username"]', -]; const PASS_SELECTORS = [ 'input[type="password"]', 'input[name="password"]', @@ -23,13 +12,9 @@ const PASS_SELECTORS = [ 'input[formcontrolname="password"]', 'input[autocomplete="current-password"]', ]; -const SUBMIT_SELECTORS = [ - 'button[type="submit"]', - 'input[type="submit"]', - 'button:has-text("Zaloguj")', - 'button:has-text("Zaloguj się")', - 'button:has-text("Login")', -]; +// Etykiety przycisku logowania. Aktualny panel Saldeo jest po angielsku („Log in”); +// starszy widok używa „Zaloguj” / „Zaloguj się”. +const SUBMIT_LABELS = ["Zaloguj", "Zaloguj się", "Log in", "Login"]; function sleep(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); @@ -259,18 +244,229 @@ async function waitFirstLocator(page, selectors, timeoutMs) { return firstLocator(page, selectors); } +function norm(value) { + return String(value || "").trim().toLowerCase(); +} + +function normText(value) { + return String(value || "").replace(/\s+/g, " ").trim().toLowerCase(); +} + +function isPasswordControl(control) { + if (!control || control.tag !== "input") { + return false; + } + const type = norm(control.type); + const name = norm(control.name); + const formControl = norm(control.formControlName) || norm(control.hostFormControlName); + const qaId = norm(control.qaId); + return ( + type === "password" || + name === "password" || + name === "j_password" || + formControl === "password" || + norm(control.autocomplete) === "current-password" || + qaId === "password" + ); +} + +// Wyższy wynik wygrywa. 0 = to nie jest pole loginu. +// Panel Saldeo (ds-input) trzyma formcontrolname na hoście, a na natywnym inpucie +// zostawia qa-id="username", type=text i autocomplete=off. Id mat-input-N pomijamy. +function usernameRank(control) { + if (!control || control.tag !== "input" || isPasswordControl(control)) { + return 0; + } + const type = norm(control.type); + if (["hidden", "checkbox", "radio", "submit", "button", "file", "image", "search"].includes(type)) { + return 0; + } + const name = norm(control.name); + const id = norm(control.id); + const formControl = norm(control.formControlName) || norm(control.hostFormControlName); + const qaId = norm(control.qaId); + if ( + name === "username" || + name === "login" || + name === "j_username" || + id === "username" || + id === "login" || + formControl === "username" || + formControl === "login" || + formControl === "j_username" || + qaId === "username" || + qaId === "login" + ) { + return 3; + } + if (norm(control.autocomplete) === "username") { + return 2; + } + if (type === "email") { + return 1; + } + return 0; +} + +function isTextLike(control) { + if (!control || control.tag !== "input" || isPasswordControl(control)) { + return false; + } + const type = norm(control.type); + return type === "text" || type === "email" || type === ""; +} + +function isSubmitControl(control) { + if (!control || (control.tag !== "button" && control.tag !== "input")) { + return false; + } + const type = norm(control.type); + if (type === "submit" || norm(control.qaId) === "login-button") { + return true; + } + const text = normText(control.text); + if (SUBMIT_LABELS.some((label) => normText(label) === text)) { + return true; + } + return text.includes("zaloguj"); +} + +function visibleControl(control) { + return Boolean(control) && control.visible !== false; +} + +// Wybiera login, hasło i przycisk z opisu kontrolek (bez sekretów). +// Gdy pole loginu nie ma name/id, bierzemy widoczny input tekstowy w formularzu hasła, +// a gdy formularza nie ma — najbliższe takie pole przed hasłem. +function pickLoginControls(controls) { + const list = Array.isArray(controls) ? controls : []; + const pass = list.find((control) => visibleControl(control) && isPasswordControl(control)) || null; + let user = null; + let bestRank = 0; + for (const control of list) { + if (!visibleControl(control)) { + continue; + } + const rank = usernameRank(control); + if (rank > bestRank) { + user = control; + bestRank = rank; + } + } + if (!user && pass) { + user = + list.find( + (control) => + visibleControl(control) && + control.inPasswordForm && + isTextLike(control) && + control.index !== pass.index + ) || null; + if (!user) { + const before = list.filter( + (control) => visibleControl(control) && isTextLike(control) && control.index < pass.index + ); + user = before.length ? before[before.length - 1] : null; + } + } + const submits = list.filter((control) => visibleControl(control) && isSubmitControl(control)); + const inPasswordForm = submits.filter((control) => control.inPasswordForm); + const pool = inPasswordForm.length ? inPasswordForm : submits; + const submit = + pool.find((control) => norm(control.qaId) === "login-button") || pool[0] || null; + return { + user: user ? user.index : null, + pass: pass ? pass.index : null, + submit: submit ? submit.index : null, + }; +} + +// Zbiera inputy i przyciski w przeglądarce. Musi być samodzielna: Playwright +// serializuje tylko tę funkcję, bez domknięć z Node. +function collectLoginControls() { + const nodes = Array.from(document.querySelectorAll("input, button")); + return nodes.map((el, index) => { + const form = el.closest("form"); + const host = el.closest("ds-input"); + const tag = el.tagName.toLowerCase(); + const attrType = el.getAttribute("type"); + const type = (attrType || (tag === "input" || tag === "button" ? el.type : "") || "").toLowerCase(); + const style = window.getComputedStyle(el); + const visible = + style.display !== "none" && style.visibility !== "hidden" && el.getClientRects().length > 0; + return { + index, + tag, + type, + name: el.getAttribute("name") || "", + id: el.id || "", + formControlName: el.getAttribute("formcontrolname") || "", + hostFormControlName: host ? host.getAttribute("formcontrolname") || "" : "", + autocomplete: el.getAttribute("autocomplete") || "", + qaId: el.getAttribute("qa-id") || "", + text: (el.innerText || el.textContent || "").replace(/\s+/g, " ").trim(), + inPasswordForm: Boolean(form && form.querySelector('input[type="password"]')), + visible, + }; + }); +} + +async function readLoginControls(page) { + try { + const controls = await page.evaluate(collectLoginControls); + return Array.isArray(controls) ? controls : []; + } catch (_) { + return []; + } +} + +async function markLoginTargets(page, picked) { + try { + return await page.evaluate((target) => { + const nodes = Array.from(document.querySelectorAll("input, button")); + for (const el of document.querySelectorAll("[data-jdg-login]")) { + el.removeAttribute("data-jdg-login"); + } + const mark = (index, role) => { + if (typeof index !== "number" || index < 0) { + return false; + } + const el = nodes[index]; + if (!el) { + return false; + } + el.setAttribute("data-jdg-login", role); + return true; + }; + if (!mark(target.user, "user") || !mark(target.pass, "pass")) { + return false; + } + if (typeof target.submit === "number") { + mark(target.submit, "submit"); + } + return true; + }, picked); + } catch (_) { + return false; + } +} + async function fillLogin(page, username, password) { - const user = await firstLocator(page, USER_SELECTORS); - const pass = await firstLocator(page, PASS_SELECTORS); - if (!user || !pass) { + const picked = pickLoginControls(await readLoginControls(page)); + if (picked.user == null || picked.pass == null) { return false; } + if (!(await markLoginTargets(page, picked))) { + return false; + } + const user = page.locator('[data-jdg-login="user"]').first(); + const pass = page.locator('[data-jdg-login="pass"]').first(); await user.click(); await user.fill(username); await pass.click(); await pass.fill(password); - const submit = await firstLocator(page, SUBMIT_SELECTORS); - if (submit) { + const submit = page.locator('[data-jdg-login="submit"]').first(); + if ((await submit.count()) > 0) { await submit.click(); } else { await pass.press("Enter"); @@ -283,8 +479,14 @@ async function waitAndFillLogin(page, username, password, timeoutMs) { if (!pass) { return false; } - await waitFirstLocator(page, USER_SELECTORS, 5000); - return fillLogin(page, username, password); + const deadline = Date.now() + 5000; + do { + if (await fillLogin(page, username, password)) { + return true; + } + await sleep(200); + } while (Date.now() < deadline); + return false; } async function storageAuthenticated(context) { @@ -473,6 +675,7 @@ module.exports = { removeTempProfile, sweepStaleTempProfiles, shutdown, - USER_SELECTORS, + pickLoginControls, + collectLoginControls, PASS_SELECTORS, }; diff --git a/scripts/test_saldeo_login.py b/scripts/test_saldeo_login.py index e41aec0..532cb66 100644 --- a/scripts/test_saldeo_login.py +++ b/scripts/test_saldeo_login.py @@ -87,6 +87,18 @@ def test_password_login_waits_for_spa_and_is_headed_by_default(self): self.assertNotIn("Boolean(login)", text) self.assertIn("waitForLoadState", text) + def test_script_finds_unnamed_username_without_mat_input_ids(self): + text = SCRIPT.read_text() + # Current Saldeo login: formcontrolname lives on , the native + # input is type=text with qa-id="username" (mat-input-N ids are unstable). + self.assertIn("inPasswordForm", text) + self.assertIn('getAttribute("qa-id")', text) + self.assertIn("Log in", text) + self.assertIn("Zaloguj", text) + self.assertIn("data-jdg-login", text) + self.assertNotIn("mat-input-0", text) + self.assertNotIn("SALDEO_PASSWORD", text) + class SaldeoLoginPureTests(unittest.TestCase): def node_json(self, expr): @@ -143,6 +155,133 @@ def test_sweep_removes_only_stale_jdg_profiles(self): self.assertTrue(fresh.exists()) self.assertTrue(unrelated.exists()) + def test_pick_login_controls_on_current_saldeo_form(self): + """The live panel (probed 2026-11-17) has an unnamed text input, a password, + a visibility toggle, and a submit button labelled 'Log in'. A decoy text + input outside that form must not be filled.""" + cases = r""" +(() => { +const field = (extra) => Object.assign({ + tag: "input", type: "text", name: "", id: "", formControlName: "", + hostFormControlName: "", autocomplete: "off", qaId: "", text: "", + inPasswordForm: false, visible: true, +}, extra); +const current = [ + field({ index: 0, name: "search", id: "q" }), + field({ index: 1, id: "mat-input-0", hostFormControlName: "username", qaId: "username", inPasswordForm: true }), + field({ index: 2, type: "password", id: "mat-input-1", hostFormControlName: "password", qaId: "password", inPasswordForm: true }), + field({ index: 3, tag: "button", type: "button", text: "visibility", qaId: "password-trailing-icon", inPasswordForm: true }), + field({ index: 4, tag: "button", type: "submit", text: "Log in", qaId: "login-button", inPasswordForm: true }), +]; +const unnamed = current.map((control, index) => index === 1 + ? field({ index, id: "mat-input-0", inPasswordForm: true }) + : control); +const noForm = unnamed.map((control) => Object.assign({}, control, { inPasswordForm: false })); +const legacy = [ + field({ index: 0, name: "username", id: "login" }), + field({ index: 1, type: "password", name: "password" }), + field({ index: 2, tag: "button", type: "submit", text: "Zaloguj się" }), +]; +const polishButton = [ + field({ index: 0, inPasswordForm: true }), + field({ index: 1, type: "password", inPasswordForm: true }), + field({ index: 2, tag: "button", type: "button", text: "visibility", inPasswordForm: true }), + field({ index: 3, tag: "button", type: "button", text: "Zaloguj się", inPasswordForm: true }), +]; +const hiddenFirst = [ + field({ index: 0, type: "hidden", inPasswordForm: true, visible: false }), + field({ index: 1, inPasswordForm: true }), + field({ index: 2, type: "password", inPasswordForm: true }), +]; +const email = [ + field({ index: 0, type: "email", autocomplete: "username", inPasswordForm: true }), + field({ index: 1, type: "password", autocomplete: "current-password", inPasswordForm: true }), +]; +const earlierSubmit = [ + field({ index: 0, tag: "button", type: "submit", text: "Search" }), + field({ index: 1, qaId: "username", inPasswordForm: true }), + field({ index: 2, type: "password", inPasswordForm: true }), + field({ index: 3, tag: "button", type: "button", text: "visibility", inPasswordForm: true }), + field({ index: 4, tag: "button", type: "submit", text: "Log in", qaId: "login-button", inPasswordForm: true }), +]; +const picked = (controls) => m.pickLoginControls(controls); +return { + current: picked(current), + unnamed: picked(unnamed), + noForm: picked(noForm), + legacy: picked(legacy), + polishButton: picked(polishButton), + hiddenFirst: picked(hiddenFirst), + email: picked(email), + earlierSubmit: picked(earlierSubmit), +}; +})() +""" + got = self.node_json(cases) + for name in ("current", "unnamed", "noForm"): + self.assertEqual(got[name], {"user": 1, "pass": 2, "submit": 4}, name) + self.assertEqual(got["legacy"], {"user": 0, "pass": 1, "submit": 2}) + self.assertEqual(got["polishButton"], {"user": 0, "pass": 1, "submit": 3}) + self.assertEqual(got["hiddenFirst"], {"user": 1, "pass": 2, "submit": None}) + self.assertEqual(got["email"], {"user": 0, "pass": 1, "submit": None}) + self.assertEqual(got["earlierSubmit"], {"user": 1, "pass": 2, "submit": 4}) + + def test_fill_login_types_saved_credentials_into_unnamed_field(self): + password = "tajne-haslo" + js = r""" +const m = require(process.argv[1]); +const controls = [ + { index: 0, tag: "input", type: "text", name: "search", id: "q", formControlName: "", hostFormControlName: "", autocomplete: "off", qaId: "", text: "", inPasswordForm: false, visible: true }, + { index: 1, tag: "input", type: "text", name: "", id: "mat-input-0", formControlName: "", hostFormControlName: "", autocomplete: "off", qaId: "", text: "", inPasswordForm: true, visible: true }, + { index: 2, tag: "input", type: "password", name: "", id: "mat-input-1", formControlName: "", hostFormControlName: "password", autocomplete: "off", qaId: "password", text: "", inPasswordForm: true, visible: true }, + { index: 3, tag: "button", type: "button", name: "", id: "", formControlName: "", hostFormControlName: "password", autocomplete: "", qaId: "password-trailing-icon", text: "visibility", inPasswordForm: true, visible: true }, + { index: 4, tag: "button", type: "submit", name: "", id: "", formControlName: "", hostFormControlName: "", autocomplete: "", qaId: "login-button", text: "Log in", inPasswordForm: true, visible: true }, +]; +const seen = []; +const fills = []; +const clicks = []; +const presses = []; +const page = { + async evaluate(_fn, arg) { + seen.push(arg === undefined ? null : arg); + return arg === undefined ? controls : true; + }, + locator(selector) { + const role = (selector.match(/data-jdg-login="([^"]+)"/) || [])[1] || selector; + const loc = { + first() { return loc; }, + async count() { + return role === "submit" && !(seen[1] && seen[1].submit != null) ? 0 : 1; + }, + async click() { clicks.push(role); }, + async fill(value) { fills.push([role, value]); }, + async press(key) { presses.push([role, key]); }, + }; + return loc; + }, +}; +m.fillLogin(page, "jan", process.argv[2]).then((ok) => { + process.stdout.write(JSON.stringify({ ok, seen, fills, clicks, presses })); +}).catch((err) => { + console.error(err && err.stack ? err.stack : String(err)); + process.exit(1); +}); +""" + result = subprocess.run( + ["node", "-e", js, str(SCRIPT), password], + capture_output=True, + text=True, + ) + self.assertEqual(result.returncode, 0, result.stderr) + got = json.loads(result.stdout) + self.assertTrue(got["ok"]) + self.assertEqual(got["seen"][1], {"user": 1, "pass": 2, "submit": 4}) + self.assertEqual(got["fills"], [["user", "jan"], ["pass", password]]) + self.assertIn("submit", got["clicks"]) + self.assertNotIn("password-trailing-icon", got["clicks"]) + self.assertNotIn(password, json.dumps(got["seen"])) + self.assertNotIn(password, result.stderr) + class SaldeoLoginTempProfileTests(unittest.TestCase): """Runs the script as `jdg-cli` does, with a stub playwright, and checks that the diff --git a/src/credentials_tests.rs b/src/credentials_tests.rs index a8dcfab..1371ebf 100644 --- a/src/credentials_tests.rs +++ b/src/credentials_tests.rs @@ -530,6 +530,21 @@ fn saldeo_login_script_reads_file_not_argv() { } } +#[test] +fn saldeo_login_script_finds_unnamed_username_in_password_form() { + let script = include_str!("../scripts/saldeo-login.js"); + // Natywny input loginu nie ma name/formcontrolname; id mat-input-N nie jest stabilne. + assert!(script.contains("inPasswordForm")); + assert!(script.contains("qa-id")); + assert!(script.contains("Log in")); + assert!(script.contains("Zaloguj")); + assert!(script.contains("data-jdg-login")); + assert!(!script.contains("mat-input-0")); + assert!(!script.contains("process.argv[")); + assert!(!script.contains("process.env.SALDEO_PASSWORD")); + assert!(script.contains("JDG_SALDEO_LOGIN_FILE")); +} + #[test] fn saldeo_auth_uses_local_playwright_not_npx_tmp() { let source = include_str!("onboard.rs");