Repository navigation
fix(plugins): a malformed blocklist refresh keeps the last good list #1569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
26bfeaf
437bfbf
9387e93
7a442ad
a469228
08efb83
a89e8e3
d761e57
9bf272e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,7 @@ use crate::manifest::{Permission, Runtime, valid_id, version_parts}; | |
| pub const DEFAULT_URL: &str = "https://plugins.rustgis.app/registry.json"; | ||
|
|
||
| /// The most entries read from a registry (or a blocklist): room for tens of thousands of plugins. | ||
| const MAX_ENTRIES: usize = 100_000; | ||
| pub(crate) const MAX_ENTRIES: usize = 100_000; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit, outside this change's logic: this constant is also what the registry reader uses at
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The registry reader's |
||
|
|
||
| /// The largest registry downloaded (a registry entry is about 1 KB, so this is ample). | ||
| pub const MAX_REGISTRY_BYTES: usize = 64 * 1024 * 1024; | ||
|
|
@@ -192,15 +192,40 @@ pub fn blocklist_url(registry: &str) -> String { | |
| resolve(registry, "blocklist.json") | ||
| } | ||
|
|
||
| /// Reads a blocklist (an object with a `blocked` array, or a bare array). | ||
| /// Reads a blocklist (an object with a `blocked` array, or a bare array). Any other shape, an | ||
| /// entry whose `id` isn't a plugin id or whose `sha256`, when given, isn't a SHA-256 hash (either | ||
| /// would match nothing; no `sha256`, or `null`, withdraws every version), or more than 100,000 | ||
| /// entries, is an error rather than a shorter list, so a malformed refresh leaves the last good | ||
| /// list in place: only `[]` or `{"blocked": []}` clears it. | ||
| pub fn parse_blocklist(text: &str) -> Result<Vec<Blocked>, String> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Silent discard of the cached blocklist at startup (Never silent). The new strict parser makes Suggested direction: match on the parse result in
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 7a442ad: |
||
| let root: Value = serde_json::from_str(text).map_err(|e| format!("the blocklist isn't valid JSON: {e}"))?; | ||
| let list = match &root { | ||
| Value::Array(a) => a.clone(), | ||
| Value::Object(o) => o.get("blocked").and_then(Value::as_array).cloned().unwrap_or_default(), | ||
| _ => Vec::new(), | ||
| let list = match root { | ||
| Value::Array(a) => a, | ||
| Value::Object(mut o) => match o.remove("blocked") { | ||
| Some(Value::Array(a)) => a, | ||
| _ => return Err("the blocklist has no 'blocked' list".into()), | ||
| }, | ||
| _ => return Err("the blocklist isn't a list or an object with a 'blocked' list".into()), | ||
| }; | ||
| Ok(list.into_iter().take(MAX_ENTRIES).filter_map(|v| serde_json::from_value::<Blocked>(v).ok()).collect()) | ||
| if list.len() > MAX_ENTRIES { | ||
| return Err(format!("the blocklist has {} entries; at most {MAX_ENTRIES} can be read", list.len())); | ||
|
Mavrick-F marked this conversation as resolved.
Resolved
|
||
| } | ||
| list.into_iter() | ||
| .enumerate() | ||
| .map(|(i, v)| { | ||
| // The reason is cut short: it can quote the entry, which comes from the network. | ||
| let b = serde_json::from_value::<Blocked>(v) | ||
| .map_err(|e| format!("blocklist entry {}: {}", i + 1, e.to_string().chars().take(200).collect::<String>()))?; | ||
| if !valid_id(&b.id, false) { | ||
| return Err(format!("blocklist entry {}: id isn't a plugin id", i + 1)); | ||
| } | ||
| // Either case: `blocked` compares hashes ignoring it. | ||
| match &b.sha256 { | ||
| Some(h) if !is_sha256(&h.to_ascii_lowercase()) => Err(format!("blocklist entry {}: sha256 isn't a SHA-256 hash", i + 1)), | ||
|
Mavrick-F marked this conversation as resolved.
Resolved
|
||
| _ => Ok(b), | ||
| } | ||
| }) | ||
| .collect() | ||
| } | ||
|
|
||
| /// Why a package is blocked, if it is. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -75,8 +75,23 @@ fn write_state(st: &State) -> Result<(), String> { | |
| pub fn load_installed() -> Vec<String> { | ||
| let mut errors = Vec::new(); | ||
| let Some(dir) = dir() else { return errors }; | ||
| if let Some(list) = std::fs::read_to_string(dir.join("blocklist.json")).ok().and_then(|t| registry::parse_blocklist(&t).ok()) { | ||
| crate::set_blocklist(list); | ||
| // A cache that can't be read (or one an older version saved that this one refuses) is | ||
| // reported, not trusted; there being none is fine. | ||
| let cached = dir.join("blocklist.json"); | ||
| let list = match std::fs::read_to_string(&cached) { | ||
| Ok(text) => registry::parse_blocklist(&text).map(Some), | ||
| Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None), | ||
| Err(e) => Err(e.to_string()), | ||
| }; | ||
| match list { | ||
| Ok(Some(list)) => { | ||
| crate::set_blocklist(list); | ||
| } | ||
| Ok(None) => {} | ||
| Err(e) => errors.push(format!( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Silent loss on the Python default path (new with this PR, medium-high confidence, about 75%). Before this PR, a cache with one bad entry still applied its good withdrawals, because
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed that the stakes are higher now. A refused cache means no withdrawals until the next fetch, instead of the good part of the list. I've said so in the PR description, along with where Python callers get the report: |
||
| "{}: {e}; it was ignored, so withdrawn plugins may not be blocked until the blocklist is fetched again", | ||
| cached.display() | ||
| )), | ||
|
Mavrick-F marked this conversation as resolved.
Resolved
|
||
| } | ||
| let st = read_state(); | ||
| let mut paths: Vec<PathBuf> = std::fs::read_dir(&dir) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -613,20 +613,37 @@ pub fn poll(app: &mut RustGisApp, ctx: &egui::Context) { | |
| Job::Blocklist { registry, slot } => match take(&slot) { | ||
| None => waiting.push(Job::Blocklist { registry, slot }), | ||
| Some(Ok(b)) => { | ||
| let text = String::from_utf8_lossy(&b).to_string(); | ||
| // Not text is refused too: a replaced byte in an id would match nothing. | ||
| let text = std::str::from_utf8(&b).map_err(|e| format!("it isn't text: {e}")); | ||
| #[cfg(not(target_arch = "wasm32"))] | ||
| let pulled = rp::store::save_blocklist(®istry, &text).unwrap_or_default(); | ||
| let pulled = text.and_then(|t| rp::store::save_blocklist(®istry, t)); | ||
| #[cfg(target_arch = "wasm32")] | ||
| let pulled = rp::registry::parse_blocklist(&text).map(|l| rp::apply_blocklist(®istry, l)).unwrap_or_default(); | ||
| let pulled = text.and_then(rp::registry::parse_blocklist).map(|l| rp::apply_blocklist(®istry, l)); | ||
|
Mavrick-F marked this conversation as resolved.
Resolved
|
||
| // One that can't be read leaves the list in force as it was. | ||
| let pulled = pulled.unwrap_or_else(|e| { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same gap in the Plugin Manager. The
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as the engine thread: |
||
| app.session.warn(crate::i18n::fmt( | ||
| "The plugin blocklist couldn't be read ({error}); the last one read, if any, stays in force.", | ||
| &[("error", &e)], | ||
| )); | ||
| Vec::new() | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| for name in pulled { | ||
| app.session.notify( | ||
| crate::i18n::fmt("{name} was withdrawn from the plugin registry and has been turned off.", &[("name", &name)]), | ||
| true, | ||
| ); | ||
| } | ||
| } | ||
| // No blocklist: nothing is blocked. | ||
| Some(Err(_)) => {} | ||
| // Another registry may have no blocklist (it blocks nothing); the hosted one always | ||
| // has one, so the list in force may be out of date. | ||
| Some(Err(e)) => { | ||
| if rp::registry::is_default(®istry) { | ||
| app.session.warn(crate::i18n::fmt( | ||
| "The plugin blocklist couldn't be downloaded ({error}); the last one read, if any, stays in force.", | ||
| &[("error", &e)], | ||
| )); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
| } | ||
|
Mavrick-F marked this conversation as resolved.
Resolved
|
||
| }, | ||
| Job::Download { entry, slot } => match take(&slot) { | ||
| None => waiting.push(Job::Download { entry, slot }), | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.