-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPotentiallyUnguardedProtocolHandler.cpp
More file actions
55 lines (48 loc) · 1.34 KB
/
PotentiallyUnguardedProtocolHandler.cpp
File metadata and controls
55 lines (48 loc) · 1.34 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
// Forward declarations - minimal stubs
extern "C" {
int read(int fd, void *buf, unsigned long count);
int system(const char *);
int sprintf(char *, const char *, ...);
int strncmp(const char *, const char *, unsigned long);
}
struct QString {
const char *data;
QString(const char *s) : data(s) {}
bool operator==(const char *other) const;
bool startsWith(const char *prefix) const;
};
struct QUrl {
const char *url;
QUrl(const char *s) : url(s) {}
QString scheme() const;
};
struct QDesktopServices {
static bool openUrl(const QUrl &url);
};
// BAD: read() into buffer, then pass to openUrl without scheme check
void bad1_qt_read() {
char buf[1024];
read(0, buf, sizeof(buf));
QDesktopServices::openUrl(QUrl(buf)); // BAD
}
// BAD: read() into buffer, intermediate usage, then openUrl without guard
void bad2_qt_read_indirect() {
char buf[1024];
char url[2048];
read(0, buf, sizeof(buf));
sprintf(url, "file://%s", buf);
// no scheme check before opening
QDesktopServices::openUrl(QUrl(url)); // BAD
}
// GOOD: hardcoded URL
void safe1_qt_hardcoded() {
QDesktopServices::openUrl(QUrl("https://example.com")); // GOOD
}
// GOOD: scheme check before openUrl
void safe2_qt_scheme_check() {
char buf[1024];
read(0, buf, sizeof(buf));
if (strncmp(buf, "https://", 8) == 0) {
QDesktopServices::openUrl(QUrl(buf)); // GOOD
}
}