/
securityController.go
182 lines (159 loc) · 6.01 KB
/
securityController.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//Copyright 2019 Onegini B.V.
//
//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 util
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strconv"
"strings"
)
func ReadCordovaSecurityPreference(preferences []cordovaPreference, preferenceName string, defaultValue bool) bool {
for _, pref := range preferences {
if pref.Name == preferenceName {
var err error
var value bool
value, err = strconv.ParseBool(pref.Value)
if err != nil {
_, _ = os.Stderr.WriteString(fmt.Sprintf("ERROR: could not parse '%s' preference: %v\n", preferenceName, err.Error()))
os.Exit(1)
} else {
fmt.Printf("WARNING: config.xml contains %s=%t, this value will be used in the SecurityController\n", preferenceName, value)
return value
}
}
}
return defaultValue
}
func ReadNativeScriptSecurityPreferences(config *Config) (rootDetection bool, debugDetection bool, debugLogs bool) {
rootDetectionSet := false
debugDetectionSet := false
if config.NativeScript.OneginiPreferences.DebugDetectionEnabled != nil {
debugDetectionSet = true
debugDetection = *config.NativeScript.OneginiPreferences.DebugDetectionEnabled
}
if config.NativeScript.OneginiPreferences.RootDetectionEnabled != nil {
rootDetectionSet = true
rootDetection = *config.NativeScript.OneginiPreferences.RootDetectionEnabled
}
if config.NativeScript.OneginiPreferences.DebugLogsEnabled {
debugLogs = config.NativeScript.OneginiPreferences.DebugLogsEnabled
}
if !rootDetectionSet {
rootDetection = true
}
if !debugDetectionSet {
debugDetection = true
}
return
}
func WriteAndroidSecurityController(config *Config, debugDetection bool, rootDetection bool, debugLogs bool, tamperingProtection bool) {
packageId := getPackageIdentifierFromConfig(config)
storePath := config.getAndroidSecurityControllerPath()
_ = os.Remove(storePath) // always remove old file
if rootDetection && debugDetection && !debugLogs && tamperingProtection {
return
}
fileContents := `package %s;
@SuppressWarnings({ "unused", "WeakerAccess" })
public final class SecurityController {
%s}`
flagsContents := prepareFlagsForAndroid(debugDetection, rootDetection, debugLogs, tamperingProtection)
fileContents = fmt.Sprintf(fileContents, packageId, flagsContents)
if err := ioutil.WriteFile(storePath, []byte(fileContents), os.ModePerm); err != nil {
log.Fatal("WARNING! Could not update security controller. This might be dangerous!")
}
}
func WriteIOSSecurityController(config *Config, debugDetection bool, rootDetection bool, debugLogs bool, tamperingProtection bool) {
group := "Configuration"
xcodeProjPath := config.getIosXcodeProjPath()
configModelPath := config.getIosConfigModelPath()
headerStorePath := path.Join(configModelPath, "SecurityController.h")
modelStorePath := path.Join(configModelPath, "SecurityController.m")
// always remove old files
removeFileFromXcodeProj(headerStorePath, xcodeProjPath, group, config.FlavorName)
removeFileFromXcodeProj(modelStorePath, xcodeProjPath, group, config.FlavorName)
_ = os.Remove(headerStorePath)
_ = os.Remove(modelStorePath)
if rootDetection && debugDetection && !debugLogs && tamperingProtection {
return
}
headerContents := `#import <Foundation/Foundation.h>
@interface SecurityController : NSObject
%s@end
`
headerContents = fmt.Sprintf(headerContents, prepareHeaderFlagsForIos(debugDetection, rootDetection, debugLogs, tamperingProtection))
modelContents := `#import "SecurityController.h"
@implementation SecurityController
%s@end
`
modelContents = fmt.Sprintf(modelContents, prepareModelFlagsForIos(debugDetection, rootDetection, debugLogs, tamperingProtection))
_ = ioutil.WriteFile(headerStorePath, []byte(headerContents), os.ModePerm)
_ = ioutil.WriteFile(modelStorePath, []byte(modelContents), os.ModePerm)
addFileToXcodeProj(headerStorePath, xcodeProjPath, config.AppTarget, group, config.FlavorName)
addFileToXcodeProj(modelStorePath, xcodeProjPath, config.AppTarget, group, config.FlavorName)
}
func prepareFlagsForAndroid(debugDetection bool, rootDetection bool, debugLogs bool, tamperingProtection bool) string {
// don't print unnecessary (default) flags
var sb strings.Builder
if !rootDetection {
sb.WriteString(" public static final boolean rootDetection = false;\n")
}
if !debugDetection {
sb.WriteString(" public static final boolean debugDetection = false;\n")
}
if debugLogs {
sb.WriteString(" public static final boolean debugLogs = true;\n")
}
if !tamperingProtection {
sb.WriteString(" public static final boolean tamperingProtection = false;\n")
}
return sb.String()
}
func prepareHeaderFlagsForIos(debugDetection bool, rootDetection bool, debugLogs bool, tamperingProtection bool) string {
// don't print unnecessary (default) flags
var sb strings.Builder
if !rootDetection {
sb.WriteString("+ (bool)rootDetection;\n")
}
if !debugDetection {
sb.WriteString("+ (bool)debugDetection;\n")
}
if debugLogs {
sb.WriteString("+ (bool)debugLogs;\n")
}
if !tamperingProtection {
sb.WriteString("+ (bool)tamperingProtection;\n")
}
return sb.String()
}
func prepareModelFlagsForIos(debugDetection bool, rootDetection bool, debugLogs bool, tamperingProtection bool) string {
// don't print unnecessary (default) flags
var sb strings.Builder
if !rootDetection {
sb.WriteString("+(bool)rootDetection{\n return NO;\n}\n")
}
if !debugDetection {
sb.WriteString("+(bool)debugDetection{\n return NO;\n}\n")
}
if debugLogs {
sb.WriteString("+(bool)debugLogs{\n return YES;\n}\n")
}
if !tamperingProtection {
sb.WriteString("+(bool)tamperingProtection{\n return NO;\n}\n")
}
return sb.String()
}