dq05304 2015-02-01 08:31
浏览 57
已采纳

使用Nosurf和Appengine Blobstore时的HTTP 400

I am using the Appengine Blob store example and it works fine (I modified to take two files but that is not the issue). However, when I turn on nosurf it gives me a HTTP 400. I am passing in the csrf token to my form. The issue exists even if I just upload one file.

nosurf works just fine for other forms but only gives me trouble with blobstore file upload.

Since the code is large (it is the just the example with some minor tweaks), I have put it here: http://play.golang.org/p/SJADmn-WvJ (of course you cannot run it there as you need app-engine and nosurf)

Small parts of the code:

const rootTemplateHTML = `
<html><body>
<form action="{{.UpUrl}}" method="POST" enctype="multipart/form-data">
    Upload File: <input type="file" name="file1"><br>
    Upload File: <input type="file" name="file2"><br>
    <input type="hidden" name="csrf_token" value="{{ .Token }}">
    <input type="submit" name="submit" value="Submit">
</form>
</body></html>
`

This does not work:

    http.Handle("/", nosurf.New(http.HandlerFunc(handleRoot)))
    http.Handle("/upload", nosurf.New(http.HandlerFunc(handleUpload)))
    http.HandleFunc("/serve/", handleServe)

But this works (no 400 status):

    http.HandleFunc("/", handleRoot)
    http.HandleFunc("/serve/", handleServe)
    http.HandleFunc("/upload", handleUpload)

Is this something to do with nosurf or app-engine? Any suggestions on what I should do to fix this issue?

Thanks!

  • 写回答

1条回答 默认 最新

  • douquan1015 2015-02-04 04:54
    关注

    The blobstore upload URL works by actually posting your file upload to a special /_ah/... route on your app, which is not actually handled by your app but is used as a signal for the infrastructure to send the data over to the storage upload handler, which is an internal handler that does the actual put to storage.

    The callback route you pass to the function that generated the upload URL is the route on your app which will receive a request once this is done that contains not the file data but the file metadata, such as the filenames, along with any other parameters passed to the request (such as, importantly, the CSRF token).

    The token which you pass, however, was generated from a call to nosurf.Token(r), where r is the request the user's browser made to your app at the time the page was generated. When the storage upload handler sends the callback request to your /upload route, nosurf expects the client that sent the request to have a valid CSRF token generated for that client (the storage upload handler) and validates that request based on that expectation. Instead it receives the CSRF token that was generated for the page which you originally sent to the user, containing the form.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。