Skip to content

Commit 3b451e6

Browse files
committed
clients/upsstats.c, docs/man/upsstats.html.txt, conf/upsstats*.html.sample.in, NEWS.adoc, UPGRADING.adoc: introduced @NUT_UPSSTATS_TEMPLATE@ magic token that HTML templates must start with [networkupstools#3249]
Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
1 parent f2c33c4 commit 3b451e6

File tree

6 files changed

+51
-1
lines changed

6 files changed

+51
-1
lines changed

NEWS.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ several `FSD` notifications into one executed action. [PR #3097]
265265
these JSON documents when browsing. [issue #2524, PRs #3171, #3249]
266266
* Handle `device.model` in addition to `ups.model` in upsstats HTML
267267
templates. [#3180]
268+
* Introduced a `@NUT_UPSSTATS_TEMPLATE@` command which the HTML template
269+
files now MUST start with (safety check that we are reading a template).
270+
[PR #3249]
268271

269272
- `upssched` tool updates:
270273
* Previously in PR #2896 (NUT releases v2.8.3 and v2.8.4) the `UPSNAME` and

UPGRADING.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ Changes from 2.8.4 to 2.8.5
5050
installed (and possibly customized) with the `*.html.sample` files delivered
5151
by the new build. [PR #3180]
5252
53+
- Introduced a `@NUT_UPSSTATS_TEMPLATE@` command which the NUT CGI template
54+
files now MUST start with (safety check that we are reading a template).
55+
While the delivered `upsstats*.html.sample` files would include the change,
56+
ultimate `upsstats*.html` templates deployed for end-users MUST be updated.
57+
[PR #3249]
58+
5359
- Dropped the `compile` script from Git sources. It originates from automake
5460
and is added to work area (if missing) during `autogen.sh` rituals anyway
5561
(as `make` says, `'automake --add-missing' can install 'compile'` when you

clients/upsstats.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,13 @@ static int do_command(char *cmd)
10361036
return 1;
10371037
}
10381038

1039-
upsdebug_call_finished1(": unknown cmd");
1039+
if (!strncmp(cmd, "NUT_UPSSTATS_TEMPLATE ", 22) || !strcmp(cmd, "NUT_UPSSTATS_TEMPLATE")) {
1040+
upsdebugx(2, "%s: saw magic token, ignoring", __func__);
1041+
upsdebug_call_finished0();
1042+
return 1;
1043+
}
1044+
1045+
upsdebug_call_finished2(": unknown cmd: '%s'", cmd);
10401046
return 0;
10411047
}
10421048

@@ -1100,6 +1106,27 @@ static void display_template(const char *tfn)
11001106
exit(EXIT_FAILURE);
11011107
}
11021108

1109+
if (!fgets(buf, sizeof(buf), tf)) {
1110+
fprintf(stderr, "upsstats: template file %s seems to be empty (fgets failed): %s\n", fn, strerror(errno));
1111+
1112+
printf("Error: template file %s seems to be empty\n", tfn);
1113+
1114+
upsdebug_call_finished1(": empty template");
1115+
exit(EXIT_FAILURE);
1116+
}
1117+
1118+
/* Test first line for a bit of expected magic */
1119+
if (!strncmp(buf, "@NUT_UPSSTATS_TEMPLATE", 22)) {
1120+
parse_line(buf);
1121+
} else {
1122+
fprintf(stderr, "upsstats: template file %s does not start with NUT_UPSSTATS_TEMPLATE command\n", fn);
1123+
1124+
printf("Error: template file %s does not start with NUT_UPSSTATS_TEMPLATE command\n", tfn);
1125+
1126+
upsdebug_call_finished1(": not a valid template");
1127+
exit(EXIT_FAILURE);
1128+
}
1129+
11031130
while (fgets(buf, sizeof(buf), tf)) {
11041131
parse_line(buf);
11051132
}

conf/upsstats-single.html.sample.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@NUT_UPSSTATS_TEMPLATE single@
12
<!-- upsstats template file -->
23

34
<!--

conf/upsstats.html.sample.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@NUT_UPSSTATS_TEMPLATE default@
12
<!-- upsstats template file -->
23

34
<!--

docs/man/upsstats.html.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ use-cases, are used by linkman:upsstats.cgi[8] to generate NUT CGI status
1414
pages. Certain commands are recognized, and will be replaced with various
1515
status elements on the fly.
1616

17+
As a bit of a safety check, the file must start with `@NUT_UPSSTATS_TEMPLATE@`
18+
command.
19+
1720
For details about configuring some popular web servers to run NUT CGI
1821
programs, please see the linkman:upsset.conf[5] page.
1922

@@ -105,6 +108,15 @@ example (more examples are available in upsstats.html).
105108
OTHER COMMANDS
106109
--------------
107110

111+
*@NUT_UPSSTATS_TEMPLATE [args]@*::
112+
The template file MUST start exactly with `@NUT_UPSSTATS_TEMPLATE@` or
113+
`@NUT_UPSSTATS_TEMPLATE Some text@` so that linkman:upsstats.cgi[8] is
114+
not coerced into showing arbitrary files.
115+
+
116+
Currently the command and its arguments are otherwise ignored; templates
117+
shipped with NUT use the argument values "default" and "single" effectively
118+
as a comment for debugging/troubleshooting.
119+
108120
*@UPSSTATSPATH filename@*::
109121
URI to the linkman:upsstats.cgi[8] program placed into HTML references.
110122
Default is the program's name, making it a link relative to the currently

0 commit comments

Comments
 (0)