首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在从Golang Buffalo应用程序发送推特时设置CSRF令牌时出错

在从Golang Buffalo应用程序发送推特时设置CSRF令牌时出错
EN

Stack Overflow用户
提问于 2019-04-12 12:17:30
回答 2查看 608关注 0票数 1

我有麻烦的去水牛城网络应用程序,我正在努力构建。我的前端基本上有一个标准的表单,当点击它时,应该发送一条推特给用户twitter帐户。我得到了一个"500: CSRF找不到“时这样做。我正在使用Go 水牛城框架,用于web应用程序和go-推特来处理twitter。

我在CSRF方面几乎没有经验,所以我希望有人能在这个特殊的情况下帮助我。

tweet.HTML:

代码语言:javascript
复制
<h3>Tweet tweet!</h3>
<form action="/tweet" method="POST">
    <div class="form-group">
        <label for="tweet">Your tweet:</label>
        <input type="text" name="tweet" class="form-control" id="tweet" placeholder="Tweet body" 
        value="">
    </div>
    <button type="submit" class="btn btn-primary">Send</button>
</form>

路由器:

代码语言:javascript
复制
app.POST("/tweet", SendHandler)

Tweet.go (包含SendHandler函数):

代码语言:javascript
复制
package actions

import (
    "fmt"
    "log"
    "os"

    "twitapp/twitter"

    "github.com/gobuffalo/buffalo"
)

var creds = twitter.Credentials{
    AccessToken:       os.Getenv("ACCESS_TOKEN"),
    AccessTokenSecret: os.Getenv("ACCESS_TOKEN_SECRET"),
    APIKey:            os.Getenv("API_KEY"),
    APISecret:         os.Getenv("API_SECRET"),
}

type TweetForm struct {
    TweetBody string
}

// TweetHandler is the handler for the Tweet page
func TweetHandler(c buffalo.Context) error {
    return c.Render(200, r.HTML("tweet.html"))
}

//SendHandler Function used by tweet.html to send the tweet/
//Takes in variables from twitter/server.go
func SendHandler(c buffalo.Context) error {

    var form TweetForm
    body := form.TweetBody

    fmt.Printf("%+v\n", creds)

    //Gets the client from the twitter package
    client, err := twitter.GetClient(&creds)
    if err != nil {
        log.Println("Error getting Twitter Client")
        log.Println(err)
    }

    //Sending the actual tweet. Body from the form
    tweet, resp, err := client.Statuses.Update(body, nil)
    if err != nil {
        log.Println(err)
    }
    log.Printf("%+v\n", resp)
    log.Printf("%+v\n", tweet)
    return c.Redirect(302, "/tweet/confirm")

}

还有一个GetClient函数是从SendHandler函数调用的,但是我无法想象问题就在那里,因为它是为调用设置twitter的标准代码。感激任何帮助/指向正确的方向。目前,我还没有足够的CSRF知识将其应用到这个领域。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-14 11:18:41

在查看了GoBuffalo文档,特别是表单页面的部分之后,我所要做的就是将这一行代码添加到表单(tweet.html)中,以处理真实性令牌。

代码语言:javascript
复制
<input name="authenticity_token" type="hidden" value="<%= authenticity_token %>">
票数 0
EN

Stack Overflow用户

发布于 2019-04-12 17:13:54

CSRF是水牛试图保护你免受攻击的一种攻击。

要做到这一点,它使用CSRF中间件来检查每一个可以改变数据的请求(例如。(张贴、放置、删除等)寻找一个特定的标记。在您的用例中,布法罗希望您发送一个名为“的输入字段,其中包含CSRF中间件在上下文中的值。您可以在上下文中相同的"authenticity_token“键中找到此值。

另一种解决方案是使用标记帮助程序(https://github.com/gobuffalo/tags)库,它为您生成预期的输入:https://github.com/gobuffalo/tags/wiki/Form

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55651460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档