mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-26 06:37:33 +00:00
fix(docsync): an idle WebDAV binding read as an emptied folder, and two ids nobody checked
The audit's worst find, reproduced against a live Nextcloud before and after the
fix: the WebDAV adapters answer "nothing changed here" by returning an EMPTY
listing plus `cursorUnchanged`, and nothing read that flag — `grep -rn
cursorUnchanged src/` found it set by four adapters and consulted by none. The
core therefore saw a folder that had lost everything. Run 1 and 2 look fine
because the run's own uploads move the ETag; from run 3 on, every run of an
idle binding ends `partial` with `mass_delete_guard` and six documents reported
missing, forever, until failure_count opens the circuit and the scheduler drops
the binding entirely. That is the ordinary case — a binding nobody is touching.
planReconcile now takes `remoteUnchanged` and plans only what happened in TREK.
Two ids were authorised by trip and never checked against it:
- resolveConflict took an item id on trust, so the owner of any trip could
decide a conflict belonging to somebody else's. Both are plain integers and
nothing compared them.
- The connection test merged the STORED credential into a probe of whatever
baseUrl the caller named — a way to have TREK post another account's API
token at a server of the caller's choosing. Stored secrets are now reused
only for the address they were stored against.
The phone's connect form was wrong in three ways at once, each of which the
desktop had right: it read the address as `baseUrl` while the field is
`base_url` (so every connection it saved carried an empty address), it drew
`allow_insecure_tls` as a text box and always sent false, and it built label
keys as `docsync.provider${cap(label)}` when the label column already holds the
full suffix — `docsync.providerProviderApiKey`, rendered raw on screen. Those
rules are not markup, so they now live in `useConnectForm`, which both shells
use. Its test fixture invented the field shape and so agreed with the broken
sheet; it now mirrors document_provider_fields.
Also: the scheduler read its interval once at bootstrap while this file's own
comment promised a per-tick read, so changing it did nothing until a restart;
and dueLinks ordered by a column that a successful run clears, which left every
binding tied and SQLite answering in insertion order — on a database with 170
bindings the same twenty ran on every tick and nothing created after them was
ever synced automatically.
This commit is contained in:
@@ -14,9 +14,20 @@ import MDocSyncSheet from './MDocSyncSheet'
|
||||
|
||||
const TRIP_ID = 3
|
||||
|
||||
/**
|
||||
* Shaped like a row of document_provider_fields, which matters more than it looks.
|
||||
*
|
||||
* `field_key` is snake_case and `label` already carries the full key suffix
|
||||
* (`providerApiKey`, not `apiKey`) — see server/src/db/document-provider-seed.ts.
|
||||
* An earlier version of this fixture invented both, which made a sheet that
|
||||
* built label keys the wrong way look correct: the fixture's `apiKey` and the
|
||||
* sheet's `docsync.provider${cap(...)}` happened to meet in the middle, while
|
||||
* the real data produced `docsync.providerProviderApiKey` and rendered the raw
|
||||
* key on screen.
|
||||
*/
|
||||
const field = (over: Partial<DocSyncProvider['fields'][number]>): DocSyncProvider['fields'][number] => ({
|
||||
field_key: 'apiToken',
|
||||
label: 'apiToken',
|
||||
field_key: 'api_token',
|
||||
label: 'providerApiToken',
|
||||
input_type: 'text',
|
||||
placeholder: null,
|
||||
hint: null,
|
||||
@@ -32,7 +43,7 @@ const provider = (over: Partial<DocSyncProvider>): DocSyncProvider => ({
|
||||
icon: 'paperless',
|
||||
available: true,
|
||||
fields: [
|
||||
field({ field_key: 'baseUrl', label: 'url', input_type: 'url', placeholder: 'https://paperless.example', secret: false }),
|
||||
field({ field_key: 'base_url', label: 'providerUrl', input_type: 'url', placeholder: 'https://paperless.example', secret: false }),
|
||||
field({}),
|
||||
],
|
||||
...over,
|
||||
@@ -102,8 +113,8 @@ beforeEach(() => {
|
||||
onClose.mockClear()
|
||||
providers = [
|
||||
provider({}),
|
||||
provider({ id: 'nextcloud', name: 'Nextcloud', fields: [field({ field_key: 'appPassword', label: 'appPassword' })] }),
|
||||
provider({ id: 'papra', name: 'Papra', fields: [field({ field_key: 'apiKey', label: 'apiKey' })] }),
|
||||
provider({ id: 'nextcloud', name: 'Nextcloud', fields: [field({ field_key: 'app_password', label: 'providerAppPassword' })] }),
|
||||
provider({ id: 'papra', name: 'Papra', fields: [field({ field_key: 'api_key', label: 'providerApiKey' })] }),
|
||||
]
|
||||
connections = [connection({}), connection({ id: 6, providerId: 'nextcloud' })]
|
||||
links = [link()]
|
||||
|
||||
@@ -14,6 +14,7 @@ import TrekMark from '../../../../components/shared/TrekMark'
|
||||
import {
|
||||
useDocSync, type DocSyncLink, type DocSyncProvider,
|
||||
} from '../../../../components/Files/docsync/useDocSync'
|
||||
import { useConnectForm } from '../../../../components/Files/docsync/useConnectForm'
|
||||
import { relativeTime } from '../../../../utils/relativeTime'
|
||||
|
||||
/**
|
||||
@@ -344,7 +345,14 @@ function DetailView({
|
||||
)
|
||||
}
|
||||
|
||||
/** Credentials for a store this instance offers but the trip has not used yet. */
|
||||
/**
|
||||
* Credentials for a store this instance offers but the trip has not used yet.
|
||||
*
|
||||
* The rules — which field is the address, which one is a switch, how a label
|
||||
* becomes a key — live in `useConnectForm`, the same one the desktop dialog
|
||||
* uses. Written out here a second time they came out wrong in four separate
|
||||
* ways, including an address this form never actually sent.
|
||||
*/
|
||||
function ConnectView({
|
||||
provider,
|
||||
sync,
|
||||
@@ -355,45 +363,49 @@ function ConnectView({
|
||||
onDone: () => void
|
||||
}) {
|
||||
const { t } = useTranslation()
|
||||
const [values, setValues] = useState<Record<string, string>>({})
|
||||
const form = useConnectForm(provider, sync.connectionFor(provider.id), sync)
|
||||
const [verdict, setVerdict] = useState<{ connected: boolean; account?: string; error?: string } | null>(null)
|
||||
|
||||
const baseUrl = values.baseUrl ?? ''
|
||||
const credentials = Object.fromEntries(Object.entries(values).filter(([k]) => k !== 'baseUrl'))
|
||||
const missing = provider.fields.some(f => f.required && !(values[f.field_key] || '').trim())
|
||||
|
||||
const probe = async () => {
|
||||
setVerdict(await sync.testConnection(provider.id, baseUrl, credentials, false))
|
||||
}
|
||||
|
||||
const save = async () => {
|
||||
if (await sync.saveConnection(provider.id, baseUrl, credentials, false)) onDone()
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="mt-3 flex flex-col gap-3">
|
||||
<p className="font-geist text-[0.71875rem] leading-snug text-m-muted">{t(`docsync.connect.about.${provider.id}`)}</p>
|
||||
|
||||
{provider.fields.map(f => (
|
||||
{form.fields.map(f => (
|
||||
<label key={f.field_key} className="flex flex-col gap-[6px]">
|
||||
<span className="font-geist text-[0.6875rem] font-bold text-m-muted">
|
||||
{t(`docsync.provider${cap(f.label)}`)}
|
||||
{t(form.labelKey(f))}
|
||||
{f.required && <span className="text-[color:var(--m-st-danger)]"> *</span>}
|
||||
</span>
|
||||
<input
|
||||
type={f.secret ? 'password' : f.input_type === 'url' ? 'url' : 'text'}
|
||||
value={values[f.field_key] ?? ''}
|
||||
onChange={e => { setValues(v => ({ ...v, [f.field_key]: e.target.value })); setVerdict(null) }}
|
||||
placeholder={f.placeholder ?? ''}
|
||||
type={f.input_type === 'password' ? 'password' : 'text'}
|
||||
value={form.valueOf(f.field_key)}
|
||||
onChange={e => { form.setValue(f.field_key, e.target.value); setVerdict(null) }}
|
||||
placeholder={form.placeholderOf(f)}
|
||||
autoCapitalize="none"
|
||||
autoCorrect="off"
|
||||
spellCheck={false}
|
||||
className="h-11 rounded-2xl border border-[color:var(--m-inbr)] bg-[color:var(--m-inner)] px-3 text-[0.8125rem] text-m-ink outline-none"
|
||||
/>
|
||||
{f.hint && <span className="font-geist text-[0.625rem] text-m-faint">{f.hint}</span>}
|
||||
{form.hintKey(f) && (
|
||||
<span className="font-geist text-[0.625rem] text-m-faint">{t(form.hintKey(f) as string)}</span>
|
||||
)}
|
||||
</label>
|
||||
))}
|
||||
|
||||
<div className="flex items-center justify-between gap-3 rounded-2xl border border-[color:var(--m-rowbr)] bg-m-sheetop px-3 py-[9px]">
|
||||
<span className="min-w-0 flex-1">
|
||||
<span className="block text-[0.78125rem] font-semibold text-m-ink">{t('docsync.allowInsecureTls')}</span>
|
||||
<span className="mt-[2px] block font-geist text-[0.625rem] leading-snug text-m-faint">
|
||||
{t('docsync.connect.insecureHint')}
|
||||
</span>
|
||||
</span>
|
||||
<MToggle
|
||||
checked={form.insecureTls}
|
||||
ariaLabel={t('docsync.allowInsecureTls')}
|
||||
onChange={form.setInsecureTls}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{verdict && (
|
||||
<p
|
||||
className={`rounded-2xl px-3 py-2 font-geist text-[0.6875rem] ${
|
||||
@@ -408,19 +420,25 @@ function ConnectView({
|
||||
</p>
|
||||
)}
|
||||
|
||||
{sync.error && !verdict && (
|
||||
<p className="rounded-2xl bg-[color:var(--m-ic)] px-3 py-2 font-geist text-[0.6875rem] text-[color:var(--m-st-danger)]">
|
||||
{t(`docsync.error.${sync.error}`)}
|
||||
</p>
|
||||
)}
|
||||
|
||||
<div className="flex gap-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => void probe()}
|
||||
disabled={missing || sync.busy === 'test'}
|
||||
onClick={() => void form.probe().then(setVerdict)}
|
||||
disabled={!form.complete || sync.busy === 'test'}
|
||||
className="h-11 flex-1 rounded-2xl border border-[color:var(--m-rowbr)] text-[0.8125rem] font-bold text-m-ink disabled:opacity-50"
|
||||
>
|
||||
{sync.busy === 'test' ? <Loader2 size={15} className="mx-auto animate-spin" /> : t('docsync.test')}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => void save()}
|
||||
disabled={missing || sync.busy === 'save'}
|
||||
onClick={() => void form.save().then(ok => { if (ok) onDone() })}
|
||||
disabled={!form.complete || sync.busy === 'save'}
|
||||
className="h-11 flex-1 rounded-2xl bg-m-act text-[0.8125rem] font-bold text-m-actfg disabled:opacity-50"
|
||||
>
|
||||
{sync.busy === 'save' ? <Loader2 size={15} className="mx-auto animate-spin" /> : t('common.save')}
|
||||
@@ -701,11 +719,6 @@ function Empty({ text, hint }: { text: string; hint?: string }) {
|
||||
)
|
||||
}
|
||||
|
||||
/** `apiToken` → `ApiToken`, so the field's stored label suffix becomes its key. */
|
||||
function cap(s: string): string {
|
||||
return s.charAt(0).toUpperCase() + s.slice(1)
|
||||
}
|
||||
|
||||
/** A folder name from the trip's own title, so nobody has to invent one. */
|
||||
function slugFor(title: string | undefined, tripId: number | string): string {
|
||||
const base = (title || 'trek')
|
||||
|
||||
Reference in New Issue
Block a user