| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | |
| 8 | "gitlab.com/Timfa/bluegit/internal/access" |
| 9 | "gitlab.com/Timfa/bluegit/internal/desctpl" |
| 10 | "gitlab.com/Timfa/bluegit/internal/gitrepo" |
| 11 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 12 | "gitlab.com/Timfa/bluegit/internal/store" |
| 13 | ) |
| 14 | |
| 15 | func (s *Server) guard(w http.ResponseWriter, r *http.Request, min access.Role) (store.User, store.Repo, access.Role, bool) { |
| 16 | owner := r.PathValue("owner") |
| 17 | name := strings.TrimSuffix(r.PathValue("repo"), ".git") |
| 18 | repo, err := s.Store.RepoByPath(r.Context(), owner, name) |
| 19 | if err != nil { |
| 20 | http.NotFound(w, r) |
| 21 | return store.User{}, store.Repo{}, "", false |
| 22 | } |
| 23 | p := s.who(r) |
| 24 | var role access.Role |
| 25 | if p.OK { |
| 26 | role, err = s.Store.EffectiveRole(r.Context(), p.User.ID, repo.ID) |
| 27 | if err != nil { |
| 28 | http.Error(w, s.tr(r, "err.rights"), http.StatusInternalServerError) |
| 29 | return store.User{}, store.Repo{}, "", false |
| 30 | } |
| 31 | } |
| 32 | if isCrawler(r) { |
| 33 | p = principal{} |
| 34 | role = access.RoleNone |
| 35 | } |
| 36 | readable := role.CanRead() || repo.Visibility == "public" |
| 37 | if !readable { |
| 38 | if isCrawler(r) { |
| 39 | s.writeLockedCard(w, r) |
| 40 | } else if p.OK { |
| 41 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 42 | } else { |
| 43 | http.NotFound(w, r) |
| 44 | } |
| 45 | return store.User{}, store.Repo{}, "", false |
| 46 | } |
| 47 | if min != access.RoleReadonly && !role.AtLeast(min) { |
| 48 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 49 | return store.User{}, store.Repo{}, "", false |
| 50 | } |
| 51 | return p.User, repo, role, true |
| 52 | } |
| 53 | |
| 54 | func (s *Server) repoView(w http.ResponseWriter, r *http.Request, repo store.Repo, role access.Role, title string) View { |
| 55 | v := s.view(w, r, title) |
| 56 | v.Repo = &repo |
| 57 | v.Role = role |
| 58 | v.CanWrite = role.CanWriteCode() |
| 59 | v.CanAdmin = role.CanManageSettings() |
| 60 | v.Rev = safeRev(r.URL.Query().Get("rev"), repo.DefaultBranch) |
| 61 | v.CloneHTTP = strings.TrimRight(s.Cfg.BaseURL, "/") + "/" + repo.FullPath() + ".git" |
| 62 | v.CloneSSH = s.Cfg.CloneSSH(repo.FullPath()) |
| 63 | return v |
| 64 | } |
| 65 | |
| 66 | func (s *Server) repoHome(w http.ResponseWriter, r *http.Request) { |
| 67 | _, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 68 | if !ok { |
| 69 | return |
| 70 | } |
| 71 | v := s.repoView(w, r, repo, role, repo.Name) |
| 72 | v.Wide = true |
| 73 | s.fillOverview(r, &v, repo) |
| 74 | v.Heat = s.heatmap(r.Context(), 0, []int64{repo.ID}, s.langOf(r)) |
| 75 | s.render(w, "repo", v) |
| 76 | } |
| 77 | |
| 78 | func (s *Server) treeRoot(w http.ResponseWriter, r *http.Request) { |
| 79 | r.SetPathValue("path", "") |
| 80 | s.tree(w, r) |
| 81 | } |
| 82 | |
| 83 | func (s *Server) tree(w http.ResponseWriter, r *http.Request) { |
| 84 | _, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 85 | if !ok { |
| 86 | return |
| 87 | } |
| 88 | rel, err := cleanRel(r.PathValue("path")) |
| 89 | if err != nil { |
| 90 | http.NotFound(w, r) |
| 91 | return |
| 92 | } |
| 93 | v := s.repoView(w, r, repo, role, rel) |
| 94 | v.Wide = true |
| 95 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 96 | if _, err := gitrepo.Tree(r.Context(), s.Git, dir, v.Rev, rel); err != nil && rel != "" { |
| 97 | http.NotFound(w, r) |
| 98 | return |
| 99 | } |
| 100 | s.fillFiles(r, &v, repo, rel, false) |
| 101 | s.render(w, "files", v) |
| 102 | } |
| 103 | |
| 104 | func (s *Server) blob(w http.ResponseWriter, r *http.Request) { |
| 105 | _, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 106 | if !ok { |
| 107 | return |
| 108 | } |
| 109 | rel, err := cleanRel(r.PathValue("path")) |
| 110 | if err != nil || rel == "" { |
| 111 | http.NotFound(w, r) |
| 112 | return |
| 113 | } |
| 114 | v := s.repoView(w, r, repo, role, rel) |
| 115 | v.Wide = true |
| 116 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 117 | if _, _, err := gitrepo.Blob(r.Context(), s.Git, dir, v.Rev, rel, 1); err != nil { |
| 118 | http.NotFound(w, r) |
| 119 | return |
| 120 | } |
| 121 | s.fillFiles(r, &v, repo, rel, true) |
| 122 | s.render(w, "files", v) |
| 123 | } |
| 124 | |
| 125 | func (s *Server) commits(w http.ResponseWriter, r *http.Request) { |
| 126 | _, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 127 | if !ok { |
| 128 | return |
| 129 | } |
| 130 | v := s.repoView(w, r, repo, role, "title.commits") |
| 131 | v.Page = "commits" |
| 132 | v.Wide = true |
| 133 | v.Query = clipQuery(r.URL.Query().Get("q")) |
| 134 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 135 | s.branchList(r, &v, repo) |
| 136 | page := commitPageNum(r.URL.Query().Get("page")) |
| 137 | list, more := s.commitWindow(r.Context(), dir, v.Rev, v.Query, (page-1)*commitPageSize, commitPageSize) |
| 138 | lang := i18n.Parse(v.Lang) |
| 139 | rows := s.presentCommits(r.Context(), repo.ID, list, lang) |
| 140 | for i := range rows { |
| 141 | s.attachHeadPipeline(r.Context(), repo, &rows[i], lang) |
| 142 | } |
| 143 | v.Commits = rows |
| 144 | v.CommitDays = groupCommitDays(rows, lang) |
| 145 | if page > 1 { |
| 146 | v.CommitPrev = commitListURL(repo.FullPath(), v.Rev, v.Query, page-1) |
| 147 | } |
| 148 | if more { |
| 149 | v.CommitNext = commitListURL(repo.FullPath(), v.Rev, v.Query, page+1) |
| 150 | } |
| 151 | s.render(w, "commits", v) |
| 152 | } |
| 153 | |
| 154 | func (s *Server) commit(w http.ResponseWriter, r *http.Request) { |
| 155 | _, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 156 | if !ok { |
| 157 | return |
| 158 | } |
| 159 | sha := r.PathValue("sha") |
| 160 | if !safeSHA(sha) { |
| 161 | http.NotFound(w, r) |
| 162 | return |
| 163 | } |
| 164 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 165 | c, err := gitrepo.Show(r.Context(), s.Git, dir, sha) |
| 166 | if err != nil { |
| 167 | http.NotFound(w, r) |
| 168 | return |
| 169 | } |
| 170 | v := s.repoView(w, r, repo, role, c.Subject) |
| 171 | v.Page = "commit" |
| 172 | v.Wide = true |
| 173 | lang := i18n.Parse(v.Lang) |
| 174 | rows := s.presentCommits(r.Context(), repo.ID, []gitrepo.Commit{c}, lang) |
| 175 | s.attachHeadPipeline(r.Context(), repo, &rows[0], lang) |
| 176 | v.Commit = &rows[0] |
| 177 | parents, files, err := gitrepo.CommitPatch(r.Context(), s.Git, dir, c.SHA) |
| 178 | if err != nil && s.Log != nil { |
| 179 | s.Log.Warn("commitdiff", "sha", c.SHA, "err", err) |
| 180 | } |
| 181 | v.CommitDiff = presentCommitDiff(repo.FullPath(), c.SHA, parents, files) |
| 182 | s.render(w, "commit", v) |
| 183 | } |
| 184 | |
| 185 | func (s *Server) mergeRequests(w http.ResponseWriter, r *http.Request) { |
| 186 | _, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 187 | if !ok { |
| 188 | return |
| 189 | } |
| 190 | s.absorbMergedMRs(r.Context(), repo) |
| 191 | v := s.repoView(w, r, repo, role, "title.mrs") |
| 192 | v.Page = "mrs" |
| 193 | v.Wide = true |
| 194 | all, _ := s.Store.ListMergeRequests(r.Context(), repo.ID) |
| 195 | v.Flow = s.flowMRs(r, repo, role, all) |
| 196 | s.render(w, "mrs", v) |
| 197 | } |
| 198 | |
| 199 | func (s *Server) createMR(w http.ResponseWriter, r *http.Request) { |
| 200 | u, repo, _, ok := s.guard(w, r, access.RoleDeveloper) |
| 201 | if !ok || !s.csrfOK(w, r) { |
| 202 | return |
| 203 | } |
| 204 | source := safeRev(r.FormValue("source"), "") |
| 205 | target := safeRev(r.FormValue("target"), repo.DefaultBranch) |
| 206 | desc := desctpl.Expand(r.FormValue("description"), s.mrVars(r, repo, source, target)) |
| 207 | mr, err := s.opCreateMR(r.Context(), u, repo, r.FormValue("title"), desc, source, target, r.FormValue("draft") == "1") |
| 208 | if err == nil { |
| 209 | err = s.Store.SetMergeOptions(r.Context(), mr.ID, r.FormValue("remove_source_branch") == "1", r.FormValue("squash") == "1", r.FormValue("squash_commit_message")) |
| 210 | } |
| 211 | if !s.uiOK(w, r, repoURL(repo)+"/-/merge-requests", err) { |
| 212 | return |
| 213 | } |
| 214 | num := strconv.FormatInt(mr.Number, 10) |
| 215 | s.redirect(w, r, repoURL(repo)+"/-/merge-requests/"+num, s.tr(r, "flash.mrOpened"), "") |
| 216 | } |
| 217 | |
| 218 | func (s *Server) showMR(w http.ResponseWriter, r *http.Request) { |
| 219 | u, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 220 | if !ok { |
| 221 | return |
| 222 | } |
| 223 | s.absorbMergedMRs(r.Context(), repo) |
| 224 | n, err := strconv.ParseInt(r.PathValue("number"), 10, 64) |
| 225 | if err != nil { |
| 226 | http.NotFound(w, r) |
| 227 | return |
| 228 | } |
| 229 | mr, err := s.Store.MergeRequestByNumber(r.Context(), repo.ID, n) |
| 230 | if err != nil { |
| 231 | http.NotFound(w, r) |
| 232 | return |
| 233 | } |
| 234 | v := s.repoView(w, r, repo, role, mr.Title) |
| 235 | v.Page = "mr" |
| 236 | v.MR = &mr |
| 237 | v.Wide = true |
| 238 | patterns, _ := s.Store.ProtectedPatterns(r.Context(), repo.ID) |
| 239 | v.CanComment = role.AtLeast(access.RoleReadonly) |
| 240 | v.CanMerge = mr.Status == "open" && role.CanMerge(access.BranchProtected(patterns, mr.TargetBranch)) |
| 241 | v.CanApply = mr.Status == "open" && role.CanPushBranch(access.BranchProtected(patterns, mr.SourceBranch)) |
| 242 | v.MergeBlocks = s.mergeBlocks(r, repo, mr) |
| 243 | v.CanForce = v.CanMerge && role.CanManageSettings() && len(v.MergeBlocks) > 0 |
| 244 | s.fillMRPipeline(r, &v, repo, mr) |
| 245 | s.fillReviews(r, &v, repo, mr, role) |
| 246 | if u.ID != 0 { |
| 247 | mine := u.ID == mr.AuthorID || role.CanManageSettings() |
| 248 | v.CanClose = mr.Status == "open" && mine |
| 249 | v.CanReopen = mr.Status == "closed" && mine |
| 250 | } |
| 251 | s.finishMR(r, &v, repo, mr, u) |
| 252 | s.render(w, "mr", v) |
| 253 | } |
| 254 | |
| 255 | func (s *Server) createNote(w http.ResponseWriter, r *http.Request) { |
| 256 | u, repo, _, ok := s.guard(w, r, access.RoleReadonly) |
| 257 | if !ok || !s.csrfOK(w, r) { |
| 258 | return |
| 259 | } |
| 260 | n, err := strconv.ParseInt(r.PathValue("number"), 10, 64) |
| 261 | if err != nil { |
| 262 | http.NotFound(w, r) |
| 263 | return |
| 264 | } |
| 265 | back := repoURL(repo) + "/-/merge-requests/" + r.PathValue("number") |
| 266 | parent, _ := strconv.ParseInt(r.FormValue("parent"), 10, 64) |
| 267 | start, _ := strconv.Atoi(r.FormValue("line_start")) |
| 268 | end, _ := strconv.Atoi(r.FormValue("line_end")) |
| 269 | if _, err := s.opAddNote(r, u, repo, n, r.FormValue("body"), r.FormValue("path"), start, end, parent); !s.uiOK(w, r, back, err) { |
| 270 | return |
| 271 | } |
| 272 | s.redirect(w, r, back, s.tr(r, "flash.note"), "") |
| 273 | } |
| 274 | |
| 275 | func (s *Server) mergeMR(w http.ResponseWriter, r *http.Request) { |
| 276 | u, repo, _, ok := s.guard(w, r, access.RoleDeveloper) |
| 277 | if !ok || !s.csrfOK(w, r) { |
| 278 | return |
| 279 | } |
| 280 | n, err := strconv.ParseInt(r.PathValue("number"), 10, 64) |
| 281 | if err != nil { |
| 282 | http.NotFound(w, r) |
| 283 | return |
| 284 | } |
| 285 | back := repoURL(repo) + "/-/merge-requests/" + r.PathValue("number") |
| 286 | out, err := s.opMergeMR(r, u, repo, n, formMergeChoice(r)) |
| 287 | if !s.uiOK(w, r, back, err) { |
| 288 | return |
| 289 | } |
| 290 | notice := s.tr(r, out.Notice) |
| 291 | if out.Arg != "" { |
| 292 | notice = s.tr(r, out.Notice, out.Arg) |
| 293 | } |
| 294 | s.redirect(w, r, back, notice, "") |
| 295 | } |
| 296 | |
| 297 | func formMergeChoice(r *http.Request) mergeChoice { |
| 298 | remove := r.FormValue("remove_source_branch") == "1" |
| 299 | squash := r.FormValue("squash") == "1" |
| 300 | message := r.FormValue("squash_commit_message") |
| 301 | return mergeChoice{ |
| 302 | FromForm: true, |
| 303 | Remove: &remove, |
| 304 | Squash: &squash, |
| 305 | Message: &message, |
| 306 | Force: r.FormValue("force") == "1", |
| 307 | Confirm: r.FormValue("confirm_force") == "1", |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | func (s *Server) closeMR(w http.ResponseWriter, r *http.Request) { |
| 312 | u, repo, _, ok := s.guard(w, r, access.RoleReadonly) |
| 313 | if !ok || !s.csrfOK(w, r) { |
| 314 | return |
| 315 | } |
| 316 | n, err := strconv.ParseInt(r.PathValue("number"), 10, 64) |
| 317 | if err != nil { |
| 318 | http.NotFound(w, r) |
| 319 | return |
| 320 | } |
| 321 | back := repoURL(repo) + "/-/merge-requests/" + r.PathValue("number") |
| 322 | if !s.uiOK(w, r, back, s.opCloseMR(r.Context(), u, repo, n, false)) { |
| 323 | return |
| 324 | } |
| 325 | s.redirect(w, r, back, s.tr(r, "flash.closed"), "") |
| 326 | } |
| 327 | |
| 328 | func (s *Server) mrFromRequest(r *http.Request, repoID int64) (store.MergeRequest, error) { |
| 329 | n, err := strconv.ParseInt(r.PathValue("number"), 10, 64) |
| 330 | if err != nil { |
| 331 | return store.MergeRequest{}, err |
| 332 | } |
| 333 | return s.Store.MergeRequestByNumber(r.Context(), repoID, n) |
| 334 | } |
| 335 | |
| 336 | func (s *Server) pipelines(w http.ResponseWriter, r *http.Request) { |
| 337 | _, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 338 | if !ok { |
| 339 | return |
| 340 | } |
| 341 | if id := strings.TrimSpace(r.URL.Query().Get("show")); id != "" { |
| 342 | if n, err := strconv.ParseInt(id, 10, 64); err == nil && n > 0 { |
| 343 | http.Redirect(w, r, repoURL(repo)+"/-/pipelines/"+id, http.StatusSeeOther) |
| 344 | return |
| 345 | } |
| 346 | } |
| 347 | v := s.repoView(w, r, repo, role, "title.pipelines") |
| 348 | v.Wide = true |
| 349 | v.Page = "pipelines" |
| 350 | s.fillPipelines(r, &v, repo) |
| 351 | if wantsJSON(r) { |
| 352 | s.writePipeList(w, v) |
| 353 | return |
| 354 | } |
| 355 | s.render(w, "pipelines", v) |
| 356 | } |
| 357 | |
| 358 | func (s *Server) pipeline(w http.ResponseWriter, r *http.Request) { |
| 359 | _, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 360 | if !ok { |
| 361 | return |
| 362 | } |
| 363 | id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) |
| 364 | if err != nil { |
| 365 | http.NotFound(w, r) |
| 366 | return |
| 367 | } |
| 368 | p, err := s.Store.PipelineByID(r.Context(), repo.ID, id) |
| 369 | if err != nil { |
| 370 | http.NotFound(w, r) |
| 371 | return |
| 372 | } |
| 373 | v := s.repoView(w, r, repo, role, "title.pipeline") |
| 374 | v.Wide = true |
| 375 | v.Page = "pipelines" |
| 376 | v.Pipe = &p |
| 377 | jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID) |
| 378 | v.Jobs = jobs |
| 379 | ranks := s.queueRanks(r.Context()) |
| 380 | tr := func(key string, args ...any) string { return s.tr(r, key, args...) } |
| 381 | v.PipeStatus = displayStatus(p.Status, jobs) |
| 382 | v.PipeLabel = tr("pipe." + v.PipeStatus) |
| 383 | v.PipeSource = s.pipelineSourceText(r, repo, p) |
| 384 | v.PipeJobs = linksFor(jobs, ranks, tr) |
| 385 | v.PipeStages = stageViews(jobs, ranks, tr) |
| 386 | v.PipeFailed = jobsFailed(jobs) |
| 387 | v.Live = jobsLive(p.Status, jobs) |
| 388 | v.LiveURL = repoURL(repo) + "/-/pipelines/" + strconv.FormatInt(p.ID, 10) |
| 389 | if wantsJSON(r) { |
| 390 | s.writePipeDetail(w, v) |
| 391 | return |
| 392 | } |
| 393 | s.render(w, "pipeline", v) |
| 394 | } |
| 395 | |
| 396 | func (s *Server) runPipeline(w http.ResponseWriter, r *http.Request) { |
| 397 | u, repo, _, ok := s.guard(w, r, access.RoleDeveloper) |
| 398 | if !ok || !s.csrfOK(w, r) { |
| 399 | return |
| 400 | } |
| 401 | if _, err := s.opRunPipeline(r.Context(), u, repo, r.FormValue("ref"), "web"); !s.uiOK(w, r, repoURL(repo)+"/-/pipelines", err) { |
| 402 | return |
| 403 | } |
| 404 | s.redirect(w, r, repoURL(repo)+"/-/pipelines", s.tr(r, "flash.pipeline"), "") |
| 405 | } |
| 406 | |
| 407 | func (s *Server) releases(w http.ResponseWriter, r *http.Request) { |
| 408 | _, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 409 | if !ok { |
| 410 | return |
| 411 | } |
| 412 | v := s.repoView(w, r, repo, role, "title.releases") |
| 413 | v.Page = "releases" |
| 414 | v.Wide = true |
| 415 | v.Releases, _ = s.Store.ListReleases(r.Context(), repo.ID) |
| 416 | s.render(w, "releases", v) |
| 417 | } |
| 418 | |
| 419 | func (s *Server) createRepo(w http.ResponseWriter, r *http.Request) { |
| 420 | u, ok := s.loginRequired(w, r) |
| 421 | if !ok || !s.csrfOK(w, r) { |
| 422 | return |
| 423 | } |
| 424 | repo, err := s.opCreateRepo(r.Context(), u, r.FormValue("name"), r.FormValue("description"), r.FormValue("group"), r.FormValue("visibility"), false) |
| 425 | if err != nil { |
| 426 | if statusOf(err) == http.StatusForbidden { |
| 427 | http.Error(w, s.trErr(r, err), http.StatusForbidden) |
| 428 | return |
| 429 | } |
| 430 | s.failCreate(w, r, s.trErr(r, err)) |
| 431 | return |
| 432 | } |
| 433 | s.finishCreate(w, r, repoURL(repo), s.tr(r, "flash.repoCreated")) |
| 434 | } |
| 435 | |
| 436 | func (s *Server) createGroup(w http.ResponseWriter, r *http.Request) { |
| 437 | u, ok := s.loginRequired(w, r) |
| 438 | if !ok || !s.csrfOK(w, r) { |
| 439 | return |
| 440 | } |
| 441 | g, err := s.opCreateGroup(r.Context(), u, r.FormValue("name"), r.FormValue("description"), r.FormValue("default_role")) |
| 442 | if err != nil { |
| 443 | s.failCreate(w, r, s.trErr(r, err)) |
| 444 | return |
| 445 | } |
| 446 | s.finishCreate(w, r, "/groups/"+g.Path, s.tr(r, "flash.groupCreated")) |
| 447 | } |
| 448 | |
| 449 | func toEntries(base string, in []gitrepo.Entry) []EntryRow { |
| 450 | var out []EntryRow |
| 451 | for _, e := range in { |
| 452 | p := e.Name |
| 453 | if base != "" { |
| 454 | p = base + "/" + e.Name |
| 455 | } |
| 456 | out = append(out, EntryRow{Name: e.Name, Type: e.Type, Path: p}) |
| 457 | } |
| 458 | return out |
| 459 | } |
| 460 | |
| 461 | func cleanRel(p string) (string, error) { |
| 462 | p = strings.TrimPrefix(p, "/") |
| 463 | if p == "" || p == "." { |
| 464 | return "", nil |
| 465 | } |
| 466 | if strings.HasPrefix(p, "/") || strings.Contains(p, "\\") || strings.Contains(p, "..") { |
| 467 | return "", errBadID |
| 468 | } |
| 469 | return p, nil |
| 470 | } |
| 471 | |
| 472 | func safeRev(s, fallback string) string { |
| 473 | if s == "" { |
| 474 | return fallback |
| 475 | } |
| 476 | if strings.HasPrefix(s, "-") || strings.Contains(s, "..") || strings.ContainsAny(s, " \t\n~^:?*[\\") { |
| 477 | return fallback |
| 478 | } |
| 479 | return s |
| 480 | } |
| 481 | |
| 482 | func safeSHA(s string) bool { |
| 483 | if len(s) < 7 || len(s) > 64 { |
| 484 | return false |
| 485 | } |
| 486 | for _, c := range s { |
| 487 | if (c < '0' || c > '9') && (c < 'a' || c > 'f') && (c < 'A' || c > 'F') { |
| 488 | return false |
| 489 | } |
| 490 | } |
| 491 | return true |
| 492 | } |