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

fix: Proper exception handling of InvalidPasswordLength #5279

Merged

Conversation

prateekj117
Copy link
Contributor

@prateekj117 prateekj117 commented May 24, 2020

Status

Ready for review

Description of Changes

When entering passphrase of more than 128 (max_limit) characters, the user lands to the Internal Server Error Page.

Fixes #5145

Changes proposed in this pull request:

Handle the exception properly so that the server returns with the flashed error message.

Testing

  • check out this branch
  • Run make dev - it completes successfully
  • Attempt to log into the journalist interface on localhost:8081 with a valid username <userName> and TOTP code, and a password longer than 127 characters:
    • Login fails with Login failed. Please wait for a new code from your two-factor mobile app or security key before trying again message.
    • Error recorded in Docker output with contents: [<timestamp>] ERROR in utils: Login for '<userName>' failed: invalid password
  • Attempt to log in to the JI with valid username, TOTP, and password:
    • Login succeeds.

Deployment

Checklist

If you made changes to the server application code:

  • Linting (make lint) and tests (make test) pass in the development container

If you made non-trivial code changes:

  • I have written a test plan and validated it for this PR

@zenmonkeykstop
Copy link
Contributor

Hi @prateekj117 and thanks for this fix! I've added a simple test plan to the PR description, hope that's OK. In general, PRs to this project should include the sections laid out in the template - it helps speed the review process.

The change looks good at first glance, but if you have time, could you add a test to cover this behaviour? Take a look at this test for an example.

@prateekj117
Copy link
Contributor Author

@zenmonkeykstop Ya, sure. I will keep that in mind. I will add the test as well. Thanks for the review !!!

Copy link
Contributor

@zenmonkeykstop zenmonkeykstop left a comment

Choose a reason for hiding this comment

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

Change looks good, a test covering the behavior change in this case would be great.

@rmol
Copy link
Contributor

rmol commented May 26, 2020

@prateekj117 Maybe I can save you some time. This could go in tests/test_journalist.py:

def test_login_password_too_long(journalist_app, test_journo, mocker):
    mocked_error_logger = mocker.patch('journalist.app.logger.error')
    with journalist_app.test_client() as app:
        resp = app.post(url_for('main.login'),
                        data=dict(username=test_journo['username'],
                                  password='a' * (Journalist.MAX_PASSWORD_LEN + 1),
                                  token=TOTP(test_journo['otp_secret']).now()))
    assert resp.status_code == 200
    text = resp.data.decode('utf-8')
    assert "Login failed" in text
    mocked_error_logger.assert_called_once_with(
        "Login for '{}' failed: Password too long (len={})".format(
            test_journo['username'], Journalist.MAX_PASSWORD_LEN + 1))

@prateekj117
Copy link
Contributor Author

@rmol Thanks a lot. Though I would have definitely enjoyed writting this logic myself and learning a few more things along the way.

@rmol
Copy link
Contributor

rmol commented May 26, 2020

@prateekj117 Well, drat. I could delete the comment. 😉

That's good to know; I will not deprive you of the pleasure of writing tests for your next PR. 🙂

@prateekj117
Copy link
Contributor Author

@zenmonkeykstop Error recorded in Docker output is ERROR in utils: Login for 'journalist' failed: Password too long (len=624), rather than the one you mentioned in PR template. Not an issue though I think.

@rmol Thanks for the test.

Copy link
Contributor

@zenmonkeykstop zenmonkeykstop left a comment

Choose a reason for hiding this comment

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

CI is passing with the new test, this is good to go!

@zenmonkeykstop zenmonkeykstop merged commit 7580e98 into freedomofpress:develop May 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

journalist login with long passphrase causes internal server error
3 participants