登录
首页 >  Golang >  Go问答

禁用 CSRF保护的JSON API调用

来源:stackoverflow

时间:2024-03-11 14:45:27 192浏览 收藏

IT行业相对于一般传统行业,发展更新速度更快,一旦停止了学习,很快就会被行业所淘汰。所以我们需要踏踏实实的不断学习,精进自己的技术,尤其是初学者。今天golang学习网给大家整理了《禁用 CSRF保护的JSON API调用》,聊聊,我们一起来看看吧!

问题内容

我有一个网站项目。它使用 go 和 gorilla 及其 csrf 包来防御 csrf。我还有一个 json api,它使用类似令牌提供程序(内部)的 jwt 进行身份验证,因此用户每次发出 json 请求之前都必须使用该 api 进行身份验证。所以 csrf 在 json 方面不是问题。至少我不这么认为。

这是我的代码,其中我使用 newrouter 作为 web 路径,并使用 subrouter 作为 /api/v1/[endpoint]。如果我调用执行 post 的 json 端点,则会使用 csrf,并且我会收到 forbidden - csrf 令牌无效。我的假设是,子路由器可能没有与 csrf 检查相关的中间件。

router := mux.NewRouter().StrictSlash(false)
router.Path("/").HandlerFunc(myApp.IndexHandler).Methods("GET")

apiRouter := router.PathPrefix("/api").Subrouter()
apiRouter.Path("/dosomething").HandlerFunc(myApp.DoSomethingAPIHandler).Methods("POST", "OPTIONS")

http.ListenAndServe(":8000",
    csrf.Protect(
        []byte("my-long-key-here-redacted"),
        csrf.Secure(false), // Set to false as we offload SSL elsewhere
    )(router)))

问题: 如何让我的 api 在有或没有 csrf 保护的情况下工作?显然,需要保护网络路径以保护表单帖子。


解决方案


一种选择是仅在特定 http 处理程序上使用 csrf 保护,而不是保护整个路由器。请注意,这将要求您对 myapp.indexhandler 执行类型转换,以满足 csrf.protect() 返回的函数的类型签名。

router := mux.newrouter().strictslash(false)

// instead of protecting your entire router, you can protect specific http
// handlers.
router.path("/").handler(
    csrf.protect(
        []byte("my-long-key-here-redacted"),
        csrf.secure(false),
    )(http.handlerfunc(myapp.indexhandler)),
).methods("get")

apirouter := router.pathprefix("/api").subrouter()
apirouter.path("/dosomething").handlerfunc(myapp.dosomethingapihandler).methods("post", "options")

http.listenandserve(
    ":8000",
    router,
)

或者,您可以使用从 csrf.protect() 返回的函数来创建您自己的中间件,其逻辑仅针对某些请求添加 csrf 保护。例如,您可以使用此方法仅在具有前缀 /api 的端点上添加保护,正如我在下面的代码中所做的那样。

protectionMiddleware := func(handler http.Handler) http.Handler {
    protectionFn := csrf.Protect(
        []byte("my-long-key-here-redacted"),
        csrf.Secure(false),
    )

    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        // Use some kind of condition here to see if the router should use
        // the CSRF protection. For the sake of this example, we'll check
        // the path prefix.
        if !strings.HasPrefix(r.URL.Path, "/api") {
            protectionFn(handler).ServeHTTP(w, r)
            return
        }

        handler.ServeHTTP(w, r)
    })
}

router := mux.NewRouter().StrictSlash(false)
router.Path("/").HandlerFunc(myApp.IndexHandler).Methods("GET")

apiRouter := router.PathPrefix("/api").Subrouter()
apiRouter.Path("/dosomething").HandlerFunc(myApp.DoSomethingAPIHandler).Methods("POST", "OPTIONS")

http.ListenAndServe(
    ":8000",
    protectionMiddleware(router),
)

今天关于《禁用 CSRF保护的JSON API调用》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>