Skip to content

fix(isHashMatch): check that hash starts with $ #3698

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

Merged
merged 1 commit into from
Jul 1, 2021

Conversation

jsjoeio
Copy link
Contributor

@jsjoeio jsjoeio commented Jun 30, 2021

This PR adds an additional check to isHashMatch to check that the hash starts with a "$" to prevent us from calling argon2.verify if it doesn't.

It also throws an error if something goes wrong with the .verify() function.

Why do we need to add this?

The other day I noticed this error: 'TypeError: pchstr must contain a $ as first char\n'

I believe it happens because we're still in between a migration from the old hashing function sha256 to the new one argon2. And locally, I switch between the latest code-server and the development one. This means it may be calling the function to verify the hash expecting it to be a hash made with argon2 and instead it's made with sha256.

Once we release the newest version of code-server with the argon2 algorithm, we don't want it to throw unexpected errors, like this one. We may need to revisit some of the logic for verifying password hashes in case there are issues.

Either way, this adds an extra check to prevent this error from happening.

Fixes N/A

@jsjoeio jsjoeio marked this pull request as ready for review June 30, 2021 19:33
@jsjoeio jsjoeio requested a review from a team as a code owner June 30, 2021 19:33
@codecov
Copy link

codecov bot commented Jun 30, 2021

Codecov Report

Merging #3698 (7a3ac81) into main (e9d4f87) will increase coverage by 0.08%.
The diff coverage is 100.00%.

❗ Current head 7a3ac81 differs from pull request most recent head 7f12fab. Consider uploading reports for the commit 7f12fab to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3698      +/-   ##
==========================================
+ Coverage   60.13%   60.22%   +0.08%     
==========================================
  Files          35       35              
  Lines        1811     1810       -1     
  Branches      365      365              
==========================================
+ Hits         1089     1090       +1     
+ Misses        606      604       -2     
  Partials      116      116              
Impacted Files Coverage Δ
src/node/util.ts 71.87% <100.00%> (+0.89%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e9d4f87...7f12fab. Read the comment docs.

Previously, we used argon2 to verify the hash with the password.

If the hash didn't start with a $, then it would enter the catch block.

Now we check the hash before trying to verify it and we also throw an Error if
the verify fails.

This makes the isHashMatch function more robust.
@jsjoeio jsjoeio force-pushed the jsjoeio-fix-argon-issue branch from 0e6096e to 7f12fab Compare June 30, 2021 22:00
@jsjoeio jsjoeio self-assigned this Jun 30, 2021
@jsjoeio jsjoeio added chore Related to maintenance or clean up testing Anything related to testing labels Jun 30, 2021
@jsjoeio jsjoeio added this to the 3.11.0 milestone Jun 30, 2021
Copy link
Member

@kylecarbs kylecarbs left a comment

Choose a reason for hiding this comment

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

Seems like a reasonable check, but I'm not entire sure why we need to do this.

Copy link
Contributor

@jawnsy jawnsy left a comment

Choose a reason for hiding this comment

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

Seems reasonable. 😄

expect(await util.isHashMatch(password, _hash)).toBe(false)
})
it("should reject the promise and throw if error", async () => {
const password = "hellowpasssword"
Copy link
Contributor

Choose a reason for hiding this comment

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

Just curious - what does it mean when the hash begins with a $? Is it used as a signifier to designate that the hash is Argon2? I had thought that the other test case was an Argon2 hash, too, so this is a little confusing for me

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just curious - what does it mean when the hash begins with a $? Is it used as a signifier to designate that the hash is Argon2?

AFAIK, I believe that's exactly why.

had thought that the other test case was an Argon2 hash, too, so this is a little confusing for me

If you point out which test case you're referring to, happening to clear up any confusion!

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry I meant that this:

    const _hash = "n2i$v=19$m=4096,t=3,p=1$EAoczTxVki21JDfIZpTUxg$rkXgyrW4RDGoDYrxBFD4H2DlSMEhP4h+Api1hXnGnFY"

Looks like a valid hash to me, but I guess it's not? What is this hash?

I guess it makes sense that it has to start with $ since a valid hash looks like const _hash = "$argon2i$v=19$m=4096,t=3,p=1$EAoczTxVki21JDfIZpTUxg$rkXgyrW4RDGoDYrxBFD4H2DlSMEhP4h+Api1hXnGnFY"?

Should we look for "$argon2i$" instead of just the $?

@jsjoeio
Copy link
Contributor Author

jsjoeio commented Jul 1, 2021

Seems like a reasonable check, but I'm not entire sure why we need to do this.

My fault. I updated the PR description with the why behind this.

@jsjoeio
Copy link
Contributor Author

jsjoeio commented Jul 1, 2021

Side note: @code-asher brought up a good point about UX here so I opened another issue: #3704

@jsjoeio jsjoeio merged commit faa896c into main Jul 1, 2021
@jsjoeio jsjoeio deleted the jsjoeio-fix-argon-issue branch July 1, 2021 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chore Related to maintenance or clean up testing Anything related to testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants