oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1package docs
2
3import (
4 "errors"
5 "html/template"
6 "net/http"
7
8 "amadan.net/rastrillo/rastrillo"
9 "gorm.io/gorm"
10)
11
12// publicPage is GET /p/{token}'s view data: a document, and nothing about
13// who can reach it.
14//
15// Compare showPage, which carries Sqid, an editor script, save-status copy
16// and a share control. None of that is here, and the absence is the
17// feature — this struct is the list of things a stranger is allowed to
18// know, so anything added to it is a decision rather than an oversight.
19//
20// Sqid in particular is absent on purpose. It is the private route's key,
21// and printing it on a page anyone can read would hand a stranger the URL
22// the signed-in app is organised around.
23type publicPage struct {
24 Frame Frame
25 Title string
26 HTML template.HTML
27
28 // ReadOnly is the standing notice that this is a published copy. It
29 // is authored copy rather than a rendering decision, so it goes
30 // through the catalog like every other string here.
31 ReadOnly string
32}
33
34// publicShow serves GET /p/{token}, the one route in this app that no
35// session guards.
36//
37// It resolves the token to a document and renders it. There is no branch
38// on who is asking, because a capability does not have a who: a signed-in
39// owner following their own link gets the same page a stranger does, which
40// is what makes the control testable by the person who turned it on.
41//
42// Every failure is 404 (§4.2 reaching the public route): an unknown token,
43// a revoked one, and a token whose document was deleted are one answer, or
44// the differences between them tell a stranger which documents exist and
45// which were published and then withdrawn.
46func (h *handlers) publicShow(w http.ResponseWriter, r *http.Request) {
47 token := r.PathValue("token")
48 doc, err := h.store.ByToken(r.Context(), token)
49 if err != nil {
50 if errors.Is(err, gorm.ErrRecordNotFound) {
51 h.fail(w, r, http.StatusNotFound, failDocument)
52 return
53 }
54 h.logger.Error("docs: resolve public link", "err", err)
55 h.fail(w, r, h.internalFailureStatus(), failStatus)
56 return
57 }
58
59 html, err := h.store.HTML(r.Context(), doc, publicView(doc, token))
60 if err != nil {
61 h.logger.Error("docs: public document html", "err", err)
62 h.fail(w, r, h.internalFailureStatus(), failStatus)
63 return
64 }
65 html = labelMissingImages(r, html)
66
67 // no-store, said rather than implied. The first version of this
68 // handler set no header at all and its comment claimed that kept a
69 // shared cache from retaining a revoked copy — which is not what an
70 // absent Cache-Control means. Absent leaves it to heuristics; a
71 // revoked link's page could sit in an intermediary. stream.go and
72 // search.go already say no-store for exactly this reason.
73 //
74 // X-Robots-Tag because "anyone with the link" is not "anyone": a
75 // capability that leaks into a crawlable place should not also become
76 // a search result. It is not a security control — the link is the
77 // authorization and a crawler that has it can read the page — it is a
78 // refusal to help the leak spread.
79 w.Header().Set("Cache-Control", "no-store")
80 w.Header().Set("X-Robots-Tag", "noindex, nofollow")
81 if err := h.render.page(w, r, "public", publicPage{
82 Frame: h.frame(r, frameOptions{titleParts: []string{doc.Title}, context: doc.Title, public: true}),
83 Title: doc.Title,
84 HTML: html,
85 ReadOnly: rastrillo.T(r, "docs.public.readonly"),
86 }); err != nil {
87 h.logger.Error("docs: render public document", "err", err)
88 h.fail(w, r, h.internalFailureStatus(), failStatus)
89 }
90}
91
92// shareLink serves POST /d/{sqid}/share/link: the owner's switch.
93//
94// One route for both directions rather than two, because the form is a
95// switch and its current state decides which way it points — two routes
96// would let a stale page turn something on that the owner had just turned
97// off, from a button that still said "off".
98//
99// Owner-only through Publish/Unpublish's own Get. A writer grantee is
100// answered 404: publishing to everyone is the largest re-share there is,
101// and no grant carries re-sharing.
102func (h *handlers) shareLink(w http.ResponseWriter, r *http.Request) {
103 // Member-only, as the other two share routes are. Publishing to the
104 // whole internet is the largest re-share there is.
105 if !h.requireMemberRequest(w, r, true) {
106 return
107 }
108 owner, err := h.owner(r)
109 if err != nil {
110 h.logger.Error("docs: resolve owner for publish", "err", err)
111 h.fail(w, r, h.internalFailureStatus(), failStatus)
112 return
113 }
114 sqid := r.PathValue("sqid")
115
116 on := r.PostFormValue("on") == "1"
117 if on {
118 var token string
119 token, err = h.store.Publish(r.Context(), owner, sqid)
120 if err == nil {
121 // The only moment this URL exists outside the token itself.
122 // Parked for exactly one render (oneshot.go) and never
123 // written down, because the database holds only a hash.
124 h.oneShots.put(oneShotKey(owner, sqid), h.origin+"/p/"+token)
125 }
126 } else {
127 err = h.store.Unpublish(r.Context(), owner, sqid)
128 }
129 switch {
130 case err == nil:
131 if wantsNative(r) {
132 nativeJSON(w, r, 200, map[string]any{"published": on, "public_url": h.oneShots.take(oneShotKey(owner, sqid))})
133 return
134 }
135 http.Redirect(w, r, shareBack(r, sqid), http.StatusSeeOther)
136 case errors.Is(err, gorm.ErrRecordNotFound):
137 h.fail(w, r, http.StatusNotFound, failDocument)
138 default:
139 h.logger.Error("docs: set public link", "sqid", sqid, "on", on, "err", err)
140 h.fail(w, r, h.internalFailureStatus(), failStatus)
141 }
142}
143