2
0

api.go 761 B

12345678910111213141516171819202122232425262728293031323334
  1. // Copyright 2020 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package app
  5. import (
  6. "net/http"
  7. "github.com/microcosm-cc/bluemonday"
  8. "gopkg.in/macaron.v1"
  9. )
  10. func ipynbSanitizer() *bluemonday.Policy {
  11. p := bluemonday.UGCPolicy()
  12. p.AllowAttrs("class", "data-prompt-number").OnElements("div")
  13. p.AllowAttrs("class").OnElements("img")
  14. p.AllowURLSchemes("data")
  15. return p
  16. }
  17. func SanitizeIpynb() macaron.Handler {
  18. p := ipynbSanitizer()
  19. return func(c *macaron.Context) {
  20. html, err := c.Req.Body().String()
  21. if err != nil {
  22. c.Error(http.StatusInternalServerError, "read body")
  23. return
  24. }
  25. c.PlainText(http.StatusOK, []byte(p.Sanitize(html)))
  26. }
  27. }