Skip to content
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

Failed authentication are now properly logged #2334

Merged
merged 1 commit into from
Aug 24, 2017

Conversation

daviian
Copy link
Member

@daviian daviian commented Aug 18, 2017

Targets #2301

Failed authentication now returns 401 status code.
Done by introducing a new RenderFormWithErr function in context to allow passing of a statuscode

Tested in following browsers:

  • Latest version of FF, Chrome and Safari Mac
  • IE 11
  • Chrome for Android Latest
  • Microsoft Edge 12

@lunny lunny added the type/enhancement An improvement of existing functionality label Aug 19, 2017
@lunny lunny added this to the 1.x.x milestone Aug 19, 2017
@daviian daviian force-pushed the auth-failed-statuscode branch 4 times, most recently from 53a657d to 6852aab Compare August 20, 2017 00:59
@lafriks
Copy link
Member

lafriks commented Aug 20, 2017

LGTM

@tboerger tboerger added the lgtm/need 1 This PR needs approval from one additional maintainer to be merged. label Aug 20, 2017
@lunny
Copy link
Member

lunny commented Aug 20, 2017

It seems CI status is disappeared?

@lafriks
Copy link
Member

lafriks commented Aug 20, 2017

@lunny yes it was there until recent force push when he fixed my suggested in discord changes :) Don't know how Drone status got lost

{username: "user1@example.com", password: "wrongPassword", errorMessage: i18n.Tr("en", "form.username_password_incorrect")},
}

prepareTestEnv(t)
Copy link
Member Author

Choose a reason for hiding this comment

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

Shouldn't this be in the testLoginFailed function because each call represents a seperate test case!?

Copy link
Member

Choose a reason for hiding this comment

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

if test-case would change data that need to be reset than yes, otherwise it will only make tests run slower.

Copy link
Member

@ethantkoenig ethantkoenig left a comment

Choose a reason for hiding this comment

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

I'm not sure returning a 401 on failed login is the correct thing to do; see https://stackoverflow.com/questions/8273757/should-a-failed-login-attempt-result-in-a-http-401-response

I've investigated several public-facing sites (Google, Amazon, Instagram, Github, Gitlab), and none of them returned 401 on failed login.

@@ -218,3 +218,33 @@
avatar_email: user13@example.com
num_repos: 3
is_active: true

Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason we can't just use existing user fixtures? user16 isn't used anywhere, and it seems we could use any existing user in place of user15 in TestSignin.

Copy link
Member Author

@daviian daviian Aug 20, 2017

Choose a reason for hiding this comment

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

Didn't want to intervene in other test cases because of duplicate email address.
But if that's not the case of course I can remove user16.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I missed that user15 and user16 had the same email addresses; sorry about that.

Instead of introducing two new fixtures for testing the duplicate email case, could we instead do something like:

func TestDuplicateEmailFailedLogin(t *testing.T) {
	prepareTestEnv(t)
	user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)

	// add new user with user2's email
	user.Name = "testuser"
	user.LowerName = strings.ToLower(user.Name)
	user.ID = 0 // will be auto-populated on insert
	models.AssertSuccessfulInsert(t, user)
	
	// TODO try to login with user.Email, should fail with duplicate email
}

It's not really scalable for us to add new test fixtures for every corner case we need to test.

Copy link
Member Author

Choose a reason for hiding this comment

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

If others feel the same way I will change that ;-)

ctx.Data["Flash"] = ctx.Flash
ctx.HTML(200, tpl)
ctx.Flash.Error(msg, true)
ctx.HTML(status, tpl)
Copy link
Member

Choose a reason for hiding this comment

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

Why did you remove the ctx.Data["Flash"] = ctx.Flash line?

Copy link
Member

Choose a reason for hiding this comment

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

@ethantkoenig ctx.Flash.Error(...) sets ctx.Data["Flash"]

Copy link
Member Author

Choose a reason for hiding this comment

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

@lafriks @ethantkoenig Only if second parameter is true

@daviian
Copy link
Member Author

daviian commented Aug 20, 2017

The main reason for 401 status code was to enable easier fail2ban configuration.
An alternative would be to only log the failed authentication attempt in an easy to distinguish way into the gitea log if that pleases you more ;-)

@lafriks
Copy link
Member

lafriks commented Aug 20, 2017

I'm now thinking that entry to gitea log would be better than changing response status code

@ethantkoenig
Copy link
Member

@daviian @lafriks I would prefer a log message as well

@daviian
Copy link
Member Author

daviian commented Aug 20, 2017

@lafriks @ethantkoenig Then it's settled. I will change PR.

@daviian
Copy link
Member Author

daviian commented Aug 22, 2017

@lafriks @ethantkoenig Which log level would you say is appropriate? Or is there a way to log independent from the set log level?

@ethantkoenig
Copy link
Member

@daviian I would say "Info", because I think it makes sense to reserve "Warning" and "Error" for actionable entries

@lafriks
Copy link
Member

lafriks commented Aug 23, 2017

@ethantkoenig but than fail2ban can be only used by enabling only at least info log level that is not best option. And in case of fail2ban usage failed authorization is actionable entry

@daviian
Copy link
Member Author

daviian commented Aug 23, 2017

@ethantkoenig @lafriks I was also struggling with this, but I think users who want to use fail2ban have to set the necessary log level either way. And I agree that Info is the best choice.

Signed-off-by: David Schneiderbauer <dschneiderbauer@gmail.com>
@lafriks
Copy link
Member

lafriks commented Aug 23, 2017

Why did you remove integration tests? These were useful anyway

@daviian
Copy link
Member Author

daviian commented Aug 23, 2017

I thought that they are not part of the PR if it is only logging. But I can add them again.

@lafriks
Copy link
Member

lafriks commented Aug 23, 2017

You could at least submit them in other PR then, we need tests :)

@daviian
Copy link
Member Author

daviian commented Aug 23, 2017

And regarding the log level. I am open for discussion ;-) It's not cast in stone yet ;-)

@lafriks
Copy link
Member

lafriks commented Aug 23, 2017

@ethantkoenig need your approval

@bkcsoft bkcsoft changed the title Failed authentication now returns 401 status code Failed authentication are now properly logged Aug 24, 2017
@bkcsoft
Copy link
Member

bkcsoft commented Aug 24, 2017

Changed the title to reflect the change 🙂

Copy link
Member

@ethantkoenig ethantkoenig left a comment

Choose a reason for hiding this comment

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

LGTM

@ethantkoenig
Copy link
Member

Let's try again LGTM

@tboerger tboerger added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Aug 24, 2017
@lafriks lafriks merged commit 174255e into go-gitea:master Aug 24, 2017
@lafriks lafriks modified the milestones: 1.2.0, 1.x.x, 1.3.0 Aug 24, 2017
@daviian daviian mentioned this pull request Aug 24, 2017
7 tasks
@daviian daviian deleted the auth-failed-statuscode branch August 24, 2017 09:42
@go-gitea go-gitea locked and limited conversation to collaborators Nov 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/enhancement An improvement of existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants