solid-pod-rs-forge 0.5.0-alpha.18

Clean-room Rust Git forge (Gogs/Gitea slice) composed on solid-pod-rs primitives: pod-native issues/PRs, forge push tokens, hosted did:nostr bodies, and Blocktrails anchoring. Cites JavaScriptSolidServer `forge` behaviour by function name only; all code original.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! `private_repos`: every read (git, HTML, JSON) becomes owner-only.
//!
//! Follows the JSS `forge` dispatcher with `privateRepos: true` (cited by
//! name only), enforced centrally on the **path segments** rather than per
//! route so a route added later is fail-closed by default:
//!
//! * `<prefix>/` and `/api/repos`: anonymous `401` (`WWW-Authenticate: Basic`),
//!   otherwise the listing is filtered to the caller's own namespace.
//! * Anything under `<owner>/…` or `/api/repos/<owner>/…`: anonymous `401`,
//!   another owner's namespace `403`. The owner check runs before the
//!   repository-exists check, so existence cannot be probed.
//! * Git smart-HTTP (read **and** write): anonymous `401`, foreign namespace
//!   `403 this namespace belongs to <owner>`.
//!
//! Exempt: `xlogin.js` (static widget code), `api/token` (the mint does its
//! own authentication) and `api/hosted/…` (its handler requires an agent for
//! reads under `private_repos`).

use crate::api::{api_err, RepoSummary};
use crate::error::ForgeError;
use crate::ownership::ForgeAgent;
use crate::request::{percent_decode, ForgeRequest, ForgeResponse};
use crate::router::Route;
use crate::{html, ForgeService};

const BASIC_CHALLENGE: &str = "Basic realm=\"jss-forge\", charset=\"UTF-8\"";

fn text(status: u16, msg: &str) -> ForgeResponse {
    ForgeResponse::with_type(
        status,
        "text/plain; charset=utf-8",
        bytes::Bytes::from(msg.to_string()),
    )
}

fn challenge(api: bool) -> ForgeResponse {
    let r = if api {
        api_err(401, "authentication required")
    } else {
        text(401, "authentication required\n")
    };
    r.with_header("www-authenticate", BASIC_CHALLENGE)
}

impl ForgeService {
    /// The owner-only gate. `rel` is the prefix-stripped path. Returns the
    /// refusal to send, or `None` to proceed. Only consulted when
    /// `private_repos` is set.
    pub(crate) fn private_gate(
        &self,
        rel: &str,
        route: &Route,
        agent: &ForgeAgent,
    ) -> Option<ForgeResponse> {
        let path = rel.split('?').next().unwrap_or(rel);
        let segs: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();
        if segs == ["xlogin.js"] {
            return None;
        }
        // A git smart-HTTP path is judged as git even when its first segment
        // looks like the API namespace (`/api/<name>.git/info/refs`).
        let git = matches!(route, Route::GitSmart { .. });
        let api = !git && segs.first() == Some(&"api");
        if api && matches!(segs.get(1), Some(&"token" | &"hosted")) {
            return None;
        }
        let me = agent.owner();
        if matches!(agent, ForgeAgent::Anonymous) {
            return Some(challenge(api));
        }
        // The namespace the request addresses (None for the root listings).
        // The parsed route's owner is what will be served, so it decides;
        // the raw segments are only a fail-closed fallback for routes that
        // carry no owner.
        let target = route.owner().or(if api {
            if segs.get(1) == Some(&"repos") {
                segs.get(2).copied()
            } else {
                None
            }
        } else {
            segs.first().copied()
        });
        let Some(me) = me else {
            return Some(if api {
                api_err(403, "no pod namespace for this agent")
            } else {
                text(403, "private forge\n")
            });
        };
        match target {
            Some(t) if t != me => Some(if matches!(route, Route::GitSmart { .. }) {
                text(403, &format!("this namespace belongs to {t}\n"))
            } else if api {
                api_err(403, "private forge")
            } else {
                text(403, "private forge\n")
            }),
            _ => None,
        }
    }

    /// `GET <prefix>/` with the `?q=` filter and, under `private_repos`, the
    /// caller's own namespace only (JSS `indexPage`).
    pub(crate) async fn h_index_filtered(
        &self,
        req: &ForgeRequest,
        agent: &ForgeAgent,
    ) -> Result<ForgeResponse, ForgeError> {
        let owner_filter = if self.cfg.private_repos {
            agent.owner().map(str::to_string)
        } else {
            None
        };
        let q: String = req
            .query_param("q")
            .map(|v| percent_decode(&v).trim().chars().take(256).collect())
            .unwrap_or_default();
        let mut rows: Vec<RepoSummary> = self.summaries(owner_filter.as_deref()).await;
        let had_any = !rows.is_empty();
        if !q.is_empty() {
            let needle = q.to_lowercase();
            rows.retain(|r| {
                format!("{}/{}", r.owner, r.name)
                    .to_lowercase()
                    .contains(&needle)
                    || r.description.to_lowercase().contains(&needle)
            });
        }
        let page =
            html::index_page_full(&self.prefix(), &rows, &q, had_any, self.cfg.private_repos);
        let pairs: Vec<(String, String)> = rows
            .iter()
            .map(|r| (r.owner.clone(), r.name.clone()))
            .collect();
        Ok(ForgeResponse::html(
            200,
            self.decorate_listing(&page, &pairs),
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::api::tests::{casey, dana, rq};
    use crate::gitrun::testutil::demo_repo;
    use crate::ForgeConfig;

    fn private_svc() -> (tempfile::TempDir, ForgeService) {
        let td = tempfile::TempDir::new().unwrap();
        demo_repo(td.path()); // casey/demo
        let cfg = ForgeConfig {
            private_repos: true,
            ..Default::default()
        };
        let svc = ForgeService::new(cfg, td.path()).unwrap();
        (td, svc)
    }

    async fn status(svc: &ForgeService, m: &str, p: &str, q: &str, a: ForgeAgent) -> ForgeResponse {
        svc.handle(rq(m, p, q, ""), a).await.unwrap()
    }

    // JSS "privateRepos: true flips every read (git, HTML, JSON) to owner-only"
    #[tokio::test]
    async fn html_reads_are_owner_only() {
        let (_td, svc) = private_svc();
        let anon = status(&svc, "GET", "/forge/casey/demo", "", ForgeAgent::Anonymous).await;
        assert_eq!(anon.status, 401);
        assert!(anon
            .headers
            .iter()
            .any(|(k, v)| k == "www-authenticate" && v.starts_with("Basic")));
        assert_eq!(
            status(&svc, "GET", "/forge/casey/demo", "", dana())
                .await
                .status,
            403
        );
        assert_eq!(
            status(&svc, "GET", "/forge/casey/demo", "", casey())
                .await
                .status,
            200
        );
        // every browse lane is gated, existing repo or not (no existence probe)
        for p in [
            "/forge/casey",
            "/forge/casey/demo/tree/main",
            "/forge/casey/demo/blob/main/README.md",
            "/forge/casey/demo/raw/main/README.md",
            "/forge/casey/demo/commits/main",
            "/forge/casey/demo/branches",
            "/forge/casey/demo/issues",
            "/forge/casey/demo/search",
            "/forge/casey/demo/releases",
            "/forge/casey/demo/archive/main.zip",
            "/forge/casey/nope",
        ] {
            assert_eq!(
                status(&svc, "GET", p, "", ForgeAgent::Anonymous)
                    .await
                    .status,
                401,
                "{p}"
            );
            assert_eq!(status(&svc, "GET", p, "", dana()).await.status, 403, "{p}");
        }
        assert_eq!(
            status(&svc, "GET", "/forge/casey/nope", "", casey())
                .await
                .status,
            404
        );
    }

    #[tokio::test]
    async fn git_reads_are_owner_only() {
        let (_td, svc) = private_svc();
        let p = "/forge/casey/demo.git/info/refs";
        let q = "service=git-upload-pack";
        let anon = status(&svc, "GET", p, q, ForgeAgent::Anonymous).await;
        assert_eq!(anon.status, 401);
        assert!(anon.headers.iter().any(|(k, _)| k == "www-authenticate"));
        let other = status(&svc, "GET", p, q, dana()).await;
        assert_eq!(other.status, 403);
        assert!(String::from_utf8_lossy(&other.body).contains("belongs to casey"));
        let own = status(&svc, "GET", p, q, casey()).await;
        assert_ne!(own.status, 401);
        assert_ne!(own.status, 403);
        let pack = status(
            &svc,
            "POST",
            "/forge/casey/demo.git/git-upload-pack",
            "",
            dana(),
        )
        .await;
        assert_eq!(pack.status, 403);
    }

    #[tokio::test]
    async fn json_reads_are_owner_only_and_the_index_is_filtered() {
        let (_td, svc) = private_svc();
        assert_eq!(
            status(&svc, "GET", "/forge/api/repos", "", ForgeAgent::Anonymous)
                .await
                .status,
            401
        );
        let own = status(&svc, "GET", "/forge/api/repos", "", casey()).await;
        let v: serde_json::Value = serde_json::from_slice(&own.body).unwrap();
        assert!(v["repos"]
            .as_array()
            .unwrap()
            .iter()
            .all(|r| r["owner"] == "casey"));
        assert_eq!(v["repos"].as_array().unwrap().len(), 1);
        // dana sees an empty list, not casey's repos
        let other = status(&svc, "GET", "/forge/api/repos", "", dana()).await;
        let v: serde_json::Value = serde_json::from_slice(&other.body).unwrap();
        assert_eq!(v["repos"].as_array().unwrap().len(), 0);
        assert_eq!(
            status(&svc, "GET", "/forge/api/repos/casey/demo", "", dana())
                .await
                .status,
            403
        );
        assert_eq!(
            status(
                &svc,
                "GET",
                "/forge/api/repos/casey/demo/tree/main",
                "",
                dana()
            )
            .await
            .status,
            403
        );
        assert_eq!(
            status(&svc, "GET", "/forge/api/repos/casey/demo", "", casey())
                .await
                .status,
            200
        );
        assert_eq!(
            status(&svc, "GET", "/forge/api/repos/casey", "", dana())
                .await
                .status,
            403
        );
        // the HTML index is filtered the same way
        let idx = status(&svc, "GET", "/forge", "", dana()).await;
        assert_eq!(idx.status, 200);
        assert!(!String::from_utf8_lossy(&idx.body).contains("/forge/casey/demo"));
        assert_eq!(
            status(&svc, "GET", "/forge", "", ForgeAgent::Anonymous)
                .await
                .status,
            401
        );
        let mine = status(&svc, "GET", "/forge", "", casey()).await;
        assert!(String::from_utf8_lossy(&mine.body).contains("/forge/casey/demo"));
    }

    #[tokio::test]
    async fn xlogin_and_token_are_exempt() {
        let (_td, svc) = private_svc();
        assert_eq!(
            status(&svc, "GET", "/forge/xlogin.js", "", ForgeAgent::Anonymous)
                .await
                .status,
            200
        );
        // the token mint authenticates itself: anonymous gets its own 401, not the gate's challenge
        let r = status(&svc, "POST", "/forge/api/token", "", ForgeAgent::Anonymous).await;
        assert_eq!(r.status, 401);
        assert!(!r
            .headers
            .iter()
            .any(|(k, v)| k == "www-authenticate" && v.starts_with("Basic")));
    }

    #[tokio::test]
    async fn public_forge_is_unchanged() {
        let td = tempfile::TempDir::new().unwrap();
        demo_repo(td.path());
        let svc = ForgeService::new(ForgeConfig::default(), td.path()).unwrap();
        assert_eq!(
            status(&svc, "GET", "/forge/casey/demo", "", ForgeAgent::Anonymous)
                .await
                .status,
            200
        );
        assert_eq!(
            status(&svc, "GET", "/forge/api/repos", "", ForgeAgent::Anonymous)
                .await
                .status,
            200
        );
    }

    // Security review L5: `/api/<name>.git/...` is git smart-HTTP to the
    // router but looked like the API namespace to the gate, so any
    // authenticated agent passed it. The git owner is now judged, and the
    // route words are not usable as owner names at all.
    #[tokio::test]
    async fn api_prefixed_git_path_is_gated_as_git() {
        let (_td, svc) = private_svc();
        for p in [
            "/forge/api/x.git/info/refs",
            "/forge/api/x.git/git-upload-pack",
            "/forge/xlogin.js/x.git/info/refs",
        ] {
            let r = status(&svc, "GET", p, "service=git-upload-pack", dana()).await;
            assert_eq!(r.status, 403, "{p}");
            let r = status(
                &svc,
                "GET",
                p,
                "service=git-upload-pack",
                ForgeAgent::Anonymous,
            )
            .await;
            assert_eq!(r.status, 401, "{p}");
        }
        assert!(!crate::guard::ok_owner("api"));
        assert!(!crate::guard::ok_owner("Api"));
        assert!(!crate::guard::ok_owner("xlogin.js"));
        assert!(!crate::compare::valid_owner("api"));
    }

    // JSS "the repo list ?q= filter is a plain server-side GET form (no JS)"
    #[tokio::test]
    async fn repo_list_q_filters_by_name_and_description() {
        let td = tempfile::TempDir::new().unwrap();
        demo_repo(td.path());
        let other = td.path().join("repos/aaa/nrepo.git");
        std::fs::create_dir_all(other.parent().unwrap()).unwrap();
        crate::gitrun::testutil::git(
            td.path(),
            &["init", "--bare", "--quiet", other.to_str().unwrap()],
        );
        let svc = ForgeService::new(ForgeConfig::default(), td.path()).unwrap();
        std::fs::write(
            td.path().join("repos/casey/demo.git/description"),
            "A demo repo with <angle> brackets\n",
        )
        .unwrap();
        let page = |q: &str| {
            let svc = svc.clone();
            let q = q.to_string();
            async move {
                let r = svc
                    .handle(
                        rq("GET", "/forge/", &format!("q={q}"), ""),
                        ForgeAgent::Anonymous,
                    )
                    .await
                    .unwrap();
                String::from_utf8_lossy(&r.body).into_owned()
            }
        };
        let h = page("nrepo").await;
        assert!(h.contains("name=\"q\""));
        assert!(h.contains("/nrepo"));
        assert!(!h.contains("/forge/casey/demo\""));
        let h = page("angle").await;
        assert!(h.contains("/forge/casey/demo"));
        assert!(
            h.contains("A demo repo with &lt;angle&gt; brackets"),
            "description on the repo list, escaped"
        );
        assert!(page("zzz-nothing-here")
            .await
            .contains("No repositories match"));
    }
}