1 package com.jsql.model.injection.strategy;
2
3 import com.jsql.model.InjectionModel;
4 import com.jsql.model.accessible.DataAccess;
5 import com.jsql.view.subscriber.Seal;
6 import com.jsql.model.injection.engine.model.EngineYaml;
7 import com.jsql.model.injection.engine.model.yaml.Method;
8 import com.jsql.model.suspendable.AbstractSuspendable;
9 import com.jsql.util.I18nUtil;
10 import com.jsql.util.LogLevelUtil;
11 import com.jsql.util.StringUtil;
12 import org.apache.logging.log4j.LogManager;
13 import org.apache.logging.log4j.Logger;
14
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
17
18 public class StrategyError extends AbstractStrategy {
19
20 private static final Logger LOGGER = LogManager.getRootLogger();
21
22 private String[] tabCapacityMethod;
23
24 private int indexErrorStrategy = 0;
25
26 public StrategyError(InjectionModel injectionModel) {
27 super(injectionModel);
28 }
29
30 @Override
31 public void checkApplicability() {
32
33 this.isApplicable = false;
34
35 var strategyYaml = this.injectionModel.getMediatorEngine().getEngine().instance().getModelYaml().getStrategy();
36
37 if (this.injectionModel.getMediatorUtils().preferencesUtil().isStrategyErrorDisabled()) {
38 LOGGER.log(LogLevelUtil.CONSOLE_INFORM, AbstractStrategy.FORMAT_SKIP_STRATEGY_DISABLED, this.getName());
39 return;
40 } else if (strategyYaml.getError().getMethod().isEmpty()) {
41 LOGGER.log(
42 LogLevelUtil.CONSOLE_INFORM,
43 AbstractStrategy.FORMAT_STRATEGY_NOT_IMPLEMENTED,
44 this.getName(),
45 this.injectionModel.getMediatorEngine().getEngine()
46 );
47 return;
48 }
49
50 this.logChecking();
51
52 this.tabCapacityMethod = new String[strategyYaml.getError().getMethod().size()];
53 var indexErrorMethod = 0;
54 var errorCapacity = 0;
55
56 for (Method errorMethod: strategyYaml.getError().getMethod()) {
57 boolean methodIsApplicable = this.isApplicable(errorMethod);
58 if (methodIsApplicable) {
59 Matcher regexSearch = this.getPerformance(errorMethod);
60 if (regexSearch.find()) {
61 errorCapacity = this.getCapacity(indexErrorMethod, errorCapacity, errorMethod, regexSearch);
62 } else {
63 LOGGER.log(
64 LogLevelUtil.CONSOLE_ERROR,
65 "{} {} but injectable size is incorrect",
66 () -> I18nUtil.valueByKey(AbstractStrategy.KEY_LOG_VULNERABLE),
67 errorMethod::getName
68 );
69 methodIsApplicable = false;
70 }
71 }
72
73 if (methodIsApplicable) {
74 this.allow(indexErrorMethod);
75 } else {
76 this.unallow(indexErrorMethod);
77 }
78 indexErrorMethod++;
79 }
80 }
81
82 private boolean isApplicable(Method errorMethod) {
83 var methodIsApplicable = false;
84
85 String performanceSourcePage = this.injectionModel.injectWithoutIndex(
86 this.injectionModel.getMediatorEngine().getEngine().instance().sqlErrorIndice(errorMethod),
87 "error#confirm"
88 );
89
90 var indexZeroToFind = "0";
91 String regexIndexZero = String.format(EngineYaml.FORMAT_INDEX, indexZeroToFind);
92 if (performanceSourcePage.matches("(?s).*"+ regexIndexZero +".*")) {
93 methodIsApplicable = true;
94 this.isApplicable = true;
95 }
96 return methodIsApplicable;
97 }
98
99 private Matcher getPerformance(Method errorMethod) {
100 String performanceErrorSourcePage = this.injectionModel.injectWithoutIndex(
101 this.injectionModel.getMediatorEngine().getEngine().instance().sqlErrorCalibrator(errorMethod),
102 "error#size"
103 );
104 return Pattern.compile("(?s)"+ DataAccess.LEAD +"("+ EngineYaml.CALIBRATOR_SQL +"+)").matcher(performanceErrorSourcePage);
105 }
106
107 private int getCapacity(int indexErrorMethod, int errorCapacityDefault, Method errorMethod, Matcher regexSearch) {
108 int errorCapacityImproved = errorCapacityDefault;
109
110 regexSearch.reset();
111 while (regexSearch.find()) {
112 if (errorCapacityImproved < regexSearch.group(1).length()) {
113 this.indexErrorStrategy = indexErrorMethod;
114 }
115 errorCapacityImproved = regexSearch.group(1).length();
116 this.tabCapacityMethod[indexErrorMethod] = Integer.toString(errorCapacityImproved);
117 }
118
119 int logErrorCapacityImproved = errorCapacityImproved;
120 LOGGER.log(
121 LogLevelUtil.CONSOLE_SUCCESS,
122 "{} [Error {}] showing [{}] characters",
123 () -> I18nUtil.valueByKey(AbstractStrategy.KEY_LOG_VULNERABLE),
124 errorMethod::getName,
125 () -> Integer.toString(logErrorCapacityImproved)
126 );
127
128 return errorCapacityImproved;
129 }
130
131 @Override
132 public void allow(int... indexError) {
133 this.injectionModel.appendAnalysisReport(
134 StringUtil.formatReport(
135 LogLevelUtil.COLOR_BLU,
136 "### Strategy: "+ this.getName() +":"+ this.injectionModel.getMediatorEngine().getEngine().instance()
137 .getModelYaml()
138 .getStrategy()
139 .getError()
140 .getMethod()
141 .get(indexError[0])
142 .getName()
143 )
144 + this.injectionModel.getReportWithoutIndex(
145 this.injectionModel.getMediatorEngine().getEngine().instance().sqlError(
146 StringUtil.formatReport(LogLevelUtil.COLOR_GREEN, "<query>"),
147 "0",
148 indexError[0],
149 true
150 ),
151 "metadataInjectionProcess"
152 )
153 );
154 this.injectionModel.sendToViews(new Seal.MarkStrategyVulnerable(indexError[0], this));
155 }
156
157 @Override
158 public void unallow(int... indexError) {
159 this.injectionModel.sendToViews(new Seal.MarkStrategyInvulnerable(indexError[0], this));
160 }
161
162 @Override
163 public String inject(String sqlQuery, String startPosition, AbstractSuspendable stoppable, String metadataInjectionProcess) {
164 return this.injectionModel.injectWithoutIndex(
165 this.injectionModel.getMediatorEngine().getEngine().instance().sqlError(sqlQuery, startPosition, this.indexErrorStrategy, false),
166 metadataInjectionProcess
167 );
168 }
169
170 @Override
171 public void activateWhenApplicable() {
172 if (this.injectionModel.getMediatorStrategy().getStrategy() == null && this.isApplicable()) {
173 LOGGER.log(
174 LogLevelUtil.CONSOLE_INFORM,
175 "{} [{} {}]",
176 () -> I18nUtil.valueByKey("LOG_USING_STRATEGY"),
177 this::getName,
178 () -> this.injectionModel.getMediatorEngine().getEngine().instance().getModelYaml().getStrategy()
179 .getError().getMethod().get(this.indexErrorStrategy).getName()
180 );
181 this.injectionModel.getMediatorStrategy().setStrategy(this);
182 this.injectionModel.sendToViews(new Seal.ActivateStrategy(this));
183 }
184 }
185
186 @Override
187 public String getPerformanceLength() {
188 return this.tabCapacityMethod[this.indexErrorStrategy];
189 }
190
191 @Override
192 public String getName() {
193 return "Error";
194 }
195
196 public Integer getIndexErrorStrategy() {
197 return this.indexErrorStrategy;
198 }
199
200 public void setIndexErrorStrategy(int indexErrorStrategy) {
201 this.indexErrorStrategy = indexErrorStrategy;
202 }
203 }