Repository navigation
User attribute value length extension #26196
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ac15402
User attribute value length extension
vramik a3feb87
Checking for possible hash collisions
vramik 521f424
Limitation of user attribute lenght.
vramik f056351
Review
ahus1 300cacb
Revert "Limitation of user attribute lenght."
ahus1 d3eacc5
Updated docs
ahus1 9f230b4
Migrate long values in for MaraiaDB and MySQL to the new table structure
ahus1 f31413c
Limit attribute values by default to 2048 characters
ahus1 eb69ae6
Avoid strange binary characters as they fail on MySQL
ahus1 c9f2c97
Add user attribute configuration to allow storing a photo
ahus1 495103d
Adding a note about the indexes
ahus1 42107b6
Refactor JpaUserProvider hash collision check
mhajas 108a96f
Fix issues where Unicode could mix up our String length counting
ahus1 ac80856
Fimpmlified method with a direct return
ahus1 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
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
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
10 changes: 10 additions & 0 deletions
10
docs/documentation/server_admin/topics/threat/validate-user-attributes.adoc
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,10 @@ | ||
| [[validate_user_attributes]] | ||
| === Validate user attributes | ||
|
|
||
| With the functionality in <<user-profile>>, administrators can restrict the data users enter for attributes, for example, in user registration or the account console. | ||
|
|
||
| Administrators should not allow unmanaged attributes for users to prevent attackers adding an unlimited number of attributes. | ||
| Attributes should have a validation that restricts the amount of data entered by attackers. | ||
|
|
||
| When using regular expressions to validate user attributes, avoid regular expressions that use an excessive amount of memory or CPU. | ||
| See https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS[OWASP's Regular expression Denial of Service] for details. |
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
72 changes: 72 additions & 0 deletions
72
...k/connections/jpa/updater/liquibase/custom/FederatedUserAttributeTextColumnMigration.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,72 @@ | ||
| /* | ||
| * Copyright 2018 Red Hat, Inc. and/or its affiliates | ||
| * and other contributors as indicated by the @author tags. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.keycloak.connections.jpa.updater.liquibase.custom; | ||
|
|
||
| import liquibase.database.core.MySQLDatabase; | ||
| import liquibase.exception.CustomChangeException; | ||
| import liquibase.statement.core.RawParameterizedSqlStatement; | ||
| import org.keycloak.storage.jpa.JpaHashUtils; | ||
|
|
||
| import java.sql.PreparedStatement; | ||
| import java.sql.ResultSet; | ||
|
|
||
| /** | ||
| * The MySQL database is the only database where columns longer than 255 characters are changed to a TEXT column, allowing | ||
| * for up to 64k characters. See {@link org.keycloak.connections.jpa.updater.liquibase.MySQL8VarcharType} for the implementation. | ||
| * As the new code expects all information longer than 2024 characters in the new column, this migration copies over the values. | ||
| * | ||
| * @author Alexander Schwartz | ||
| */ | ||
| public class FederatedUserAttributeTextColumnMigration extends CustomKeycloakTask { | ||
|
|
||
| @Override | ||
| protected void generateStatementsImpl() throws CustomChangeException { | ||
|
|
||
| if (database instanceof MySQLDatabase) { | ||
|
|
||
| try (PreparedStatement ps = connection.prepareStatement("SELECT t.ID, t.VALUE" + | ||
| " FROM " + getTableName("FED_USER_ATTRIBUTE") + " t" + | ||
| " WHERE LENGTH(t.VALUE) > 2024"); | ||
| ResultSet resultSet = ps.executeQuery() | ||
| ) { | ||
| while (resultSet.next()) { | ||
| String id = resultSet.getString(1); | ||
| String value = resultSet.getString(2); | ||
| // The SQL LENGTH() will count bytes, where Java's length() will count Unicode characters. | ||
| // There's also SQL CHAR_LENGTH() which is probably equivalent to Java's Character.codePointCount(), | ||
| // but it is not a fit here as we're not using code points in the JPA entities | ||
| if (value.length() > 2024) { | ||
| statements.add(new RawParameterizedSqlStatement("UPDATE " + getTableName("FED_USER_ATTRIBUTE") + " SET VALUE = null, LONG_VALUE_HASH = ?, LONG_VALUE_HASH_LOWER_CASE = ?, LONG_VALUE = ? WHERE ID = ?", | ||
| JpaHashUtils.hashForAttributeValue(value), | ||
| JpaHashUtils.hashForAttributeValueLowerCase(value), | ||
| value, | ||
| id)); | ||
| } | ||
| } | ||
| } catch (Exception e) { | ||
| throw new CustomChangeException(getTaskId() + ": Exception when updating data from previous version", e); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| protected String getTaskId() { | ||
| return "Leave only single offline session per user and client"; | ||
| } | ||
|
|
||
| } |
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
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.