Skip to content

Commit c89d350

Browse files
committed
disallow accidental updates to existing links
Prior to #177, our XSRF tokens were bound to link IDs, with a special `.new` value used for newly created links. So if a user tried to create a link that already existed, the XSRF check would fail. After #177, this now silently allows the user to overwrite the existing link without any indication that this happened. This change adds a hidden `update` param to the details edit form that must be present when updating an existing link. Updates #177 Change-Id: Ia101a4a3005adb9118051b3416f5a64a4a45987d Signed-off-by: Will Norris <[email protected]>
1 parent 69257dd commit c89d350

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

golink.go

+7
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,13 @@ func serveSave(w http.ResponseWriter, r *http.Request) {
881881
return
882882
}
883883

884+
// Prevent accidental overwrites of existing links.
885+
// If the link already exists, make sure this request is an intentional update.
886+
if link != nil && r.FormValue("update") == "" {
887+
http.Error(w, "link already exists", http.StatusForbidden)
888+
return
889+
}
890+
884891
if !canEditLink(r.Context(), link, cu) {
885892
http.Error(w, fmt.Sprintf("cannot update link owned by %q", link.Owner), http.StatusForbidden)
886893
return

golink_test.go

+23-2
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ func TestServeSave(t *testing.T) {
230230
name string
231231
short string
232232
long string
233+
update bool
233234
allowUnknownUsers bool
234235
currentUser func(*http.Request) (user, error)
235236
wantStatus int
@@ -252,6 +253,19 @@ func TestServeSave(t *testing.T) {
252253
long: "http://who/",
253254
wantStatus: http.StatusOK,
254255
},
256+
{
257+
name: "disallow accidental updates",
258+
short: "who",
259+
long: "http://who2/",
260+
wantStatus: http.StatusForbidden,
261+
},
262+
{
263+
name: "allow intentional updates",
264+
short: "who",
265+
long: "http://who/",
266+
update: true,
267+
wantStatus: http.StatusOK,
268+
},
255269
{
256270
name: "disallow editing another's link",
257271
short: "who",
@@ -263,13 +277,15 @@ func TestServeSave(t *testing.T) {
263277
name: "allow editing link owned by tagged-devices",
264278
short: "link-owned-by-tagged-devices",
265279
long: "/after",
280+
update: true,
266281
currentUser: func(*http.Request) (user, error) { return user{login: "[email protected]"}, nil },
267282
wantStatus: http.StatusOK,
268283
},
269284
{
270285
name: "admins can edit any link",
271286
short: "who",
272287
long: "http://who/",
288+
update: true,
273289
currentUser: func(*http.Request) (user, error) { return user{login: "[email protected]", isAdmin: true}, nil },
274290
wantStatus: http.StatusOK,
275291
},
@@ -304,10 +320,15 @@ func TestServeSave(t *testing.T) {
304320
*allowUnknownUsers = tt.allowUnknownUsers
305321
t.Cleanup(func() { *allowUnknownUsers = oldAllowUnknownUsers })
306322

307-
r := httptest.NewRequest("POST", "/", strings.NewReader(url.Values{
323+
v := url.Values{
308324
"short": {tt.short},
309325
"long": {tt.long},
310-
}.Encode()))
326+
}
327+
if tt.update {
328+
v.Set("update", "1")
329+
}
330+
331+
r := httptest.NewRequest("POST", "/", strings.NewReader(v.Encode()))
311332
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
312333
w := httptest.NewRecorder()
313334
serveSave(w, r)

tmpl/detail.html

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ <h2 class="text-xl font-bold pb-2">Link Details</h2>
2626
<dd>{{.Link.LastEdit.Format "Jan _2, 2006 3:04pm MST"}}</dd>
2727
</dl>
2828

29+
<input type="hidden" name="update" value="1">
2930
<button type=submit class="py-2 px-4 my-4 rounded-md bg-blue-500 border-blue-500 text-white hover:bg-blue-600 hover:border-blue-600">Update</button>
3031
</form>
3132

tmpl/help.html

+3
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,8 @@ <h2 id="api">Application Programming Interface (API)</h2>
139139
{{`{"Short":"cs","Long":"https://cs.github.com/","Created":"2022-06-03T22:15:29.993978392Z","LastEdit":"2022-06-03T22:15:29.993978392Z","Owner":"[email protected]"}`}}
140140
</pre>
141141

142+
<p>
143+
To update an existing link, also include `-d update=1`.
144+
142145
</article>
143146
{{ end }}

0 commit comments

Comments
 (0)