upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/docs/archive/2025-11-04-evening/2025-11-04-git-http-backend-deep-dive.md
blob: 26d0526bf5daed6e6e3c061af6c231b3c81aa684 (plain)
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
**ARCHIVED: 2025-11-04**  
**Reason:** Analysis complete, crate validated  
**Outcome:** Confirmed suitable for use (with fork for authorization)

---

# git-http-backend Crate Deep Dive

**Date:** 2025-11-04  
**Status:** ✅ ARCHIVED - Analysis Complete  
**Purpose:** Validate the recommendation in `work/current_status.md` regarding git-http-backend crate

---

## Executive Summary

**Recommendation Status:** ✅ **VALIDATED WITH CAVEATS**

The `git-http-backend` crate (v0.1.3) is a **good foundation** but requires significant customization for our inline authorization needs. The hybrid approach recommended in `current_status.md` is sound, but we'll need to:

1. **Fork or vendor** the crate for customization
2. **Add interception points** for authorization
3. **Enhance error handling** for better push rejection messages
4. **Add CORS support** (missing from current implementation)

---

## Crate Overview

### Basic Info
- **Name:** `git-http-backend`
- **Version:** 0.1.3
- **Author:** lazhenyi
- **License:** MIT
- **Repository:** https://github.com/lazhenyi/git-http-backend
- **Documentation:** https://docs.rs/git-http-backend/0.1.3

### Dependencies
```toml
tokio = { version = "1", features = ["sync","macros","rt", "rt-multi-thread","net"] }
actix-web = { version = "4.9.0", features = ["default"] }
actix-files = { version = "0.6.6", features = ["actix-server"] }
futures-util = { version = "0.3.31", features = ["futures-channel"] }
flate2 = "1.0.35"           # Gzip compression
async-stream = "0.3.6"       # Streaming responses
async-trait = "0.1.83"       # Async trait support
```

**Good news:** Already uses actix-web 4.9.0 (same as we plan to use)

---

## Architecture Analysis

### Core Design

The crate provides:

1. **GitConfig Trait** - Path rewriting abstraction
2. **Actix Router** - Pre-configured routes for Git Smart HTTP
3. **Protocol Handlers** - Upload-pack, receive-pack, info/refs
4. **System Git Integration** - Spawns `git` subprocess

### URL Structure

```
/{namespace}/{repo}/info/refs?service=git-upload-pack
/{namespace}/{repo}/git-upload-pack
/{namespace}/{repo}/git-receive-pack
/{namespace}/{repo}/HEAD
/{namespace}/{repo}/objects/info/packs
/{namespace}/{repo}/objects/pack/{pack}
```

**Perfect match** for our `/{npub}/{identifier}.git/` structure!

### Request Flow

```
HTTP Request
    ↓
Actix Router → Handler Function
    ↓
GitConfig::rewrite() → Path resolution
    ↓
Spawn git subprocess (upload-pack/receive-pack)
    ↓
Stream response back to client
```

---

## Key Handlers Analysis

### 1. info/refs Handler (refs.rs)

**Purpose:** Advertise repository refs (clone/fetch discovery)

**Flow:**
1. Parse `service` query param (upload-pack or receive-pack)
2. Resolve repository path via `GitConfig::rewrite()`
3. Spawn `git upload-pack --advertise-refs --stateless-rpc .`
4. Return with proper content-type header

**Code:**
```rust
pub async fn info_refs(request: HttpRequest, service: web::Data<impl GitConfig>) -> impl Responder {
    let uri = request.uri();
    let path = uri.path().to_string().replace("/info/refs", "");
    let path = service.rewrite(path).await;
    
    // Parse service from query
    let service = query.split('=').map(|x| x.to_string()).collect::<Vec<_>>()[1].clone();
    
    // Spawn git
    let mut cmd = Command::new("git");
    cmd.arg(service_name.clone());
    cmd.arg("--stateless-rpc");
    cmd.arg("--advertise-refs");
    cmd.arg(".");
    cmd.current_dir(path);
    
    // Return response with proper headers
    resp.append_header(("Content-Type", format!("application/x-git-{}-advertisement", service_name)));
    resp.append_header(("Cache-Control", "no-cache, max-age=0, must-revalidate"));
}
```

**Good:**
- ✅ Proper content-type headers
- ✅ Cache control headers
- ✅ Git protocol version support (Git-Protocol header)

**Issues:**
- ❌ No CORS headers
- ❌ No error handling for missing repos
- ❌ Query parsing is fragile (will panic on malformed input)

### 2. git-upload-pack Handler (git_upload_pack.rs)

**Purpose:** Handle clone/fetch operations (read-only)

**Flow:**
1. Resolve repository path
2. Read request body (may be gzipped)
3. Spawn `git upload-pack --stateless-rpc .`
4. Stream response back

**Code:**
```rust
pub async fn git_upload_pack(
    request: HttpRequest,
    mut payload: Payload,
    service: web::Data<impl GitConfig>,
) -> impl Responder {
    // Resolve path
    let path = service.rewrite(path).await;
    
    // Spawn git
    let mut cmd = Command::new("git");
    cmd.arg("upload-pack");
    cmd.arg("--stateless-rpc");
    cmd.arg(".");
    cmd.current_dir(path);
    
    let mut span = cmd.spawn()?;
    let mut stdin = span.stdin.take().unwrap();
    let mut stdout = span.stdout.take().unwrap();
    
    // Read request body
    let mut bytes = web::BytesMut::new();
    while let Some(chunk) = payload.next().await {
        bytes.extend_from_slice(&data);
    }
    
    // Handle gzip
    let body_data = match encoding {
        Some("gzip") => decode_gzip(bytes),
        _ => bytes.to_vec(),
    };
    
    // Write to git stdin
    stdin.write_all(&body_data)?;
    drop(stdin);
    
    // Stream response
    let body_stream = actix_web::body::BodyStream::new(async_stream::stream! {
        let mut buffer = [0; 8192];
        loop {
            match stdout.read(&mut buffer) {
                Ok(0) => break,
                Ok(n) => yield Ok(web::Bytes::copy_from_slice(&buffer[..n])),
                Err(e) => break,
            }
        }
    });
    resp.body(body_stream)
}
```

**Good:**
- ✅ Handles gzip compression
- ✅ Streams response (efficient for large repos)
- ✅ Proper content-type headers

**Issues:**
- ❌ No CORS headers
- ❌ No repository existence check
- ❌ Error handling uses eprintln! (not tracing)

**For our use:** Upload-pack is read-only, so we can use as-is (just add CORS)

### 3. git-receive-pack Handler (git_receive_pack.rs) ⚠️

**Purpose:** Handle push operations (write)

**This is the critical handler for inline authorization!**

**Current Flow:**
1. Resolve repository path
2. **Check if bare repository** (good!)
3. Read request body (may be gzipped)
4. Spawn `git receive-pack --stateless-rpc .`
5. Stream response back

**Code:**
```rust
pub async fn git_receive_pack(
    request: HttpRequest,
    mut payload: Payload,
    service: web::Data<impl GitConfig>,
) -> impl Responder {
    let path = service.rewrite(path).await;
    
    // Check repository exists
    if !path.join("HEAD").exists() || !path.join("config").exists() {
        return HttpResponse::BadRequest().body("Repository not found or invalid.");
    }

    // Check if bare
    let is_bare_repo = match std::fs::read_to_string(path.join("config")) {
        Ok(config) => config.contains("bare = true"),
        Err(_) => false,
    };
    if !is_bare_repo {
        return HttpResponse::BadRequest().body("Push operation requires a bare repository.");
    }
    
    // Spawn git receive-pack
    let mut cmd = Command::new("git");
    cmd.arg("receive-pack");
    cmd.arg("--stateless-rpc");
    cmd.arg(".");
    cmd.current_dir(&path);
    
    let mut git_process = cmd.spawn()?;
    let mut stdin = git_process.stdin.take().unwrap();
    let mut stdout = git_process.stdout.take().unwrap();
    
    // Read request body
    let mut bytes = web::BytesMut::new();
    while let Some(chunk) = payload.next().await {
        bytes.extend_from_slice(&data);
    }
    
    // Decode if gzipped
    let body_data = match encoding {
        Some(encoding) if encoding.contains("gzip") => decode_gzip(bytes),
        _ => bytes.to_vec(),
    };
    
    // Write to git stdin
    stdin.write_all(&body_data)?;
    drop(stdin);
    
    // Stream response
    let body_stream = /* stream stdout */;
    resp.body(body_stream)
}
```

**Good:**
- ✅ Validates repository exists
- ✅ Validates bare repository
- ✅ Handles gzip compression
- ✅ Streams response

**Critical Issues for Our Use:**
- ❌ **No authorization hook!** Spawns git immediately
- ❌ **No way to inspect push data** before spawning git
- ❌ **No CORS headers**
- ❌ **Can't reject unauthorized pushes** with custom error

**This is where we need customization!**

---

## Customization Requirements

### 1. Authorization Interception Point

**Need to add BEFORE spawning git:**

```rust
pub async fn git_receive_pack(
    request: HttpRequest,
    mut payload: Payload,
    service: web::Data<impl GitConfig>,
    validator: web::Data<PushValidator>,  // ← ADD THIS
) -> impl Responder {
    let path = service.rewrite(path).await;
    
    // Existing checks...
    
    // Read request body
    let body_data = read_and_decode_body(&mut payload, &request).await?;
    
    // ← ADD AUTHORIZATION HERE
    let ref_updates = parse_receive_pack_request(&body_data)?;
    
    // Extract npub and identifier from path
    let (npub, identifier) = extract_repo_info(&request.uri().path())?;
    
    // Validate against Nostr state
    if let Err(e) = validator.validate_push(&npub, &identifier, &ref_updates).await {
        return HttpResponse::Forbidden()
            .json(json!({
                "error": "unauthorized",
                "message": e.to_string(),
                "ref_updates": ref_updates,
            }));
    }
    
    // Only spawn git if authorized
    let mut cmd = Command::new("git");
    // ... rest of existing code
}
```

### 2. Parse Git Protocol

**Need to add protocol parsing:**

```rust
// src/git/protocol.rs

pub struct RefUpdate {
    pub old_oid: String,
    pub new_oid: String,
    pub ref_name: String,
}

pub fn parse_receive_pack_request(body: &[u8]) -> Result<Vec<RefUpdate>> {
    // Parse git pack protocol
    // Format: <old-oid> <new-oid> <ref-name>\0<capabilities>\n
    // Example: 0000000000000000000000000000000000000000 a1b2c3d4... refs/heads/main\0 report-status\n
    
    let mut updates = Vec::new();
    let lines = body.split(|&b| b == b'\n');
    
    for line in lines {
        if line.is_empty() {
            continue;
        }
        
        // Parse pkt-line format
        // First 4 bytes are hex length
        let pkt_len = parse_pkt_len(&line[0..4])?;
        if pkt_len == 0 {
            continue; // flush packet
        }
        
        let data = &line[4..pkt_len];
        let parts: Vec<&[u8]> = data.splitn(3, |&b| b == b' ').collect();
        
        if parts.len() >= 3 {
            let old_oid = String::from_utf8_lossy(parts[0]).to_string();
            let new_oid = String::from_utf8_lossy(parts[1]).to_string();
            
            // Ref name may have capabilities after \0
            let ref_data = parts[2];
            let ref_name = if let Some(null_pos) = ref_data.iter().position(|&b| b == b'\0') {
                String::from_utf8_lossy(&ref_data[..null_pos]).to_string()
            } else {
                String::from_utf8_lossy(ref_data).to_string()
            };
            
            updates.push(RefUpdate {
                old_oid,
                new_oid,
                ref_name,
            });
        }
    }
    
    Ok(updates)
}
```

**Note:** Git pack protocol is complex. We may want to use a library for this:
- `git2` crate has protocol parsing
- Or we can implement minimal parsing for our needs

### 3. Add CORS Support

**Need to add to all handlers:**

```rust
// Add CORS middleware or headers to all responses
resp.append_header(("Access-Control-Allow-Origin", "*"));
resp.append_header(("Access-Control-Allow-Methods", "GET, POST, OPTIONS"));
resp.append_header(("Access-Control-Allow-Headers", "Content-Type, Git-Protocol"));
```

### 4. Better Error Handling

**Replace eprintln! with tracing:**

```rust
use tracing::{error, info, debug};

// Instead of:
eprintln!("Error running command: {}", e);

// Use:
error!(error = ?e, "Failed to spawn git process");
```

---

## Integration Strategy

### Option A: Fork the Crate ✅ RECOMMENDED

**Pros:**
- Full control over authorization logic
- Can add CORS, error handling, protocol parsing
- Can publish as `ngit-grasp-git-http-backend`
- Keep upstream changes visible

**Cons:**
- Need to maintain fork
- Diverges from upstream

**Implementation:**
1. Fork https://github.com/lazhenyi/git-http-backend
2. Add to our workspace as git submodule or copy
3. Modify `git_receive_pack.rs` to add authorization
4. Add protocol parsing module
5. Add CORS support
6. Improve error handling

### Option B: Vendor the Code

**Pros:**
- Complete control
- No external dependency
- Can heavily customize

**Cons:**
- Lose upstream updates
- More code to maintain

**Implementation:**
1. Copy source into `src/git/http_backend/`
2. Modify as needed
3. No external dependency

### Option C: Wrap the Crate

**Pros:**
- Keep upstream crate
- Add authorization via middleware

**Cons:**
- ❌ **Can't intercept before git spawns!**
- Would need to parse response, too late
- Complex to inject validator

**Not recommended** - can't achieve inline authorization

---

## Recommended Approach

### Use Forked git-http-backend + git2 + System Git

**Architecture:**

```
HTTP Request
    ↓
Actix Router (from forked git-http-backend)
    ↓
Custom GitConfig Implementation
    ↓
git_receive_pack Handler (MODIFIED)
    ↓
┌─────────────────────────────────┐
│ 1. Read request body            │
│ 2. Parse ref updates (protocol) │  ← ADD THIS
│ 3. Validate via PushValidator   │  ← ADD THIS
│    ├─ Query Nostr relay         │
│    ├─ Check state event         │
│    └─ Validate maintainers      │
│ 4. If authorized:               │
│    └─ Spawn git receive-pack    │  ← EXISTING
│ 5. If unauthorized:             │
│    └─ Return 403 with error     │  ← ADD THIS
└─────────────────────────────────┘
    ↓
Stream response to client
```

**Dependencies:**

```toml
[dependencies]
# Fork of git-http-backend (or vendored code)
git-http-backend = { git = "https://github.com/our-org/git-http-backend", branch = "ngit-grasp" }

# Or vendor it:
# (no dependency, code in src/git/http_backend/)

# Git operations
git2 = "0.20"  # For repository management, ref queries

# Already have:
actix-web = "4.9"
tokio = { version = "1", features = ["full"] }
nostr-sdk = "0.43"
```

**Implementation Plan:**

1. **Phase 1: Fork & Setup**
   - Fork git-http-backend
   - Add to our project (git submodule or copy)
   - Verify existing functionality works

2. **Phase 2: Protocol Parsing**
   - Add `src/git/protocol.rs`
   - Implement `parse_receive_pack_request()`
   - Unit tests for protocol parsing

3. **Phase 3: Authorization Integration**
   - Modify `git_receive_pack.rs`
   - Add `PushValidator` parameter
   - Call validator before spawning git
   - Return 403 on unauthorized

4. **Phase 4: CORS & Polish**
   - Add CORS headers to all handlers
   - Improve error messages
   - Add tracing instead of eprintln!

5. **Phase 5: Testing**
   - Unit tests for authorization
   - Integration tests with real git
   - GRASP-01 compliance tests

---

## Validation of current_status.md Recommendations

### Hybrid Approach ✅ VALIDATED

**Original recommendation:**
> 1. **git-http-backend** - HTTP protocol handling
> 2. **git2-rs** - Repository management, ref validation
> 3. **System git** - Actual pack operations (upload-pack/receive-pack)

**Analysis:**
- ✅ **git-http-backend** - Good foundation, needs customization
- ✅ **git2** - Perfect for repo management (init, refs, validation)
- ✅ **System git** - Proven pack protocol implementation

**Verdict:** Sound approach, but need to fork/vendor git-http-backend

### Tool Selection ✅ CORRECT

**Original analysis:**
- git2 for repository management ✅
- System git for pack operations ✅
- git-http-backend for HTTP layer ✅ (with modifications)

**Additional findings:**
- Need protocol parsing (can use git2 or implement minimal)
- Need CORS support (add to fork)
- Need better error handling (add to fork)

### Inline Authorization ✅ ACHIEVABLE

**Original goal:**
> We intercept the `git-receive-pack` operation before spawning the Git process

**Analysis:**
- ✅ Possible by modifying `git_receive_pack.rs`
- ✅ Can parse request body before spawning git
- ✅ Can return 403 before git touches repository

**Requirement:**
- Must fork or vendor git-http-backend
- Can't achieve with unmodified crate

---

## Updated Implementation Plan

### Week 1: Foundation (UPDATED)

1. ✅ Add git2 dependency
2. **Fork git-http-backend** (NEW)
3. **Add protocol parsing** (NEW)
4. Implement GitRepository (Phase 1)
5. Write unit tests for repository operations
6. Test repository creation from announcements

### Week 2: Protocol & Authorization

1. Implement protocol parsing (Phase 2)
2. Implement authorization logic (Phase 3)
3. **Modify git_receive_pack handler** (NEW)
4. Write unit tests for both
5. Integration tests for validation

### Week 3: HTTP & Integration

1. **Add CORS support to fork** (NEW)
2. Implement HTTP handlers (Phase 4)
3. Integrate with Nostr events (Phase 5)
4. Integration tests for full flow
5. Error handling improvements

### Week 4: E2E & Polish

1. E2E tests with real git (Phase 6)
2. Performance testing
3. GRASP-01 compliance testing
4. Documentation and examples

---

## Risks & Mitigations

### Risk 1: Fork Maintenance

**Risk:** Fork diverges from upstream, miss updates

**Mitigation:**
- Keep fork minimal (only modify git_receive_pack.rs)
- Document all changes clearly
- Consider upstreaming authorization hooks
- Monitor upstream for security fixes

### Risk 2: Protocol Parsing Complexity

**Risk:** Git pack protocol is complex, may miss edge cases

**Mitigation:**
- Use git2 for protocol parsing if available
- Implement minimal parsing (just ref updates)
- Extensive testing with real git clients
- Refer to Git protocol documentation

### Risk 3: Performance

**Risk:** Authorization adds latency to push operations

**Mitigation:**
- Keep validation logic fast (< 100ms target)
- Cache state events in memory
- Async validation (don't block)
- Profile and optimize

---

## Conclusion

### Summary

The **hybrid approach** recommended in `current_status.md` is **sound and validated**, with these adjustments:

1. **Fork or vendor git-http-backend** - Can't use unmodified crate
2. **Add protocol parsing** - Need to parse ref updates from request
3. **Modify git_receive_pack handler** - Add authorization before spawning git
4. **Add CORS support** - Missing from current implementation
5. **Improve error handling** - Better messages for push rejections

### Next Steps

1. ✅ **Review this analysis** - Confirm approach
2. **Fork git-http-backend** - Set up fork/vendor
3. **Start Phase 1** - Add git2, implement GitRepository
4. **Add protocol parsing** - Parse ref updates from pack protocol
5. **Modify receive-pack handler** - Add authorization logic

### Questions for Review

1. **Fork vs. Vendor?** Fork allows upstream tracking, vendor gives full control
2. **Protocol parsing?** Use git2 or implement minimal parser?
3. **CORS scope?** Support all origins or restrict?
4. **Error detail?** How much info to expose in 403 responses?
5. **Performance target?** Is < 100ms for auth validation reasonable?

---

**Status:** ✅ Analysis complete, ready to proceed with implementation

**Recommendation:** Fork git-http-backend, add authorization to git_receive_pack, use git2 for repo management

---

*Analysis Date: November 4, 2025*