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

LUT-28079: Fixed SONAR issue by updating the hash algorithm used #444

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
*/
package fr.paris.lutece.portal.service.security.impl;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

Expand Down Expand Up @@ -64,8 +65,11 @@
public final class DefaultAccessLogger implements IAccessLogger
{

private static final String CONSTANT_HASH_ENCODING = "UTF-8";
private static final String CONSTANT_HASH_DIGEST = "MD5";
private static final Charset CONSTANT_HASH_ENCODING = StandardCharsets.UTF_8;
private static final String CONSTANT_HASH_DIGEST = "SHA-256";
// Amount of characters in the hashed value
private static final int CONSTANT_HASH_CHARACTERS_COUNT = 64;

private static final String PROPERTY_ADD_HASH_TO_LOGS = "accessLogger.defaultAccessLogger.addHashToLogs";
private static final String PROPERTY_ACCESSLOG_MESSAGE_FORMAT = "accessLogger.defaultAccessLogger.messageFormat";
private static final String PROPERTY_ACCESSLOG_MESSAGE_FORMAT_SEPARATOR = "accessLogger.defaultAccessLogger.messageFormatSeparator";
Expand Down Expand Up @@ -238,7 +242,7 @@ private static String getHash( String message )
return sb.toString( );

}
catch( UnsupportedEncodingException | NoSuchAlgorithmException e )
catch( NoSuchAlgorithmException e )
{
return "Hash ERROR : " + e.getLocalizedMessage( );
}
Expand All @@ -258,8 +262,10 @@ public boolean verifyMessageHash( String message )
{

int idx = message.indexOf( _messageFormatSeparator, message.indexOf( DEFAULT_LOGGER_ACCESS_LOG ) );
String hash = message.substring( idx + 1, idx + 33 );
String data = "||" + message.substring( idx + 34 );
// Get the hash's value
String hash = message.substring( idx + 1, idx + CONSTANT_HASH_CHARACTERS_COUNT + 1 );
// Get the actual data to verify
String data = "||" + message.substring( idx + CONSTANT_HASH_CHARACTERS_COUNT + 2 );

return ( hash != null && ( hash.equals( "" ) || hash.equals( getHash( data ) ) ) );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
public final class DefaultAccessLoggerTest extends LuteceTestCase
{

private final String TEST_MESSAGE_TRUE = "09/04/21 15:59:13 INFO [http-nio-8080-exec-9] lutece.accessLogger - |ffa2bcacdcc721223d1a7211f273ec15|<the application code>|CONNECT|user.loginAdminUser|BO:admin||";
private final String TEST_MESSAGE_FALSE = "09/04/21 15:59:13 INFO [http-nio-8080-exec-9] lutece.accessLogger - |badhashhhhhhhhhhhhhhhhhhhhhhhhhh|<the application code>|CONNECT|user.loginAdminUser|BO:admin||";
private final String TEST_MESSAGE_TRUE = "09/04/21 15:59:13 INFO [http-nio-8080-exec-9] lutece.accessLogger - |3f40eac29d54799452cefbfa6c834b118ee9059df2e7ebe37554d39c7d78ff89|<the application code>|CONNECT|user.loginAdminUser|BO:admin|Our data|";
private final String TEST_MESSAGE_FALSE = "09/04/21 15:59:13 INFO [http-nio-8080-exec-9] lutece.accessLogger - |3f40eac29d54799452cefbfa6c834b118ee9059df2e7ebe37554d39c7d78ff89|<the application code>|CONNECT|user.loginAdminUser|BO:admin|Our altered data|";

/**
* test valid hash
Expand Down