File size: 10,470 Bytes
24463b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "privacy/security_policy.hpp"
#include "privacy/shield.hpp"

#include <algorithm>
#include <cctype>
#include <sstream>

namespace astra {

namespace {

std::string to_lower_str(std::string_view s) {
    std::string out(s);
    for (char& c : out) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
    return out;
}

std::string trim_ws(std::string_view s) {
    size_t a = 0;
    while (a < s.size() && std::isspace(static_cast<unsigned char>(s[a]))) ++a;
    size_t b = s.size();
    while (b > a && std::isspace(static_cast<unsigned char>(s[b - 1]))) --b;
    return std::string(s.substr(a, b - a));
}

uint16_t default_port_for(const std::string& scheme) {
    if (scheme == "http" || scheme == "ws") return 80;
    if (scheme == "https" || scheme == "wss") return 443;
    return 0;
}

} // namespace

SecurityOrigin SecurityOrigin::from_url(std::string_view url) {
    SecurityOrigin o;
    ParsedUrl p = parse_url(url);
    if (!p.valid || p.scheme == "data" || p.scheme == "javascript" || p.scheme == "about") {
        o.opaque = true;
        o.scheme = p.scheme;
        return o;
    }
    o.scheme = to_lower_str(p.scheme);
    o.host = to_lower_str(p.host);
    o.port = p.port ? p.port : default_port_for(o.scheme);
    if (o.scheme == "file" || o.scheme == "astra") {
        o.opaque = false;
        o.port = 0;
    }
    return o;
}

bool SecurityOrigin::same_origin(const SecurityOrigin& other) const {
    if (opaque || other.opaque) return false;
    return scheme == other.scheme && host == other.host && port == other.port;
}

bool SecurityOrigin::same_site(const SecurityOrigin& other) const {
    if (opaque || other.opaque) return false;
    if (scheme == "file" && other.scheme == "file") return true;
    if (scheme != other.scheme) return false;
    return site_key() == other.site_key();
}

bool SecurityOrigin::is_loopback() const {
    return host == "localhost" || host == "127.0.0.1" || host == "::1";
}

bool SecurityOrigin::is_potentially_trustworthy() const {
    if (opaque) return false;
    return scheme == "https" || scheme == "wss" || scheme == "file" || scheme == "astra" || is_loopback();
}

std::string SecurityOrigin::serialize() const {
    if (opaque) return "null";
    if (scheme == "file") return "file://";
    if (scheme == "astra") return "astra://" + host;
    std::string out = scheme + "://" + host;
    uint16_t def = default_port_for(scheme);
    if (port != 0 && port != def) out += ":" + std::to_string(port);
    return out;
}

std::string SecurityOrigin::site_key() const {
    if (opaque) return "opaque";
    if (scheme == "file" || scheme == "astra") return scheme + "://" + host;
    ParsedUrl tmp;
    tmp.scheme = scheme;
    tmp.host = host;
    tmp.port = port;
    tmp.valid = true;
    std::string reg = tmp.registrable_domain();
    return scheme + "://" + (reg.empty() ? host : reg);
}

void ContentSecurityPolicy::parse(std::string_view policy_header) {
    size_t pos = 0;
    while (pos < policy_header.size()) {
        size_t semi = policy_header.find(';', pos);
        std::string_view part = (semi == std::string_view::npos)
                                    ? policy_header.substr(pos)
                                    : policy_header.substr(pos, semi - pos);
        std::string trimmed = trim_ws(part);
        if (!trimmed.empty()) {
            std::istringstream iss(trimmed);
            std::string directive;
            if (iss >> directive) {
                directive = to_lower_str(directive);
                std::vector<std::string> tokens;
                std::string tok;
                while (iss >> tok) tokens.push_back(to_lower_str(tok));
                // First occurrence of a directive wins per W3C CSP spec
                if (directives_.find(directive) == directives_.end()) {
                    directives_[directive] = std::move(tokens);
                    active_ = true;
                }
            }
        }
        if (semi == std::string_view::npos) break;
        pos = semi + 1;
    }
}

const std::vector<std::string>* ContentSecurityPolicy::effective_sources(ResourceKind kind) const {
    const char* name = "default-src";
    switch (kind) {
        case ResourceKind::Script:   name = "script-src"; break;
        case ResourceKind::Style:    name = "style-src"; break;
        case ResourceKind::Image:    name = "img-src"; break;
        case ResourceKind::Connect:  name = "connect-src"; break;
        case ResourceKind::Frame:    name = "frame-src"; break;
        case ResourceKind::Media:    name = "media-src"; break;
        case ResourceKind::Font:     name = "font-src"; break;
        case ResourceKind::Document: name = "default-src"; break;
    }
    auto it = directives_.find(name);
    if (it != directives_.end()) return &it->second;
    auto def = directives_.find("default-src");
    if (def != directives_.end()) return &def->second;
    return nullptr;
}

bool ContentSecurityPolicy::allows_inline_script() const {
    if (!active_) return true;
    const auto* srcs = effective_sources(ResourceKind::Script);
    if (!srcs) return true;
    for (const auto& s : *srcs) {
        if (s == "'unsafe-inline'") return true;
    }
    return false;
}

bool ContentSecurityPolicy::allows_inline_style() const {
    if (!active_) return true;
    const auto* srcs = effective_sources(ResourceKind::Style);
    if (!srcs) return true;
    for (const auto& s : *srcs) {
        if (s == "'unsafe-inline'") return true;
    }
    return false;
}

bool ContentSecurityPolicy::source_matches(const std::string& expr,
                                           const SecurityOrigin& target,
                                           const SecurityOrigin& doc_origin,
                                           std::string_view raw_url) {
    if (expr == "'none'") return false;
    if (expr == "*") return !target.opaque && (target.scheme == "http" || target.scheme == "https" || target.scheme == "ws" || target.scheme == "wss");
    if (expr == "'self'") return target.same_origin(doc_origin);
    // Scheme source e.g. "https:", "data:"
    if (!expr.empty() && expr.back() == ':') {
        std::string want_scheme = expr.substr(0, expr.size() - 1);
        if (want_scheme == "data" && to_lower_str(raw_url.substr(0, 5)) == "data:") return true;
        return target.scheme == want_scheme;
    }
    // Host / origin source (with optional scheme and wildcard *.domain)
    std::string e = expr;
    std::string req_scheme;
    size_t sp = e.find("://");
    if (sp != std::string::npos) {
        req_scheme = e.substr(0, sp);
        e = e.substr(sp + 3);
    }
    size_t slash = e.find('/');
    if (slash != std::string::npos) e = e.substr(0, slash);
    uint16_t req_port = 0;
    size_t colon = e.find(':');
    if (colon != std::string::npos) {
        req_port = static_cast<uint16_t>(std::atoi(e.substr(colon + 1).c_str()));
        e = e.substr(0, colon);
    }
    if (!req_scheme.empty() && target.scheme != req_scheme) return false;
    if (req_port != 0 && target.port != req_port) return false;
    if (e.size() > 2 && e[0] == '*' && e[1] == '.') {
        std::string suffix = e.substr(1); // ".example.com"
        return target.host.size() > suffix.size() &&
               target.host.compare(target.host.size() - suffix.size(), suffix.size(), suffix) == 0;
    }
    return target.host == e;
}

bool ContentSecurityPolicy::allows_url(ResourceKind kind,
                                       std::string_view target_url,
                                       const SecurityOrigin& doc_origin,
                                       std::string* reason_out) const {
    if (!active_) return true;
    const auto* srcs = effective_sources(kind);
    if (!srcs) return true;
    SecurityOrigin target = SecurityOrigin::from_url(target_url);
    for (const auto& s : *srcs) {
        if (s == "'none'") {
            if (reason_out) *reason_out = "Blocked by CSP ('none')";
            return false;
        }
        if (source_matches(s, target, doc_origin, target_url)) return true;
    }
    if (reason_out) {
        *reason_out = "Blocked by Content-Security-Policy for " + std::string(target_url);
    }
    return false;
}

bool MixedContentBlocker::is_blocked(const SecurityOrigin& doc_origin,
                                     std::string_view target_url,
                                     ResourceKind kind,
                                     std::string* reason_out) {
    if (doc_origin.scheme != "https") return false;
    SecurityOrigin target = SecurityOrigin::from_url(target_url);
    if (target.is_loopback()) return false;
    bool insecure_net = (target.scheme == "http" || target.scheme == "ws");
    if (!insecure_net) return false;
    // Block all active mixed content (scripts, stylesheets, fetch/XHR/WS, iframes, fonts)
    bool is_active = (kind == ResourceKind::Script || kind == ResourceKind::Style ||
                      kind == ResourceKind::Connect || kind == ResourceKind::Frame ||
                      kind == ResourceKind::Font);
    if (is_active) {
        if (reason_out) {
            *reason_out = "Mixed Content blocked: insecure " + target.scheme + ":// resource from https:// document";
        }
        return true;
    }
    return false;
}

bool CorsEnforcer::allows_response(const SecurityOrigin& doc_origin,
                                   std::string_view target_url,
                                   const std::map<std::string, std::string>& resp_headers,
                                   std::string* reason_out) {
    // Local file:// and internal astra:// tools bypass network CORS unless explicitly remote
    if (doc_origin.scheme == "file" || doc_origin.scheme == "astra" || doc_origin.opaque) return true;
    SecurityOrigin target = SecurityOrigin::from_url(target_url);
    if (target.same_origin(doc_origin)) return true;

    std::string acao;
    for (const auto& [k, v] : resp_headers) {
        if (to_lower_str(k) == "access-control-allow-origin") {
            acao = trim_ws(v);
            break;
        }
    }
    if (acao == "*" || to_lower_str(acao) == to_lower_str(doc_origin.serialize())) {
        return true;
    }
    if (reason_out) {
        *reason_out = "CORS policy blocked cross-origin read from " + doc_origin.serialize() +
                      " to " + target.serialize() + " (missing Access-Control-Allow-Origin)";
    }
    return false;
}

} // namespace astra