Coverage Report

Created: 2026-12-01 06:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/httrack/fuzz/fuzz-singlefile.c
Line
Count
Source
1
/* ------------------------------------------------------------ */
2
/*
3
HTTrack Website Copier, Offline Browser for Windows and Unix
4
Copyright (C) 2026 Xavier Roche and other contributors
5
6
SPDX-License-Identifier: GPL-3.0-or-later
7
8
This program is free software: you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation, either version 3 of the License, or
11
(at your option) any later version.
12
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
GNU General Public License for more details.
17
18
You should have received a copy of the GNU General Public License
19
along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21
Ethical use: we kindly ask that you NOT use this software to harvest email
22
addresses or to collect any other private information about people. Doing so
23
would dishonor our work and waste the many hours we have spent on it.
24
25
Please visit our Website: http://www.httrack.com
26
*/
27
28
/* Fuzz the --single-file rewriter (htssinglefile.c): hostile HTML walked
29
   through the tag, CSS url()/@import and srcset parsers, then re-serialized.
30
   The resolver is aimed at a private temp tree, so the inlining half (MIME
31
   guess, base64, nested stylesheet) is reached and nothing else on disk is. */
32
#include "fuzz.h"
33
34
#include "httrack-library.h"
35
#include "htssinglefile.h"
36
37
#include <stdio.h>
38
#include <stdlib.h>
39
#include <sys/stat.h>
40
41
/* Between a.png and big.png, so one input reaches both the inline path and the
42
   over-cap fallback. */
43
1
#define FUZZ_SF_CAP 64
44
45
static char sf_root[512];
46
static char sf_page[600];
47
48
/* The asset tree, in removal order: the subdirectory comes after its file. */
49
static const char *const sf_files[] = {"a.png",     "big.png", "j.js", "s.css",
50
                                       "sub/b.css", "sub",     NULL};
51
52
1
static void sf_cleanup(void) {
53
1
  char path[700];
54
1
  int i;
55
56
7
  for (i = 0; sf_files[i] != NULL; i++) {
57
6
    snprintf(path, sizeof(path), "%s/%s", sf_root, sf_files[i]);
58
6
    (void) remove(path);
59
6
  }
60
1
  (void) remove(sf_root);
61
1
}
62
63
/* One httrackp serves the run, because the corpus marks need its secret. */
64
static httrackp *sf_opt = NULL;
65
66
/* A missing asset would reduce the target to its parser half. */
67
5
static void sf_write(const char *name, hts_span bytes) {
68
5
  char path[700];
69
5
  FILE *fp;
70
71
5
  snprintf(path, sizeof(path), "%s/%s", sf_root, name);
72
5
  fp = fopen(path, "wb");
73
5
  if (fp == NULL || fwrite(bytes.p, 1, bytes.len, fp) != bytes.len)
74
0
    abort();
75
5
  fclose(fp);
76
5
}
77
78
/* Append ref and a valid mark, which a fixture cannot spell. */
79
4
static void sf_marked(String *out, const char *ref, char cls) {
80
4
  char mark[SINGLEFILE_MARK_MAX];
81
82
4
  StringCat(*out, ref);
83
4
  StringCat(*out, singlefile_mark(sf_opt, HTS_BUF(mark), cls, strlen(ref)));
84
4
}
85
86
/* \001<ref>\002 in an input becomes <ref> plus its mark, which is the only
87
   way a corpus file can reach the mark parser at all. */
88
1.62k
static void sf_expand_input(hts_span in, String *out) {
89
1.62k
  size_t i, start = 0;
90
91
1.62k
  StringClear(*out);
92
58.0M
  for (i = 0; i < in.len; i++) {
93
58.0M
    if (in.p[i] == '\001') {
94
757k
      start = StringLength(*out);
95
57.3M
    } else if (in.p[i] == '\002') {
96
30.8M
      char mark[SINGLEFILE_MARK_MAX];
97
98
30.8M
      StringCat(*out,
99
30.8M
                singlefile_mark(sf_opt, HTS_BUF(mark), SINGLEFILE_CLASS_ANY,
100
30.8M
                                StringLength(*out) - start));
101
30.8M
    } else {
102
26.5M
      StringAddchar(*out, in.p[i]);
103
26.5M
    }
104
58.0M
  }
105
1.62k
}
106
107
static void sf_css(const char *name, const char *pre, const char *ref,
108
1
                   const char *post) {
109
1
  String body = STRING_EMPTY;
110
111
1
  StringCopy(body, pre);
112
1
  sf_marked(&body, ref, SINGLEFILE_CLASS_ANY);
113
1
  StringCat(body, post);
114
1
  sf_write(name, hts_span_at(StringBuff(body), StringLength(body)));
115
1
  StringFree(body);
116
1
}
117
118
1
static void sf_init(void) {
119
1
  static const char png[] = "\x89PNG\r\n\x1a\n";
120
1
  static const char big[4096] = "\x89PNG";
121
1
  char path[700];
122
123
1
  hts_init();
124
1
  sf_opt = hts_create_opt();
125
1
  sf_opt->log = sf_opt->errlog = NULL;
126
1
  sf_opt->single_file_max_size = FUZZ_SF_CAP;
127
1
  if (!fuzz_tmpdir(HTS_BUF(sf_root), "httrack-fuzz-sf", sf_cleanup))
128
0
    abort();
129
1
  snprintf(sf_page, sizeof(sf_page), "%s/page.html", sf_root);
130
1
  snprintf(path, sizeof(path), "%s/sub", sf_root);
131
1
  if (mkdir(path, 0700) != 0)
132
0
    abort();
133
1
  sf_write("a.png", hts_span_at(png, sizeof(png) - 1));
134
1
  sf_write("big.png", hts_span_at(big, sizeof(big)));
135
1
  sf_write("j.js", hts_span_of("var x=1;\n"));
136
  /* Marks let an inlined stylesheet recurse and rebase its references. */
137
1
  {
138
1
    String css = STRING_EMPTY;
139
140
1
    StringCopy(css, "@import url(");
141
1
    sf_marked(&css, "sub/b.css", SINGLEFILE_CLASS_CSS);
142
1
    StringCat(css, ");\ndiv{background:url(");
143
1
    sf_marked(&css, "a.png", SINGLEFILE_CLASS_ANY);
144
1
    StringCat(css, ")}\np{background:url(");
145
1
    sf_marked(&css, "big.png", SINGLEFILE_CLASS_ANY);
146
1
    StringCat(css, ")}\n");
147
1
    sf_write("s.css", hts_span_at(StringBuff(css), StringLength(css)));
148
1
    StringFree(css);
149
1
  }
150
1
  sf_css("sub/b.css", "p{background:url(", "../a.png", ")}\n");
151
1
}
152
153
1.62k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
154
1.62k
  static int inited = 0;
155
156
1.62k
  String out = STRING_EMPTY;
157
1.62k
  String in = STRING_EMPTY;
158
1.62k
  char *html;
159
1.62k
  size_t html_len;
160
161
1.62k
  if (!inited) {
162
1
    sf_init();
163
1
    inited = 1;
164
1
  }
165
1.62k
  sf_expand_input(fuzz_span(data, size), &in);
166
1.62k
  html_len = StringLength(in);
167
  /* The copy is unterminated, so ASan catches a read past html_len. */
168
1.62k
  html = malloct(html_len != 0 ? html_len : 1);
169
1.62k
  memcpy(html, StringBuff(in), html_len);
170
1.62k
  StringFree(in);
171
172
1.62k
  StringClear(out);
173
1.62k
  (void) singlefile_rewrite_html(sf_opt, sf_root, sf_page,
174
1.62k
                                 hts_span_at(html, html_len),
175
1.62k
                                 SINGLEFILE_MAX_PAGE_SIZE, &out);
176
177
1.62k
  StringFree(out);
178
  freet(html);
179
1.62k
  return 0;
180
1.62k
}