Skip to content

Commit 49858c3

Browse files
frank-msodspfmarasco
andauthored
Added JNI Wrappers for Sanitizer (#1359)
* Added JNI Wrappers for Sanitizer * revert version * fixed casing --------- Co-authored-by: fmarasco <fmarasco+odspmdb@microsoft.com>
1 parent 1ab6eaf commit 49858c3

File tree

6 files changed

+231
-1
lines changed

6 files changed

+231
-1
lines changed

lib/android_build/maesdk/src/main/cpp/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ set(CMAKE_CXX_STANDARD 11)
1010
option(BUILD_AZMON "Build for Azure Monitor" YES)
1111
option(BUILD_PRIVACYGUARD "Build Privacy Guard" YES)
1212
option(BUILD_SIGNALS "Build Signals" YES)
13+
option(BUILD_SANITIZER "Build Sanitizer" YES)
1314

1415
if(ENABLE_CAPI_HTTP_CLIENT)
1516
add_definitions(-DENABLE_CAPI_HTTP_CLIENT)
@@ -113,6 +114,22 @@ if (EXISTS ${SDK_ROOT}/lib/modules/signals/ AND BUILD_SIGNALS)
113114
)
114115
endif()
115116

117+
if (EXISTS ${SDK_ROOT}/lib/modules/sanitizer/ AND BUILD_SANITIZER)
118+
list(APPEND SRCS
119+
${SDK_ROOT}/lib/jni/Sanitizer_jni.cpp
120+
${SDK_ROOT}/lib/modules/sanitizer/detectors/EmailAddressDetector.cpp
121+
${SDK_ROOT}/lib/modules/sanitizer/detectors/JwtDetector.cpp
122+
${SDK_ROOT}/lib/modules/sanitizer/detectors/SPOPassword.cpp
123+
${SDK_ROOT}/lib/modules/sanitizer/detectors/UrlDetector.cpp
124+
${SDK_ROOT}/lib/modules/sanitizer/Sanitizer.cpp
125+
${SDK_ROOT}/lib/modules/sanitizer/SanitizerProvider.cpp
126+
${SDK_ROOT}/lib/modules/sanitizer/SanitizerStringUtils.cpp
127+
${SDK_ROOT}/lib/modules/sanitizer/SanitizerTargets.cpp
128+
${SDK_ROOT}/lib/modules/sanitizer/SanitizerTrie.cpp
129+
${SDK_ROOT}/lib/modules/sanitizer/SanitizerTrieNode.cpp
130+
)
131+
endif()
132+
116133
if (USE_ROOM)
117134
add_definitions("-DUSE_ROOM")
118135
list(APPEND SRCS ${SDK_ROOT}/lib/offline/OfflineStorage_Room.cpp)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
package com.microsoft.applications.events;
6+
7+
public class Sanitizer {
8+
9+
/**
10+
* Initializes the sanitizer with the provided configuration.
11+
*
12+
* @param config The configuration object containing logger and event name.
13+
* @return true if initialization succeeds, false otherwise.
14+
* @throws IllegalArgumentException if config or any required field is null or invalid.
15+
*/
16+
public static boolean initialize(SanitizerConfiguration config) {
17+
18+
// Validate that the configuration object is not null
19+
if(config == null) {
20+
throw new IllegalArgumentException("initConfig cannot be null");
21+
}
22+
23+
// Ensure the logger instance is provided
24+
if(config.loggerInstance == null) {
25+
throw new IllegalArgumentException(("loggerInstance cannot be null in config."));
26+
}
27+
28+
// Ensure the notification event name is not null or empty
29+
if (config.notificationEventName == null || config.notificationEventName.isEmpty()) {
30+
throw new IllegalArgumentException(("notificationEventName cannot be null in config."));
31+
}
32+
33+
return nativeInitialize(config.loggerInstance.getNativeILoggerPtr(), config.notificationEventName);
34+
}
35+
36+
/**
37+
* Checks if the sanitizer is initialized.
38+
*
39+
* @return true if initialized, false otherwise.
40+
*/
41+
public static native boolean isInitialized();
42+
43+
/**
44+
* Initializes the sanitizer with the given logger pointer and optional notification event name.
45+
*
46+
* @param loggerNativePtr Native pointer to ILogger.
47+
* @param notificationEventName Optional event name for sanitizer notifications.
48+
* @return true if initialization was successful, false otherwise.
49+
*/
50+
public static native boolean nativeInitialize(long loggerNativePtr, String notificationEventName);
51+
52+
/**
53+
* Uninitializes the sanitizer.
54+
*
55+
* @return true if uninitialization was successful, false otherwise.
56+
*/
57+
public static native boolean uninitialize();
58+
59+
/**
60+
* Checks if the sanitizer is currently enabled.
61+
*
62+
* @return true if enabled, false otherwise.
63+
*/
64+
public static native boolean isEnabled();
65+
66+
/**
67+
* Enables or disables the sanitizer.
68+
*
69+
* @param enabled true to enable, false to disable.
70+
* @return true if the operation was successful, false otherwise.
71+
*/
72+
public static native boolean setEnabled(boolean enabled);
73+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
package com.microsoft.applications.events;
6+
7+
/**
8+
* Represents the configuration settings used to initialize a sanitizer instance.
9+
*/
10+
11+
public class SanitizerConfiguration {
12+
13+
/**
14+
* The logger instance used to record privacy concern events.
15+
* This field is required.
16+
*/
17+
public final ILogger loggerInstance;
18+
19+
/**
20+
     * The custom event name used when logging sanitizer concerns.
21+
     * Optional. Defaults to "SanitizerConcerns" if not specified.
22+
     */
23+
public String notificationEventName = "SanitizerConcerns";
24+
25+
/**
26+
     * Constructs a new SanitizerConfiguration with the specified logger.
27+
     *
28+
     * @param logger The ILogger implementation used to log privacy concern events.
29+
     * @throws IllegalArgumentException if the logger is null.
30+
     */
31+
public SanitizerConfiguration(ILogger logger) {
32+
33+
if(logger == null) {
34+
throw new IllegalArgumentException("logger cannot be null");
35+
}
36+
37+
this.loggerInstance = logger;
38+
}
39+
}

lib/jni/SanitizerHelper.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
6+
#include "ctmacros.hpp"
7+
#include "modules/sanitizer/Sanitizer.hpp"
8+
9+
namespace MAT_NS_BEGIN
10+
{
11+
struct Sanitizer {
12+
/**
13+
* Get the current instance of Sanitizer.
14+
* @return SanitizerPtr if it is initialized, nullptr otherwise.
15+
*/
16+
static std::shared_ptr<Sanitizer> GetSanitizerPtr() noexcept;
17+
};
18+
} MAT_NS_END

lib/jni/Sanitizer_jni.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
6+
#include "ctmacros.hpp"
7+
#include "JniConvertors.hpp"
8+
#include "modules/sanitizer/Sanitizer.hpp"
9+
#include "SanitizerHelper.hpp"
10+
11+
using namespace MAT;
12+
13+
std::shared_ptr<Sanitizer> spSanitizer;
14+
15+
std::shared_ptr<Sanitizer> SanitizerHelper::GetSanitizerPtr() noexcept
16+
{
17+
return spSanitizer;
18+
}
19+
20+
extern "C"
21+
JNIEXPORT jboolean JNICALL
22+
Java_com_microsoft_applications_events_Sanitizer_isInitialized(const JNIEnv *env, jclass/* this */) {
23+
24+
return spSanitizer != nullptr;
25+
}
26+
27+
extern "C"
28+
JNIEXPORT jboolean JNICALL
29+
Java_com_microsoft_applications_events_Sanitizer_nativeInitialize(
30+
JNIEnv *env, jclass /* this */,
31+
jlong iLoggerNativePtr,
32+
jstring notificationEventName) {
33+
34+
if (spSanitizer != nullptr) {
35+
return false;
36+
}
37+
38+
SanitizerConfiguration sanitizerConfig(reinterpret_cast<ILogger*>(iLoggerNativePtr));
39+
40+
if (notificationEventName != nullptr) {
41+
config.NotificationEventName = JStringToStdString(env, notificationEventName).c_str();
42+
}
43+
44+
spSanitizer = std::make_shared<Sanitizer>(config);
45+
return true;
46+
}
47+
48+
49+
extern "C"
50+
JNIEXPORT jboolean JNICALL
51+
Java_com_microsoft_applications_events_Sanitizer_uninitialize(const JNIEnv *env, jclass /*this*/) {
52+
53+
if(spSanitizer == nullptr) {
54+
return false;
55+
}
56+
57+
spSanitizer.reset();
58+
59+
return true;
60+
}
61+
62+
extern "C"
63+
JNIEXPORT jboolean JNICALL
64+
Java_com_microsoft_applications_events_Sanitizer_isEnabled(JNIEnv *env, jclass clazz) {
65+
66+
if (spSanitizer == nullptr) {
67+
return false;
68+
}
69+
70+
return spSanitizer.get()->IsEnabled();
71+
}
72+
73+
extern "C"
74+
JNIEXPORT jboolean JNICALL
75+
Java_com_microsoft_applications_events_Sanitizer_setEnabled(JNIEnv *env, jclass clazz,
76+
jboolean enabled) {
77+
if (spDataInspector == nullptr) {
78+
return false;
79+
}
80+
81+
spDataInspector->SetEnabled(static_cast<bool>(enabled));
82+
return true;
83+
}

lib/modules

0 commit comments

Comments
 (0)