-
Notifications
You must be signed in to change notification settings - Fork 733
增加 csrf 的判断 #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
增加 csrf 的判断 #55
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env bash | ||
|
||
DIR=`pwd` | ||
export GOPATH= | ||
export GOPATH=$DIR | ||
export PATH=$PATH:$DIR/bin | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个文件是干嘛用的?将 studygolang 执行文件加入 PATH ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 当前的工作环境的gopath设置。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. start.sh 脚本就会设置这些 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2017 The StudyGolang Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
// http://studygolang.com | ||
// Author: momaek [email protected] | ||
|
||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
"util" | ||
|
||
"github.com/labstack/echo" | ||
) | ||
|
||
// ErrorRet 如果是 ajax 请求,返回前端错误信息的通用结构体 | ||
type ErrorRet struct { | ||
OK int `json:"ok"` | ||
Error string `json:"error"` | ||
} | ||
|
||
// CsrfRefererFilter 通过 referer 过滤csrf请求 | ||
func CsrfRefererFilter() echo.MiddlewareFunc { | ||
return func(next echo.HandlerFunc) echo.HandlerFunc { | ||
return func(ctx echo.Context) (err error) { | ||
req := ctx.Request() | ||
can := false | ||
|
||
defer func() { | ||
if can { | ||
err = next(ctx) | ||
} else { | ||
if util.IsAjax(ctx) { | ||
ctx.JSON(499, &ErrorRet{0, "CSRF Detected"}) | ||
} else { | ||
ctx.String(499, "CSRF Detected") | ||
} | ||
} | ||
}() | ||
|
||
switch req.Method() { | ||
case http.MethodGet, http.MethodHead: | ||
can = true | ||
return | ||
} | ||
|
||
referer := req.Referer() | ||
if len(referer) == 0 { | ||
return | ||
} | ||
|
||
u, err := url.Parse(referer) | ||
if err != nil { | ||
return | ||
} | ||
|
||
if u.Host != req.Host() { | ||
return | ||
} | ||
|
||
can = true | ||
return | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -370,6 +370,8 @@ func (UserLogic) Total() int64 { | |
var ( | ||
ErrUsername = errors.New("用户名不存在") | ||
ErrPasswd = errors.New("密码错误") | ||
|
||
ErrUnameOrPwd = errors.New("用户名或密码错误") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 告知用户名或密码错误,是怕猜测吗? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 对 |
||
) | ||
|
||
// Login 登录;成功返回用户登录信息(user_login) | ||
|
@@ -385,7 +387,7 @@ func (self UserLogic) Login(ctx context.Context, username, passwd string) (*mode | |
// 校验用户 | ||
if userLogin.Uid == 0 { | ||
objLog.Infof("user %q is not exists!", username) | ||
return nil, ErrUsername | ||
return nil, ErrUnameOrPwd | ||
} | ||
|
||
// 检验用户状态是否正常(未激活的可以登录,但不能发布信息) | ||
|
@@ -405,7 +407,7 @@ func (self UserLogic) Login(ctx context.Context, username, passwd string) (*mode | |
objLog.Debugf("passwd: %s, passcode: %s, md5passwd: %s, dbpasswd: %s", passwd, userLogin.Passcode, md5Passwd, userLogin.Passwd) | ||
if md5Passwd != userLogin.Passwd { | ||
objLog.Infof("用户名 %q 填写的密码错误", username) | ||
return nil, ErrPasswd | ||
return nil, ErrUnameOrPwd | ||
} | ||
|
||
go func() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ func main() { | |
serveStatic(e) | ||
|
||
e.Use(thirdmw.EchoLogger()) | ||
e.Use(pwm.CsrfRefererFilter()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 项目中还有一个 AppGroup,也就是用于 APP 接口的,而接口没有 csrf 一说。所以,这个中间件,加到 frontG 和 adminG 上面吧。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个处理下? @momaek There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK |
||
e.Use(mw.Recover()) | ||
e.Use(pwm.Installed(filterPrefixs)) | ||
e.Use(pwm.HTTPError()) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的用途是?
结合 env.sh 然后 执行 make run-studygolang ? 感觉是不是没啥必要?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
方便运行
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
上面 make start 就可以运行了呀
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
start做的事情是需要先 build ,然后在start。如果每次都写一点东西然后build,然后再start感觉有点效率低下。