1

I finished doing finance and tested it without many errors i would say. Fun fact, i was implementing a change password and it got me this error

TypeError: a bytes-like object is required, not 'NoneType'

@app.route("/change_password", methods=["GET", "POST"])
@login_required
def change_password():

if request.method == "POST":

    # Get info from fields
    old_pw = request.form.get("old_password")
    new_pw = request.form.get("new_password")
    confirm_password = request.form.get("confirm_password")

    # Ensure fields are not empty
    if not (old_pw or new_pw or confirm_password):
        return apology("Field missing", 403)

    # Check that both new password are correctly spell
    elif not confirm_password == new_pw:
        return apology("passwords must be equal", 403)

    # Ensure old and new password are different
    elif new_pw == old_pw:
        return apology("New password cannot be the same", 403)

    # Query db for password
    rows = db.execute("SELECT hash FROM users WHERE id = :user_id", user_id=session["user_id"])

    # Check password is correct
    if len(rows) != 1 or not check_password_hash(rows[0]["hash"], old_pw):
        return apology("Invalid password", 403)

    # Query db to update password
    db.execute("UPDATE users SET hash = :password WHERE id = :user_id",
                password=generate_password_hash(new_pw), user_id=session["user_id"])

    flash("Password Changed Successfully")
    return redirect("/")

else:
    return render_template("change_pw.html")

this is the log

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.7/site-packages/werkzeug/middleware/proxy_fix.py", line 232, in __call__
    return self.app(environ, start_response)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/ubuntu/cs50/set8/finance/helpers.py", line 34, in decorated_function
    return f(*args, **kwargs)
  File "/home/ubuntu/cs50/set8/finance/application.py", line 206, in change_password
    if len(rows) != 1 or not check_password_hash(rows[0]["hash"], request.form.get("old_password")):
  File "/usr/local/lib/python3.7/site-packages/werkzeug/security.py", line 221, in check_password_hash
    return safe_str_cmp(_hash_internal(method, salt, password)[0], hashval)
  File "/usr/local/lib/python3.7/site-packages/werkzeug/security.py", line 153, in _hash_internal
    rv = pbkdf2_hex(password, salt, iterations, hashfunc=method)
  File "/usr/local/lib/python3.7/site-packages/werkzeug/security.py", line 53, in pbkdf2_hex
    rv = pbkdf2_bin(data, salt, iterations, keylen, hashfunc)
  File "/usr/local/lib/python3.7/site-packages/werkzeug/security.py", line 87, in pbkdf2_bin
    return hashlib.pbkdf2_hmac(hash_name, data, salt, iterations, keylen)
TypeError: a bytes-like object is required, not 'NoneType'

1 Answer 1

1

I think old_pw has a value of None. You write

if not (old_pw or new_pw or confirm_password):

but mean

if not old_pw or not new_pw or not confirm_password:

or equivalently

if not (old_pw and new_pw and confirm_password):

Edit:

In code not provided in the question, there was a typo in name="old_passwrod", which would cause request.form.get("old_password") to return the default value of None. I had hoped that fixing the check, giving you the correct error message of "Field missing", would point you to that kind of mistake.

4
  • that was indeed a bug but it stills returns error. It passes every validation. I think it has to do with the hash function.. but i cannot figure out what is it.
    – Inju69
    Feb 11, 2020 at 12:30
  • Errors would not affect the checker, as /change_password is your "personal touch" feature. Is it still the same error, or a different one? What makes you so sure the problem is with the Python function, and not how it is called by the form in change_pw.html?
    – Blauelf
    Feb 11, 2020 at 12:53
  • <form action="/change_password" method="post"> <div class="form-group"> <input class="form-control" name="old_passwrod" placeholder="Old password" type="password"> </div> <div class="form-group"> <input class="form-control" name="new_password" placeholder="New Password" type="password"> </div> <div class="form-group"> <input class="form-control" name="confirm_password" placeholder="Confirm New Password" type="password"> </div> <button class="btn btn-primary" type="submit">Change Password</button> </form>
    – Inju69
    Feb 12, 2020 at 13:17
  • Edited my answer.
    – Blauelf
    Feb 12, 2020 at 13:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .