Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Clone

Read only, no credentials:

https://rodent.software/git/vole.git

Write over SSH with a registered key:

git clone ssh://git@0.0.0.0:2222/vole.git

A free and open-source forge that lives in your Git repo.

Reorganise the web package around one implementation per rule

c717d4eb92b4257b05fa065918b0071f6302f53e · Keenan Nemetz · 13d ago · parent 4506a4d3

Reorganise the web package around one implementation per rule

server/web had grown by feature, so several rules and widgets existed twice: the
comment stack for issues and change requests, the pager, the file tree, the write
preamble, and the ACL predicates. Each pair agreed today and would drift
tomorrow. No route, stored format, or permission rule changes; the few behaviour
differences found along the way are listed at the end.

Files now say what they answer:

  web.go          the Web type, render, open, the error pages
  page.go         Page, pageSection, who, nav
  templatefuncs.go the template function map, with the escaping rules noted
  ui.go           relative time, avatars, breadcrumbs, the pager
  markdown.go     the goldmark and bluemonday boundary
  codebrowser.go  the code browser, out of web.go
  tokens.go       the label/assignee token input
  rolepicker.go   the role control, out of admin.go
  comments.go     the comment view, thread builder, action menus, snippet
  comment_writes.go  add, edit and delete a comment, for both surfaces
  write_context.go   beginWrite/beginPost/beginAdminPost, fail, refuse
  capabilities.go    the ACL rules a page and a handler share
  audit.go        the audit log
  request_view.go the change conversation page, out of requests.go
  admin_access.go the instance-admin access rules, out of settings.go

A comment is one thing, so it has one view and one thread builder: metadata.Comment
is the same type on both surfaces and only a change request can anchor one to a
diff line. Adding, editing and deleting are one implementation each, with a
commentTopic naming what differs per surface. The pager is one control for issues,
changes and the instance feed; the file tree is one widget for the code browser
and a change's Files tab.

Every write starts at beginWrite (session, body bound, CSRF, repository, rules,
form) and asks capabilities.go for the one rule it needs, so the control a page
draws and the check a handler makes cannot disagree. A rejected write answers
through refuse; a write that fails for another reason answers through fail, which
re-renders the section for HTMX and returns text otherwise.

Templates follow the same split: chrome.html, components.html, diff.html and
role-picker.html hold what more than one surface renders, and each page owns its
fragments. The route table moved to routes.go, one function per surface.

Behaviour changes found while unifying:

  - A reply cycle (two comments answering each other, possible through a pushed
    metadata branch) used to drop both comments from the page; anything the walk
    does not reach is now emitted as a root.
  - A rejected anchored comment keeps its "Commenting on path:line" note and
    hidden anchor fields, so the reviewer does not have to pick the line again.
  - A repository whose access rules cannot be read answers 500 rather than 403;
    the account was being blamed for a broken instance.
  - Pager wording is now "Showing a-b of n / page x of y" with Previous/Next
    everywhere, and the instance feed's page links keep their kind filter.
  - The activity feed, issue list and change list share one page control.

Docs: docs/ui-plan.md gains a "Where the code lives" map, the write gate, the
shared comment model, the route table, and rules for rejected writes, shared
template fragments, and the stylesheet's section order. The passkey script is now
an ordinary asset (static/auth.js) under the same CSP as the rest.

server/web/auth.go +1−53

@@ -335,13 +335,7 @@ func (w *Web) accountName(r *http.Request) string {
335 335 }
336 336
337 337 func (w *Web) canDiscuss(r *http.Request, acl *access.ACL) bool {
338 name := w.accountName(r)
339 return name != "" && acl.CanDiscuss(name)
340 }
341
342 func (w *Web) canApprove(r *http.Request, acl *access.ACL, author string) bool {
343 name := w.accountName(r)
344 return name != "" && name != author && acl.CanApprove(name)
338 return canDiscuss(acl, w.accountName(r))
345 339 }
346 340
347 341 func (w *Web) page(r *http.Request, title string) Page {
@@ -627,49 +621,3 @@ func decodeJSONBody(r *http.Request, dst any) error {
627 621 }
628 622 return nil
629 623 }
630
631 const authJS = `(function(){
632 const csrf = (document.querySelector('meta[name="csrf"]') || {}).content || '';
633 function b64(s){s=s.replace(/-/g,'+').replace(/_/g,'/');while(s.length%4)s+='=';const bin=atob(s);const out=new Uint8Array(bin.length);for(let i=0;i<bin.length;i++)out[i]=bin.charCodeAt(i);return out}
634 function enc(buf){const b=new Uint8Array(buf);let s='';for(let i=0;i<b.length;i++)s+=String.fromCharCode(b[i]);return btoa(s).replace(/\+/g,'-').replace(/\//g,'_').replace(/=+$/,'')}
635 function prepCreate(o){o.publicKey.challenge=b64(o.publicKey.challenge);o.publicKey.user.id=b64(o.publicKey.user.id);(o.publicKey.excludeCredentials||[]).forEach(c=>c.id=b64(c.id));return o.publicKey}
636 function prepGet(o){o.publicKey.challenge=b64(o.publicKey.challenge);(o.publicKey.allowCredentials||[]).forEach(c=>c.id=b64(c.id));return o.publicKey}
637 function resp(c){const r=c.response;const out={id:c.id,rawId:enc(c.rawId),type:c.type,clientExtensionResults:c.getClientExtensionResults()};if(r.attestationObject){out.response={clientDataJSON:enc(r.clientDataJSON),attestationObject:enc(r.attestationObject)};if(r.getTransports)out.response.transports=r.getTransports()}else{out.response={clientDataJSON:enc(r.clientDataJSON),authenticatorData:enc(r.authenticatorData),signature:enc(r.signature)};if(r.userHandle)out.response.userHandle=enc(r.userHandle)}if(c.authenticatorAttachment)out.authenticatorAttachment=c.authenticatorAttachment;return out}
638 async function post(url,body,withCSRF){const h={'Content-Type':'application/json'};if(withCSRF)h['X-Vole-CSRF']=csrf;const r=await fetch(url,{method:'POST',headers:h,body:JSON.stringify(body),credentials:'same-origin'});let d={};try{d=await r.json()}catch(e){}if(!r.ok)throw new Error(d.error||r.statusText);return d}
639 function fail(form,err){const e=form.querySelector('[data-auth-error]')||document.querySelector('[data-auth-error]');if(e)e.textContent=err.message||String(err);else alert(err.message||String(err))}
640 document.querySelectorAll('form[data-auth]').forEach(function(form){
641 form.addEventListener('submit',async function(ev){
642 ev.preventDefault();const btn=form.querySelector('button[type=submit]');if(btn)btn.disabled=true;
643 try{
644 if(form.dataset.auth==='login'){
645 const b=await post('/auth/login/begin',{username:form.username.value.trim()});
646 const c=await navigator.credentials.get({publicKey:prepGet(b)});
647 await post('/auth/login/finish',resp(c));
648 location.href=form.dataset.next||'/';
649 }else if(form.dataset.auth==='invite'){
650 const b=await post('/auth/invite/begin',{token:form.dataset.token});
651 const c=await navigator.credentials.create({publicKey:prepCreate(b)});
652 await post('/auth/invite/finish',resp(c));
653 location.href='/';
654 }else if(form.dataset.auth==='register'){
655 const b=await post('/auth/register/begin',{username:form.username.value.trim()});
656 const c=await navigator.credentials.create({publicKey:prepCreate(b)});
657 await post('/auth/register/finish',resp(c));
658 location.href='/';
659 }else if(form.dataset.auth==='passkey'){
660 const b=await post('/account/passkeys/begin',{label:form.label.value.trim()},true);
661 const c=await navigator.credentials.create({publicKey:prepCreate(b)});
662 await post('/account/passkeys/finish',resp(c),true);
663 location.reload();
664 }
665 }catch(err){fail(form,err);if(btn)btn.disabled=false}
666 })
667 })
668 })();`
669
670 // AuthJS is the only script the CSP allows: same-origin, no eval, no CDN.
671 func (w *Web) AuthJS(rw http.ResponseWriter, r *http.Request) {
672 rw.Header().Set("Content-Type", "application/javascript; charset=utf-8")
673 rw.Header().Set("Cache-Control", "no-cache")
674 _, _ = rw.Write([]byte(authJS))
675 }