2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
package header
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2019-04-04 18:51:20 +00:00
|
|
|
"github.com/laszlocph/drone-oss-08/version"
|
2015-09-30 01:21:17 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2015-10-13 09:08:08 +00:00
|
|
|
// NoCache is a middleware function that appends headers
|
|
|
|
// to prevent the client from caching the HTTP response.
|
|
|
|
func NoCache(c *gin.Context) {
|
|
|
|
c.Header("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate, value")
|
|
|
|
c.Header("Expires", "Thu, 01 Jan 1970 00:00:00 GMT")
|
|
|
|
c.Header("Last-Modified", time.Now().UTC().Format(http.TimeFormat))
|
|
|
|
c.Next()
|
|
|
|
}
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2015-10-13 09:08:08 +00:00
|
|
|
// Options is a middleware function that appends headers
|
|
|
|
// for options requests and aborts then exits the middleware
|
|
|
|
// chain and ends the request.
|
|
|
|
func Options(c *gin.Context) {
|
|
|
|
if c.Request.Method != "OPTIONS" {
|
|
|
|
c.Next()
|
|
|
|
} else {
|
2016-02-05 21:04:46 +00:00
|
|
|
c.Header("Access-Control-Allow-Origin", "*")
|
2015-10-13 09:08:08 +00:00
|
|
|
c.Header("Access-Control-Allow-Methods", "GET,POST,PUT,PATCH,DELETE,OPTIONS")
|
2016-02-05 21:39:11 +00:00
|
|
|
c.Header("Access-Control-Allow-Headers", "authorization, origin, content-type, accept")
|
2015-10-13 09:08:08 +00:00
|
|
|
c.Header("Allow", "HEAD,GET,POST,PUT,PATCH,DELETE,OPTIONS")
|
|
|
|
c.Header("Content-Type", "application/json")
|
|
|
|
c.AbortWithStatus(200)
|
|
|
|
}
|
|
|
|
}
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2015-10-13 09:08:08 +00:00
|
|
|
// Secure is a middleware function that appends security
|
|
|
|
// and resource access headers.
|
|
|
|
func Secure(c *gin.Context) {
|
|
|
|
c.Header("Access-Control-Allow-Origin", "*")
|
|
|
|
c.Header("X-Frame-Options", "DENY")
|
|
|
|
c.Header("X-Content-Type-Options", "nosniff")
|
|
|
|
c.Header("X-XSS-Protection", "1; mode=block")
|
|
|
|
if c.Request.TLS != nil {
|
|
|
|
c.Header("Strict-Transport-Security", "max-age=31536000")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also consider adding Content-Security-Policy headers
|
|
|
|
// c.Header("Content-Security-Policy", "script-src 'self' https://cdnjs.cloudflare.com")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version is a middleware function that appends the Drone
|
|
|
|
// version information to the HTTP response. This is intended
|
|
|
|
// for debugging and troubleshooting.
|
2016-03-30 07:48:47 +00:00
|
|
|
func Version(c *gin.Context) {
|
2017-04-12 11:04:39 +00:00
|
|
|
c.Header("X-DRONE-VERSION", version.Version.String())
|
2016-03-30 07:48:47 +00:00
|
|
|
c.Next()
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|