Skip to content

armgit / xerj

Public Imported from GitHub
0

AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.

fix(api): accept share_id when claiming a share link (#1684)

xerj-orgd34229ac45cc
diff --git a/engine/crates/xerj-api/src/share.rs b/engine/crates/xerj-api/src/share.rs
index b13b5e769..cc486f2d3 100644
--- a/engine/crates/xerj-api/src/share.rs
+++ b/engine/crates/xerj-api/src/share.rs
@@ -1081,6 +1081,9 @@ async fn create_share_inner(
let mut out = record.public_json(now);
out["share_id"] = json!(share_id);
+ // The claim body reads `id`. Returning only `share_id` made the obvious
+ // follow-up — post this object back — miss the share.
+ out["id"] = json!(share_id);
out["url_path"] = json!(format!("/_xerj-console/share#{share_id}"));
out["passcode"] = json!(passcode);
out["claim_limiter"] = json!({
@@ -1215,14 +1218,25 @@ pub async fn claim_share(
no_store(claim_share_inner(state, source, body).await)
}
-/// The share id a claim body names. Anything that is not a string is the
-/// empty id, which no share has: a body without one is answered like any other
-/// unknown link — charged to the source window, audited, `404`.
+/// The share id a claim body names. The documented field is `id`. `share_id`
+/// is accepted too, because that is the field name `POST /_share` returns —
+/// posting the create response's own key back used to 404 as "unknown share
+/// link". A non-string is the empty id, which no share has: a body without
+/// one is answered like any other unknown link — charged to the source
+/// window, audited, `404`. When both fields are present, `id` wins.
fn claim_id(body: Option<&Value>) -> String {
- body.and_then(|b| b.get("id"))
- .and_then(Value::as_str)
- .map(|s| s.trim().to_string())
- .unwrap_or_default()
+ let Some(body) = body else {
+ return String::new();
+ };
+ for key in ["id", "share_id"] {
+ if let Some(s) = body.get(key).and_then(Value::as_str) {
+ let s = s.trim();
+ if !s.is_empty() {
+ return s.to_string();
+ }
+ }
+ }
+ String::new()
}
async fn claim_share_inner(state: AppState, source: String, body: Option<Json<Value>>) -> Response {
diff --git a/engine/crates/xerj-api/tests/share_links_review_findings.rs b/engine/crates/xerj-api/tests/share_links_review_findings.rs
index 2092bcded..98c6ff791 100644
--- a/engine/crates/xerj-api/tests/share_links_review_findings.rs
+++ b/engine/crates/xerj-api/tests/share_links_review_findings.rs
@@ -134,6 +134,49 @@ async fn audit_entries(native: &axum::Router) -> Vec<Value> {
// The share id is never part of a request path
// ─────────────────────────────────────────────────────────────────────────────
+/// `POST /_share` returns the secret as `share_id`. Claiming with that same
+/// field name has to work, and the response also carries `id` so the
+/// documented claim body can be copied from it.
+#[tokio::test]
+async fn a_claim_accepts_the_share_id_field_the_create_response_returns() {
+ let dir = tempfile::tempdir().expect("tempdir");
+ let state = state_over(dir.path().to_str().expect("utf8 path"));
+ let es = build_es_compat_router(state);
+ let (status, _, _) = send(&es, "PUT", "/casefile-alias", &admin(), "").await;
+ assert_eq!(status, StatusCode::OK);
+ let (status, share, _) = send(
+ &es,
+ "POST",
+ "/_share",
+ &admin(),
+ &json!({"index": "casefile-alias", "max_claims": 1}).to_string(),
+ )
+ .await;
+ assert_eq!(status, StatusCode::OK, "{share}");
+ assert_eq!(share["id"], share["share_id"], "{share}");
+ let (status, got, _) = send(
+ &es,
+ "POST",
+ "/_share/claim",
+ "",
+ &json!({
+ "share_id": share["share_id"],
+ "passcode": share["passcode"],
+ })
+ .to_string(),
+ )
+ .await;
+ assert_eq!(
+ status,
+ StatusCode::OK,
+ "claim by share_id should succeed: {got}"
+ );
+ assert!(
+ got["api_key"].as_str().is_some_and(|k| !k.is_empty()),
+ "{got}"
+ );
+}
+
#[tokio::test]
async fn the_share_id_travels_in_the_claim_body_never_in_a_path() {
let dir = tempfile::tempdir().expect("tempdir");