MCPcopy Create free account
hub / github.com/gogs/gogs / Contexter

Function Contexter

internal/context/context.go:225–293  ·  view source on GitHub ↗

Contexter initializes a classic context for a request.

(store Store)

Source from the content-addressed store, hash-verified

223
224// Contexter initializes a classic context for a request.
225func Contexter(store Store) macaron.Handler {
226 return func(ctx *macaron.Context, l i18n.Locale, cache cache.Cache, sess session.Store, f *session.Flash, x csrf.CSRF) {
227 c := &Context{
228 Context: ctx,
229 Cache: cache,
230 csrf: x,
231 Flash: f,
232 Session: sess,
233 Link: conf.Server.Subpath + strings.TrimSuffix(ctx.Req.URL.Path, "/"),
234 Repo: &Repository{
235 PullRequest: &PullRequest{},
236 },
237 Org: &Organization{},
238 }
239 c.Data["Link"] = template.EscapePound(c.Link)
240 c.Data["PageStartTime"] = time.Now()
241
242 if len(conf.HTTP.AccessControlAllowOrigin) > 0 {
243 c.Header().Set("Access-Control-Allow-Origin", conf.HTTP.AccessControlAllowOrigin)
244 c.Header().Set("Access-Control-Allow-Credentials", "true")
245 c.Header().Set("Access-Control-Max-Age", "3600")
246 c.Header().Set("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With")
247 }
248
249 // Get user from session or header when possible
250 c.User, c.IsBasicAuth, c.IsTokenAuth = authenticatedUser(store, c.Context, c.Session)
251
252 if c.User != nil {
253 c.IsLogged = true
254 c.Data["IsLogged"] = c.IsLogged
255 c.Data["LoggedUser"] = c.User
256 c.Data["LoggedUserID"] = c.User.ID
257 c.Data["LoggedUserName"] = c.User.Name
258 c.Data["IsAdmin"] = c.User.IsAdmin
259 } else {
260 c.Data["LoggedUserID"] = 0
261 c.Data["LoggedUserName"] = ""
262 }
263
264 // If request sends files, parse them here otherwise the Query() can't be parsed and the CsrfToken will be invalid.
265 if c.Req.Method == "POST" && strings.Contains(c.Req.Header.Get("Content-Type"), "multipart/form-data") {
266 if err := c.Req.ParseMultipartForm(conf.Attachment.MaxSize << 20); err != nil && !strings.Contains(err.Error(), "EOF") { // 32MB max size
267 c.Error(err, "parse multipart form")
268 return
269 }
270 }
271
272 // 🚨 SECURITY: Prevent XSS from injected CSRF cookie by stripping all
273 // characters that are not used for generating CSRF tokens, see
274 // https://github.com/gogs/gogs/issues/6953 for details.
275 csrfToken := csrfTokenExcludePattern.ReplaceAllString(x.GetToken(), "")
276 c.Data["CSRFToken"] = csrfToken
277 c.Data["CSRFTokenHTML"] = template.Safe(`<input type="hidden" name="_csrf" value="` + csrfToken + `">`)
278 log.Trace("Session ID: %s", sess.ID())
279 log.Trace("CSRF Token: %v", c.Data["CSRFToken"])
280
281 c.Data["ShowRegistrationButton"] = !conf.Auth.DisableRegistration
282 c.Data["ShowFooterBranding"] = conf.Other.ShowFooterBranding

Callers 1

runWebFunction · 0.92

Calls 9

ErrorMethod · 0.95
renderNoticeBannerMethod · 0.95
EscapePoundFunction · 0.92
SafeFunction · 0.92
authenticatedUserFunction · 0.85
HeaderMethod · 0.80
ReplaceAllStringMethod · 0.80
GetMethod · 0.65
IDMethod · 0.65

Tested by

no test coverage detected