Thanks to visit codestin.com
Credit goes to github.com

Skip to content

增加 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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ stop:

migrate:
./bin/migrator --changeVersion=${v}

run-studygolang:
cd src/server/studygolang; go run `ls | grep -v windows`; cd -
Copy link
Member

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 ? 感觉是不是没啥必要?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

方便运行

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上面 make start 就可以运行了呀

Copy link
Author

@momaek momaek Feb 27, 2018

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感觉有点效率低下。

6 changes: 6 additions & 0 deletions env.sh
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个文件是干嘛用的?将 studygolang 执行文件加入 PATH ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当前的工作环境的gopath设置。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

start.sh 脚本就会设置这些

6 changes: 1 addition & 5 deletions src/http/middleware/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ func AdminAuth() echo.MiddlewareFunc {
return ctx.HTML(http.StatusForbidden, `403 Forbidden`)
}

if err := next(ctx); err != nil {
return err
}

return nil
return next(ctx)
}
}
}
66 changes: 66 additions & 0 deletions src/http/middleware/csrf.go
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
}
}
}
6 changes: 4 additions & 2 deletions src/logic/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ func (UserLogic) Total() int64 {
var (
ErrUsername = errors.New("用户名不存在")
ErrPasswd = errors.New("密码错误")

ErrUnameOrPwd = errors.New("用户名或密码错误")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

告知用户名或密码错误,是怕猜测吗?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

)

// Login 登录;成功返回用户登录信息(user_login)
Expand All @@ -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
}

// 检验用户状态是否正常(未激活的可以登录,但不能发布信息)
Expand All @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions src/server/studygolang/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func main() {
serveStatic(e)

e.Use(thirdmw.EchoLogger())
e.Use(pwm.CsrfRefererFilter())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

项目中还有一个 AppGroup,也就是用于 APP 接口的,而接口没有 csrf 一说。所以,这个中间件,加到 frontG 和 adminG 上面吧。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个处理下? @momaek

Copy link
Author

Choose a reason for hiding this comment

The 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())
Expand Down