-
Notifications
You must be signed in to change notification settings - Fork 559
feat(security): add pwned password validation #2959
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
Open
AlexProgrammerDE
wants to merge
2
commits into
AuthMe:master
Choose a base branch
from
AlexProgrammerDE:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
authme-core/src/main/java/fr/xephi/authme/service/PwnedPasswordService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package fr.xephi.authme.service; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
| import fr.xephi.authme.ConsoleLogger; | ||
| import fr.xephi.authme.output.ConsoleLoggerFactory; | ||
| import fr.xephi.authme.security.HashUtils; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
| import java.net.HttpURLConnection; | ||
| import java.net.URI; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Locale; | ||
| import java.util.OptionalLong; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Queries the Have I Been Pwned Pwned Passwords range API. | ||
| */ | ||
| public class PwnedPasswordService { | ||
|
|
||
| private static final String RANGE_API_URL = "https://api.pwnedpasswords.com/range/"; | ||
| private static final String USER_AGENT = "AuthMeReloaded"; | ||
| private static final int HASH_PREFIX_LENGTH = 5; | ||
| private static final int CONNECT_TIMEOUT_MILLIS = 5_000; | ||
| private static final int READ_TIMEOUT_MILLIS = 5_000; | ||
|
|
||
| private final ConsoleLogger logger = ConsoleLoggerFactory.get(PwnedPasswordService.class); | ||
|
|
||
| /** | ||
| * Returns how many times the password appears in the Pwned Passwords database. | ||
| * | ||
| * @param password the password to check | ||
| * @return the count, 0 when absent, or empty if the API could not be queried | ||
| */ | ||
| public OptionalLong getPwnedCount(String password) { | ||
| String hash = HashUtils.sha1(password).toUpperCase(Locale.ROOT); | ||
| String hashPrefix = hash.substring(0, HASH_PREFIX_LENGTH); | ||
| String hashSuffix = hash.substring(HASH_PREFIX_LENGTH); | ||
|
|
||
| try { | ||
| return parsePwnedCount(hashSuffix, requestHashRange(hashPrefix)); | ||
| } catch (IOException e) { | ||
| logger.debug("Could not query the Pwned Passwords API: {0}", e.getMessage()); | ||
| return OptionalLong.empty(); | ||
| } | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| protected String requestHashRange(String hashPrefix) throws IOException { | ||
| HttpURLConnection connection = (HttpURLConnection) URI.create(RANGE_API_URL + hashPrefix) | ||
| .toURL() | ||
| .openConnection(); | ||
| connection.setRequestMethod("GET"); | ||
| connection.setRequestProperty("User-Agent", USER_AGENT); | ||
| connection.setRequestProperty("Add-Padding", "true"); | ||
| connection.setConnectTimeout(CONNECT_TIMEOUT_MILLIS); | ||
| connection.setReadTimeout(READ_TIMEOUT_MILLIS); | ||
|
|
||
| try { | ||
| int responseCode = connection.getResponseCode(); | ||
| if (responseCode != HttpURLConnection.HTTP_OK) { | ||
| throw new IOException("HTTP " + responseCode); | ||
| } | ||
|
|
||
| try (BufferedReader reader = new BufferedReader( | ||
| new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) { | ||
| return reader.lines().collect(Collectors.joining("\n")); | ||
| } | ||
| } finally { | ||
| connection.disconnect(); | ||
| } | ||
| } | ||
|
|
||
| private OptionalLong parsePwnedCount(String searchedHashSuffix, String response) { | ||
| String[] entries = response.split("\\R"); | ||
| for (String entry : entries) { | ||
| int delimiterIndex = entry.indexOf(':'); | ||
| if (delimiterIndex < 0) { | ||
| continue; | ||
| } | ||
|
|
||
| String hashSuffix = entry.substring(0, delimiterIndex); | ||
| if (hashSuffix.equalsIgnoreCase(searchedHashSuffix)) { | ||
| return parseCount(entry.substring(delimiterIndex + 1)); | ||
| } | ||
| } | ||
| return OptionalLong.of(0); | ||
| } | ||
|
|
||
| private OptionalLong parseCount(String count) { | ||
| try { | ||
| return OptionalLong.of(Long.parseLong(count.trim())); | ||
| } catch (NumberFormatException e) { | ||
| logger.debug("Could not parse Pwned Passwords count: {0}", count); | ||
| return OptionalLong.empty(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
authme-core/src/test/java/fr/xephi/authme/service/PwnedPasswordServiceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package fr.xephi.authme.service; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.OptionalLong; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
|
|
||
| /** | ||
| * Test for {@link PwnedPasswordService}. | ||
| */ | ||
| class PwnedPasswordServiceTest { | ||
|
|
||
| @Test | ||
| void shouldReturnPwnedCountForMatchingPasswordHash() { | ||
| // given | ||
| TestPwnedPasswordService service = new TestPwnedPasswordService( | ||
| "003D68EB55068C33ACE09247EE4C639306B:1\n" | ||
| + "1E4C9B93F3F0682250B6CF8331B7EE68FD8:12345\n" | ||
| + "FFFFF0AC487871FEEC1891C490136E006E2:0"); | ||
|
|
||
| // when | ||
| OptionalLong count = service.getPwnedCount("password"); | ||
|
|
||
| // then | ||
| assertThat(count.isPresent(), equalTo(true)); | ||
| assertThat(count.getAsLong(), equalTo(12345L)); | ||
| assertThat(service.requestedHashPrefix, equalTo("5BAA6")); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldReturnZeroWhenPasswordHashIsAbsent() { | ||
| // given | ||
| TestPwnedPasswordService service = new TestPwnedPasswordService( | ||
| "003D68EB55068C33ACE09247EE4C639306B:1\n" | ||
| + "FFFFF0AC487871FEEC1891C490136E006E2:0"); | ||
|
|
||
| // when | ||
| OptionalLong count = service.getPwnedCount("password"); | ||
|
|
||
| // then | ||
| assertThat(count.isPresent(), equalTo(true)); | ||
| assertThat(count.getAsLong(), equalTo(0L)); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldReturnEmptyWhenRangeRequestFails() { | ||
| // given | ||
| TestPwnedPasswordService service = new TestPwnedPasswordService(new IOException("boom")); | ||
|
|
||
| // when | ||
| OptionalLong count = service.getPwnedCount("password"); | ||
|
|
||
| // then | ||
| assertThat(count.isPresent(), equalTo(false)); | ||
| } | ||
|
|
||
| private static final class TestPwnedPasswordService extends PwnedPasswordService { | ||
| private final String response; | ||
| private final IOException exception; | ||
| private String requestedHashPrefix; | ||
|
|
||
| private TestPwnedPasswordService(String response) { | ||
| this.response = response; | ||
| this.exception = null; | ||
| } | ||
|
|
||
| private TestPwnedPasswordService(IOException exception) { | ||
| this.response = null; | ||
| this.exception = exception; | ||
| } | ||
|
|
||
| @Override | ||
| protected String requestHashRange(String hashPrefix) throws IOException { | ||
| requestedHashPrefix = hashPrefix; | ||
| if (exception != null) { | ||
| throw exception; | ||
| } | ||
| return response; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.