mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2025-06-05 21:59:39 +02:00
[feature] Use ETag for robots.txt to prevent mishaps (#3829)
* [feature] Use ETag for robots.txt to prevent mishaps * check incoming if-none-match header
This commit is contained in:
@ -39,7 +39,7 @@ func (rb *Robots) Route(r *router.Router, m ...gin.HandlerFunc) {
|
|||||||
// https://www.rfc-editor.org/rfc/rfc9309.html#section-2.4
|
// https://www.rfc-editor.org/rfc/rfc9309.html#section-2.4
|
||||||
robotsGroup.Use(
|
robotsGroup.Use(
|
||||||
middleware.CacheControl(middleware.CacheControlConfig{
|
middleware.CacheControl(middleware.CacheControlConfig{
|
||||||
Directives: []string{"public", "max-age=86400"},
|
Directives: []string{"public", "no-cache"},
|
||||||
Vary: []string{"Accept-Encoding"},
|
Vary: []string{"Accept-Encoding"},
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
@ -49,9 +49,29 @@ func (m *Module) Route(attachHandler func(method string, path string, f ...gin.H
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Module) robotsGETHandler(c *gin.Context) {
|
func (m *Module) robotsGETHandler(c *gin.Context) {
|
||||||
|
const ETag = "\"" + apiutil.RobotsTxtETag + "\""
|
||||||
|
c.Header("ETag", ETag)
|
||||||
|
|
||||||
|
if c.Request.Header.Get("If-None-Match") == ETag {
|
||||||
|
// Cached.
|
||||||
|
c.AbortWithStatus(http.StatusNotModified)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Not cached, serve.
|
||||||
c.String(http.StatusOK, apiutil.RobotsTxt)
|
c.String(http.StatusOK, apiutil.RobotsTxt)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Module) robotsGETHandlerDisallowNodeInfo(c *gin.Context) {
|
func (m *Module) robotsGETHandlerDisallowNodeInfo(c *gin.Context) {
|
||||||
|
const ETag = "\"" + apiutil.RobotsTxtDisallowNodeInfoETag + "\""
|
||||||
|
c.Header("ETag", ETag)
|
||||||
|
|
||||||
|
if c.Request.Header.Get("If-None-Match") == ETag {
|
||||||
|
// Cached.
|
||||||
|
c.AbortWithStatus(http.StatusNotModified)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Not cached, serve.
|
||||||
c.String(http.StatusOK, apiutil.RobotsTxtDisallowNodeInfo)
|
c.String(http.StatusOK, apiutil.RobotsTxtDisallowNodeInfo)
|
||||||
}
|
}
|
||||||
|
@ -130,4 +130,9 @@ Disallow: /.well-known/webfinger
|
|||||||
Disallow: /.well-known/nodeinfo
|
Disallow: /.well-known/nodeinfo
|
||||||
Disallow: /nodeinfo/
|
Disallow: /nodeinfo/
|
||||||
`
|
`
|
||||||
|
|
||||||
|
// MD5 hash of basic robots.txt.
|
||||||
|
RobotsTxtETag = `ce6729aacbb16fae3628210c04b462b7`
|
||||||
|
// MD5 hash of robots.txt with NodeInfo disallowed.
|
||||||
|
RobotsTxtDisallowNodeInfoETag = `a1e4ce6342978bc8d6c3e3dfab07cab4`
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user