cl
8 小时以前 c4bba32b20f0869b45ed14be04543869dd91ee6c
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package com.vincent.rsf.httpaudit.service;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vincent.rsf.httpaudit.entity.HttpAuditRule;
import com.vincent.rsf.httpaudit.mapper.HttpAuditRuleMapper;
import com.vincent.rsf.httpaudit.model.HttpAuditDecision;
import com.vincent.rsf.httpaudit.props.HttpAuditProperties;
import com.vincent.rsf.httpaudit.support.HttpAuditSupport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
 
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.net.URI;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.regex.Pattern;
 
/**
 * 规则缓存;入站/出站或关系;record_all 时白名单下也全记
 */
@Slf4j
public class HttpAuditRuleServiceImpl extends ServiceImpl<HttpAuditRuleMapper, HttpAuditRule> implements HttpAuditRuleService {
 
    private final HttpAuditProperties props;
    private final CopyOnWriteArrayList<HttpAuditRule> cache = new CopyOnWriteArrayList<>();
 
    public HttpAuditRuleServiceImpl(HttpAuditRuleMapper mapper, HttpAuditProperties props) {
        this.baseMapper = mapper;
        this.props = props;
    }
 
    @PostConstruct
    public void init() {
        refreshCache();
    }
 
    @Override
    @Scheduled(fixedDelayString = "${http-audit.rule-cache-refresh-ms:60000}")
    public void refreshCache() {
        try {
            List<HttpAuditRule> list = list(new LambdaQueryWrapper<HttpAuditRule>()
                    .eq(HttpAuditRule::getDeleted, 0)
                    .eq(HttpAuditRule::getEnabled, 1)
                    .orderByAsc(HttpAuditRule::getSortOrder)
                    .orderByAsc(HttpAuditRule::getId));
            cache.clear();
            cache.addAll(list);
            log.debug("http-audit 规则缓存已刷新,条数={}", cache.size());
        } catch (Exception e) {
            log.warn("http-audit 规则缓存刷新失败(白名单将不生效,入站不落库)", e);
        }
    }
 
    @Override
    public boolean shouldAudit(HttpServletRequest request, String requestBody) {
        return decideInbound(request, requestBody).isAudit();
    }
 
    @Override
    public HttpAuditDecision decideInbound(HttpServletRequest request, String requestBody) {
        if (!props.isWhitelistOnly()) {
            return HttpAuditDecision.yes(reqLimitFromRecordAllRow(), resLimitFromRecordAllRow());
        }
        if (cache.isEmpty()) {
            return HttpAuditDecision.SKIP;
        }
        HttpAuditRule allRow = firstRecordAllRule();
        if (allRow != null) {
            return HttpAuditDecision.yes(allRow.getRequestMaxChars(), allRow.getResponseMaxChars());
        }
        String path = HttpAuditSupport.safePath(request);
        String ip = HttpAuditSupport.clientIp(request);
        String body = requestBody == null ? "" : requestBody;
        for (HttpAuditRule r : cache) {
            if (isRecordAll(r)) {
                continue;
            }
            if (!appliesInbound(r)) {
                continue;
            }
            try {
                if (matchInbound(r, path, ip, body)) {
                    return HttpAuditDecision.yes(r.getRequestMaxChars(), r.getResponseMaxChars());
                }
            } catch (Exception e) {
                log.debug("http-audit 规则 id={} 匹配异常:{}", r.getId(), e.getMessage());
            }
        }
        return HttpAuditDecision.SKIP;
    }
 
    @Override
    public HttpAuditDecision decideOutbound(String fullUrl, String method, String requestBody) {
        if (!props.isWhitelistOnly()) {
            return HttpAuditDecision.yes(reqLimitFromRecordAllRow(), resLimitFromRecordAllRow());
        }
        if (cache.isEmpty()) {
            return HttpAuditDecision.SKIP;
        }
        HttpAuditRule allRow = firstRecordAllRule();
        if (allRow != null) {
            return HttpAuditDecision.yes(allRow.getRequestMaxChars(), allRow.getResponseMaxChars());
        }
        String body = requestBody == null ? "" : requestBody;
        for (HttpAuditRule r : cache) {
            if (isRecordAll(r)) {
                continue;
            }
            if (!appliesOutbound(r)) {
                continue;
            }
            try {
                if (matchOutbound(r, fullUrl, body)) {
                    return HttpAuditDecision.yes(r.getRequestMaxChars(), r.getResponseMaxChars());
                }
            } catch (Exception e) {
                log.debug("http-audit 出站规则 id={} 匹配异常:{}", r.getId(), e.getMessage());
            }
        }
        return HttpAuditDecision.SKIP;
    }
 
    private Integer reqLimitFromRecordAllRow() {
        HttpAuditRule row = firstRecordAllRule();
        return row == null ? null : row.getRequestMaxChars();
    }
 
    private Integer resLimitFromRecordAllRow() {
        HttpAuditRule row = firstRecordAllRule();
        return row == null ? null : row.getResponseMaxChars();
    }
 
    private HttpAuditRule firstRecordAllRule() {
        for (HttpAuditRule r : cache) {
            if (isRecordAll(r)) {
                return r;
            }
        }
        return null;
    }
 
    private static boolean isRecordAll(HttpAuditRule r) {
        return r.getRecordAll() != null && r.getRecordAll() == 1;
    }
 
    private static String dir(HttpAuditRule r) {
        String d = r.getDirection();
        if (d == null || d.isEmpty()) {
            return HttpAuditRule.DIR_IN;
        }
        return d;
    }
 
    private static boolean appliesInbound(HttpAuditRule r) {
        if (isRecordAll(r)) {
            return false;
        }
        String d = dir(r);
        return HttpAuditRule.DIR_IN.equals(d) || HttpAuditRule.DIR_BOTH.equals(d);
    }
 
    private static boolean appliesOutbound(HttpAuditRule r) {
        if (isRecordAll(r)) {
            return false;
        }
        String d = dir(r);
        return HttpAuditRule.DIR_OUT.equals(d) || HttpAuditRule.DIR_BOTH.equals(d);
    }
 
    private static boolean matchInbound(HttpAuditRule r, String path, String ip, String body) {
        return matchByRuleType(r, path, ip, body);
    }
 
    private boolean matchOutbound(HttpAuditRule r, String fullUrl, String body) {
        String t = r.getRuleType();
        String mode = r.getMatchMode();
        String p = r.getPattern();
        if (p == null) {
            return false;
        }
        if (HttpAuditRule.TYPE_URI.equals(t)) {
            if (matchString(fullUrl, mode, p)) {
                return true;
            }
            return matchString(extractPath(fullUrl), mode, p);
        }
        if (HttpAuditRule.TYPE_IP.equals(t)) {
            String host = extractHost(fullUrl);
            return matchString(host, mode, p);
        }
        if (HttpAuditRule.TYPE_REQUEST_BODY.equals(t)) {
            return matchString(body, mode, p);
        }
        return false;
    }
 
    private static String extractPath(String url) {
        try {
            URI u = URI.create(url);
            String path = u.getPath();
            return path == null ? "" : path;
        } catch (Exception e) {
            return url == null ? "" : url;
        }
    }
 
    private static String extractHost(String url) {
        try {
            URI u = URI.create(url);
            String h = u.getHost();
            return h == null ? "" : h;
        } catch (Exception e) {
            return "";
        }
    }
 
    private static boolean matchByRuleType(HttpAuditRule r, String path, String ip, String body) {
        String t = r.getRuleType();
        String mode = r.getMatchMode();
        String p = r.getPattern();
        if (p == null) {
            return false;
        }
        if (HttpAuditRule.TYPE_URI.equals(t)) {
            return matchString(path, mode, p);
        }
        if (HttpAuditRule.TYPE_IP.equals(t)) {
            return matchString(ip, mode, p);
        }
        if (HttpAuditRule.TYPE_REQUEST_BODY.equals(t)) {
            return matchString(body, mode, p);
        }
        return false;
    }
 
    private static boolean matchString(String value, String mode, String pattern) {
        if (value == null) {
            value = "";
        }
        if (HttpAuditRule.MODE_EQUAL.equals(mode)) {
            return value.equals(pattern);
        }
        if (HttpAuditRule.MODE_PREFIX.equals(mode)) {
            return value.startsWith(pattern);
        }
        if (HttpAuditRule.MODE_CONTAINS.equals(mode)) {
            return value.contains(pattern);
        }
        if (HttpAuditRule.MODE_REGEX.equals(mode)) {
            return Pattern.compile(pattern).matcher(value).find();
        }
        return false;
    }
}