Share my setup, links: a waybetter://setup link opened from outside reaches the shell as text on the install-link event (shape and size checked natively, never decoded or applied there); the install controller hands it to the app's onSetupLink and stages nothing
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
a8710dfe02
commit
fe7f60cd8d
4 changed files with 73 additions and 8 deletions
|
|
@ -20,8 +20,14 @@ pub const RECORDS_FILE: &str = "store-installed.json";
|
||||||
/// The last verified catalog and its signature (verified again when read back).
|
/// The last verified catalog and its signature (verified again when read back).
|
||||||
pub const CATALOG_CACHE: &str = "store-catalog.json";
|
pub const CATALOG_CACHE: &str = "store-catalog.json";
|
||||||
pub const CATALOG_SIG_CACHE: &str = "store-catalog.json.sig";
|
pub const CATALOG_SIG_CACHE: &str = "store-catalog.json.sig";
|
||||||
/// Shell event: a validated install link (`{ link: { kind, url } }`) or `{ error }`.
|
/// Shell event: a validated install link (`{ link: { kind, url } }`), a "Share my setup" link
|
||||||
|
/// (`{ setup }`: its text, for the shell's preview) or `{ error }`.
|
||||||
pub const EVENT_LINK: &str = "install-link";
|
pub const EVENT_LINK: &str = "install-link";
|
||||||
|
/// "Share my setup" links: `waybetter://setup?app=…&d=…`. Native code only checks the shape and
|
||||||
|
/// hands the text to the shell; it never decodes or applies a setup (the shell shows what would
|
||||||
|
/// change and asks first).
|
||||||
|
pub const SETUP_LINK: &str = "waybetter://setup";
|
||||||
|
const MAX_SETUP_LINK: usize = 24 * 1024;
|
||||||
/// Links received before the shell asked for them (startup, or while the shell reloads).
|
/// Links received before the shell asked for them (startup, or while the shell reloads).
|
||||||
const MAX_PENDING: usize = 8;
|
const MAX_PENDING: usize = 8;
|
||||||
const MAX_STAGED: usize = 8;
|
const MAX_STAGED: usize = 8;
|
||||||
|
|
@ -38,6 +44,22 @@ pub struct LinkEvent {
|
||||||
pub link: Option<InstallLink>,
|
pub link: Option<InstallLink>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub error: Option<String>,
|
pub error: Option<String>,
|
||||||
|
/// A "Share my setup" link, as text (see [`SETUP_LINK`]).
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub setup: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `Some` when `raw` is a setup link: its text when it has the plain shape (the path, a query of
|
||||||
|
/// URL-safe characters, bounded), else an error. `None`: not a setup link.
|
||||||
|
pub fn setup_link(raw: &str) -> Option<Result<String, String>> {
|
||||||
|
let rest = raw.strip_prefix(SETUP_LINK)?;
|
||||||
|
let query = rest.strip_prefix("/?").or_else(|| rest.strip_prefix('?'));
|
||||||
|
let ok = query.is_some_and(|query| {
|
||||||
|
!query.is_empty()
|
||||||
|
&& raw.len() <= MAX_SETUP_LINK
|
||||||
|
&& query.bytes().all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'=' | b'&' | b'%' | b'.'))
|
||||||
|
});
|
||||||
|
Some(if ok { Ok(raw.to_owned()) } else { Err("LINK: this setup link is not valid".to_owned()) })
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize)]
|
#[derive(Clone, Debug, Serialize)]
|
||||||
|
|
@ -220,9 +242,13 @@ impl Store {
|
||||||
/// A link reached the app (OS deep link, second launch): validate it and queue it for the
|
/// A link reached the app (OS deep link, second launch): validate it and queue it for the
|
||||||
/// shell. Returns the event to emit ([`EVENT_LINK`]). Nothing is downloaded or installed here.
|
/// shell. Returns the event to emit ([`EVENT_LINK`]). Nothing is downloaded or installed here.
|
||||||
pub fn receive_link(&self, raw: &str) -> LinkEvent {
|
pub fn receive_link(&self, raw: &str) -> LinkEvent {
|
||||||
let event = match dist::parse_install_link(self.config, raw) {
|
let event = match setup_link(raw) {
|
||||||
Ok(link) => LinkEvent { link: Some(link), error: None },
|
Some(Ok(text)) => LinkEvent { link: None, error: None, setup: Some(text) },
|
||||||
Err(error) => LinkEvent { link: None, error: Some(error) },
|
Some(Err(error)) => LinkEvent { link: None, error: Some(error), setup: None },
|
||||||
|
None => match dist::parse_install_link(self.config, raw) {
|
||||||
|
Ok(link) => LinkEvent { link: Some(link), error: None, setup: None },
|
||||||
|
Err(error) => LinkEvent { link: None, error: Some(error), setup: None },
|
||||||
|
},
|
||||||
};
|
};
|
||||||
let mut inner = self.lock();
|
let mut inner = self.lock();
|
||||||
if inner.pending.len() >= MAX_PENDING {
|
if inner.pending.len() >= MAX_PENDING {
|
||||||
|
|
@ -598,6 +624,19 @@ mod tests {
|
||||||
assert!(ok.link.is_some() && ok.error.is_none());
|
assert!(ok.link.is_some() && ok.error.is_none());
|
||||||
let bad = store.receive_link("waybetter://install?kind=theme&url=https://evil.example/a.json");
|
let bad = store.receive_link("waybetter://install?kind=theme&url=https://evil.example/a.json");
|
||||||
assert!(bad.link.is_none() && bad.error.unwrap().starts_with("LINK:"));
|
assert!(bad.link.is_none() && bad.error.unwrap().starts_with("LINK:"));
|
||||||
|
// A "Share my setup" link is only handed over as text; its data is never read here.
|
||||||
|
let setup = store.receive_link("waybetter://setup?app=x&d=eyJmb3JtYXQiOiJ3YXliZXR0ZXItc2V0dXAifQ");
|
||||||
|
assert_eq!(setup.setup.as_deref(), Some("waybetter://setup?app=x&d=eyJmb3JtYXQiOiJ3YXliZXR0ZXItc2V0dXAifQ"));
|
||||||
|
assert!(setup.link.is_none() && setup.error.is_none());
|
||||||
|
for bad in ["waybetter://setup", "waybetter://setup?", "waybetter://setup?d=<script>", "waybetter://setup?d=a b", "waybetter://setupx?d=a"] {
|
||||||
|
let event = store.receive_link(bad);
|
||||||
|
assert!(event.setup.is_none(), "{bad}");
|
||||||
|
assert!(event.error.is_some(), "{bad}");
|
||||||
|
}
|
||||||
|
let long = format!("waybetter://setup?d={}", "a".repeat(MAX_SETUP_LINK));
|
||||||
|
assert!(store.receive_link(&long).setup.is_none(), "bounded");
|
||||||
|
assert_eq!(serde_json::to_string(&setup).unwrap(), r#"{"setup":"waybetter://setup?app=x&d=eyJmb3JtYXQiOiJ3YXliZXR0ZXItc2V0dXAifQ"}"#);
|
||||||
|
store.take_pending();
|
||||||
for _ in 0..20 {
|
for _ in 0..20 {
|
||||||
store.receive_link("waybetter://install?kind=theme&url=https://waybetter.fyi/a.json");
|
store.receive_link("waybetter://install?kind=theme&url=https://waybetter.fyi/a.json");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,11 @@ export type InstallControllerOptions<Theme> = {
|
||||||
onThemesChanged?(): void;
|
onThemesChanged?(): void;
|
||||||
/** A string pack was installed (the app hands the site tabs the packs that are on). */
|
/** A string pack was installed (the app hands the site tabs the packs that are on). */
|
||||||
onStringsChanged?(): void | Promise<void>;
|
onStringsChanged?(): void | Promise<void>;
|
||||||
|
/**
|
||||||
|
* A `waybetter://setup` link was opened (its text, shape-checked natively): the app shows its
|
||||||
|
* "Share my setup" preview (panel/ShareSetup.tsx). Absent: such links are ignored.
|
||||||
|
*/
|
||||||
|
onSetupLink?(text: string): void;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type InstallController<Theme> = {
|
export type InstallController<Theme> = {
|
||||||
|
|
@ -191,7 +196,8 @@ export function createInstallController<Theme>(options: InstallControllerOptions
|
||||||
if (started) return () => {};
|
if (started) return () => {};
|
||||||
started = true;
|
started = true;
|
||||||
const handle = (event: LinkEvent) => {
|
const handle = (event: LinkEvent) => {
|
||||||
if (event.link) void controller.requestLink(event.link);
|
if (event.setup) options.onSetupLink?.(event.setup);
|
||||||
|
else if (event.link) void controller.requestLink(event.link);
|
||||||
else if (event.error) controller.linkRejected(event.error);
|
else if (event.error) controller.linkRejected(event.error);
|
||||||
};
|
};
|
||||||
// Native code queues every link and also emits it; a handled event clears the queue so a
|
// Native code queues every link and also emits it; a handled event clears the queue so a
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,7 @@ export type InstallPreview = {
|
||||||
};
|
};
|
||||||
|
|
||||||
/** The `install-link` event and `install_pending` entries (waybetter-host `LinkEvent`). */
|
/** The `install-link` event and `install_pending` entries (waybetter-host `LinkEvent`). */
|
||||||
export type LinkEvent = { link?: { kind: InstallKind; url: string }; error?: string };
|
export type LinkEvent = { link?: { kind: InstallKind; url: string }; error?: string; setup?: string };
|
||||||
|
|
||||||
/** Plugin permissions (waybetter-host `PERMISSIONS`, runtime `permissions.ts`) and how much they allow. */
|
/** Plugin permissions (waybetter-host `PERMISSIONS`, runtime `permissions.ts`) and how much they allow. */
|
||||||
export const PERMISSION_TONES: Readonly<Record<string, PermissionTone>> = {
|
export const PERMISSION_TONES: Readonly<Record<string, PermissionTone>> = {
|
||||||
|
|
|
||||||
|
|
@ -65,7 +65,7 @@ test('every message key the store UI looks up exists, and {site} strings take th
|
||||||
});
|
});
|
||||||
|
|
||||||
type Theme = { id: string; name: string };
|
type Theme = { id: string; name: string };
|
||||||
function harness(options: { pending?: unknown[]; theme?: string } = {}) {
|
function harness(options: { pending?: unknown[]; theme?: string; onSetupLink?(text: string): void } = {}) {
|
||||||
const calls: Array<[string, Record<string, unknown> | undefined]> = [];
|
const calls: Array<[string, Record<string, unknown> | undefined]> = [];
|
||||||
const listeners = new Map<string, (payload: unknown) => void>();
|
const listeners = new Map<string, (payload: unknown) => void>();
|
||||||
const preview = (kind: 'plugin' | 'theme', patch: Partial<InstallPreview> = {}): InstallPreview => ({
|
const preview = (kind: 'plugin' | 'theme', patch: Partial<InstallPreview> = {}): InstallPreview => ({
|
||||||
|
|
@ -99,12 +99,32 @@ function harness(options: { pending?: unknown[]; theme?: string } = {}) {
|
||||||
save: async (theme, id, opts) => { saved.push([theme, id, opts]); return { enabled: opts.enable, fonts: [] }; },
|
save: async (theme, id, opts) => { saved.push([theme, id, opts]); return { enabled: opts.enable, fonts: [] }; },
|
||||||
};
|
};
|
||||||
let pluginsChanged = 0;
|
let pluginsChanged = 0;
|
||||||
const controller = createInstallController({ backend, t, site: 'X', theme: themes, onPluginsChanged: () => { pluginsChanged += 1; } });
|
const controller = createInstallController({ backend, t, site: 'X', theme: themes, onPluginsChanged: () => { pluginsChanged += 1; }, ...(options.onSetupLink ? { onSetupLink: options.onSetupLink } : {}) });
|
||||||
return { controller, calls, listeners, saved, pluginsChanged: () => pluginsChanged };
|
return { controller, calls, listeners, saved, pluginsChanged: () => pluginsChanged };
|
||||||
}
|
}
|
||||||
|
|
||||||
const settle = () => new Promise(resolve => setTimeout(resolve, 0));
|
const settle = () => new Promise(resolve => setTimeout(resolve, 0));
|
||||||
|
|
||||||
|
test('install controller: a setup link goes to the app, stages nothing and opens no install screen', async () => {
|
||||||
|
const seen: string[] = [];
|
||||||
|
const h = harness({ onSetupLink: text => seen.push(text) });
|
||||||
|
const stop = h.controller.start();
|
||||||
|
await settle();
|
||||||
|
h.listeners.get('install-link')!({ setup: 'waybetter://setup?app=x&d=e30' });
|
||||||
|
await settle();
|
||||||
|
assert.deepEqual(seen, ['waybetter://setup?app=x&d=e30']);
|
||||||
|
assert.equal(h.controller.current(), null);
|
||||||
|
assert.ok(!h.calls.some(c => c[0].startsWith('install_stage')));
|
||||||
|
// An app without "Share my setup" ignores such a link.
|
||||||
|
const plain = harness();
|
||||||
|
const stopPlain = plain.controller.start();
|
||||||
|
await settle();
|
||||||
|
plain.listeners.get('install-link')!({ setup: 'waybetter://setup?app=x&d=e30' });
|
||||||
|
await settle();
|
||||||
|
assert.equal(plain.controller.current(), null);
|
||||||
|
stop(); stopPlain();
|
||||||
|
});
|
||||||
|
|
||||||
test('install controller: a link is staged natively, shown, and committed only on confirm', async () => {
|
test('install controller: a link is staged natively, shown, and committed only on confirm', async () => {
|
||||||
const h = harness();
|
const h = harness();
|
||||||
const stop = h.controller.start();
|
const stop = h.controller.start();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue