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

Update Sha2Crypt.java #300

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

Update Sha2Crypt.java #300

wants to merge 1 commit into from

Conversation

kangxy
Copy link

@kangxy kangxy commented Jul 29, 2024

The old regular expression is incorrect.
If you want to match [a-zA-Z0-9./] and limit the length to 16, I think it should be the following expression: "^\$([56])\$(rounds=(\d+)\$)?([\.\/a-zA-Z0-9]{1,16})$")

The old regular expression is incorrect.
If you want to match [a-zA-Z0-9./] and limit the length to 16, I think it should be the following expression:
"^\\$([56])\\$(rounds=(\\d+)\\$)?([\\.\\/a-zA-Z0-9]{1,16})$")
@garydgregory
Copy link
Member

@kangxy

There are no tests in this PR.
You'll want a test that fails if there are no changes to the main directory.

@sebbASF
Copy link
Contributor

sebbASF commented Jul 29, 2024

I agree that the regex looks wrong. As it stands, it will accept any salt that has at least one alphanumeric character after the $[56]$ prefix.

However, the Javadoc for Crypt.crypt(String,String) [1] says:

" ... It is therefore valid to enter a complete hash value as salt ..."

This would not be possible if the regex was changed as per this PR.

[1] https://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/digest/Crypt.html#crypt(java.lang.String,java.lang.String)

@sebbASF sebbASF marked this pull request as draft July 29, 2024 20:33
@kangxy kangxy marked this pull request as ready for review July 30, 2024 09:30
@sebbASF
Copy link
Contributor

sebbASF commented Jul 30, 2024

I've added some more tests to CryptTest based on the Javadoc.
Some fail if the RE is terminated after the salt characters.

The Javadoc says that '$' will terminate the salt.
At present arbitrary characters such as '%' will silently terminate the salt; this seems wrong.
But equally disallowing the '$hash' suffix is wrong.

An alternative might be to allow an optional '$' followed by any characters.

For example, replace '.' at the end of the RE with '($|\$.)'

@sebbASF sebbASF marked this pull request as draft July 30, 2024 11:34
@sebbASF
Copy link
Contributor

sebbASF commented Jul 30, 2024

The proposed RE does not work because it does not allow for truncating the salt after 16 characters, as per the following test failures:

Sha256CryptTest.testSha256CryptStrings:63 » IllegalArgument Invalid salt value: $5$1234567890123456789
Sha512CryptTest.testSha512CryptStrings:98 » IllegalArgument Invalid salt value: $6$1234567890123456789

There needs to be further work before the RE can be made stricter.

@kangxy kangxy marked this pull request as ready for review July 31, 2024 01:42
@kangxy
Copy link
Author

kangxy commented Jul 31, 2024

The proposed RE does not work because it does not allow for truncating the salt after 16 characters, as per the following test failures:

Sha256CryptTest.testSha256CryptStrings:63 » IllegalArgument Invalid salt value: $5$1234567890123456789 Sha512CryptTest.testSha512CryptStrings:98 » IllegalArgument Invalid salt value: $6$1234567890123456789

There needs to be further work before the RE can be made stricter.

Both of the above salt values are wrong. We need the salt to be 16 bytes or less

@sebbASF
Copy link
Contributor

sebbASF commented Jul 31, 2024

Both of the above salt values are wrong.

Not so, see below.

We need the salt to be 16 bytes or less

Agreed.

The JavaDoc for Crypt includes the phrase " ... and is cut at the maximum length ...."

See

It should be possible to allow for a longer salt, but it may be tricky to do so in a single regex.

The first task is to document exactly what should be allowed and what is not allowed.
Only then can tests be written and code amended.

@sebbASF sebbASF marked this pull request as draft July 31, 2024 10:17
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.

3 participants