-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest.cpp
More file actions
60 lines (51 loc) · 1.4 KB
/
test.cpp
File metadata and controls
60 lines (51 loc) · 1.4 KB
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
// Forward declarations - minimal to avoid system header dependencies
extern "C" {
int system(const char *);
int sprintf(char *, const char *, ...);
int strncmp(const char *, const char *, unsigned long);
}
// Mock QString class
struct QString {
const char *data;
QString(const char *s) : data(s) {}
bool operator==(const char *other) const;
};
// Mock Qt-like class for QDesktopServices::openUrl
struct QUrl {
const char *url;
QUrl(const char *s) : url(s) {}
QString scheme() const;
bool startsWith(const char *prefix) const;
};
struct QDesktopServices {
static bool openUrl(const QUrl &url);
};
// Untrusted input sources
extern "C" char *getUserInput();
extern "C" const char *getUrlParam();
// BAD: QDesktopServices::openUrl with untrusted input
void bad1_qt(const char *userUrl) {
QUrl url(userUrl);
QDesktopServices::openUrl(url); // BAD
}
void bad2_qt() {
const char *input = getUrlParam();
QUrl url(input);
QDesktopServices::openUrl(url); // BAD
}
void safe1_qt() {
QUrl url("https://example.com");
QDesktopServices::openUrl(url); // GOOD - no taint
}
void safe2_qt(const char *userUrl) {
if (strncmp(userUrl, "https://", 8) == 0 ||
strncmp(userUrl, "http://", 7) == 0) {
QUrl url(userUrl);
QDesktopServices::openUrl(url); // GOOD
}
}
void safe3_qt(QUrl &url) {
if (url.scheme() == "https" || url.scheme() == "http") {
QDesktopServices::openUrl(url); // GOOD
}
}