|
| 1 | +<?php |
| 2 | +/* |
| 3 | + +-------------------------------------------------------------------------+ |
| 4 | + | Copyright (C) 2004-2026 The Cacti Group | |
| 5 | + | | |
| 6 | + | This program is free software; you can redistribute it and/or | |
| 7 | + | modify it under the terms of the GNU General Public License | |
| 8 | + | as published by the Free Software Foundation; either version 2 | |
| 9 | + | of the License, or (at your option) any later version. | |
| 10 | + +-------------------------------------------------------------------------+ |
| 11 | + | Cacti: The Complete RRDtool-based Graphing Solution | |
| 12 | + +-------------------------------------------------------------------------+ |
| 13 | + */ |
| 14 | + |
| 15 | +/* |
| 16 | + * Regression checks for JavaScript-context output hardening. |
| 17 | + * |
| 18 | + * These tests ensure request/session derived values are encoded or normalized |
| 19 | + * before being embedded in JavaScript. |
| 20 | + */ |
| 21 | + |
| 22 | +$authProfilePath = __DIR__ . '/../../auth_profile.php'; |
| 23 | +$authResetpasswordPath = __DIR__ . '/../../auth_resetpassword.php'; |
| 24 | +$authChangepasswordPath = __DIR__ . '/../../auth_changepassword.php'; |
| 25 | +$pluginsPath = __DIR__ . '/../../plugins.php'; |
| 26 | +$htmlGraphPath = __DIR__ . '/../../lib/html_graph.php'; |
| 27 | +$dataDebugPath = __DIR__ . '/../../data_debug.php'; |
| 28 | + |
| 29 | +test('auth_profile encodes tab for JavaScript and redirect URL', function () use ($authProfilePath) { |
| 30 | + $contents = file_get_contents($authProfilePath); |
| 31 | + |
| 32 | + expect($contents)->toContain("json_encode((string) grv('tab'))"); |
| 33 | + expect($contents)->toContain("gfrv('tab', FILTER_VALIDATE_REGEXP"); |
| 34 | + expect($contents)->toContain('rawurlencode($currentTab)'); |
| 35 | +}); |
| 36 | + |
| 37 | +test('plugins page normalizes state and encodes sort column in JavaScript', function () use ($pluginsPath) { |
| 38 | + $contents = file_get_contents($pluginsPath); |
| 39 | + |
| 40 | + expect($contents)->toContain("json_encode((string) grv('sort_column'))"); |
| 41 | + expect($contents)->toContain("var tableState = <?php print (int) grv('state'); ?>;"); |
| 42 | + expect($contents)->not->toContain("var tableState = <?php print grv('state'); ?>"); |
| 43 | +}); |
| 44 | + |
| 45 | +test('graph list view uses JSON and sanitized CSV for graph list', function () use ($htmlGraphPath) { |
| 46 | + $contents = file_get_contents($htmlGraphPath); |
| 47 | + |
| 48 | + expect($contents)->toContain('$graph_list_js = []'); |
| 49 | + expect($contents)->toContain('ctype_digit($item)'); |
| 50 | + expect($contents)->toContain('json_encode($graph_list_js)'); |
| 51 | + expect($contents)->toContain("graph_list=<?php print \$graph_list_csv; ?>"); |
| 52 | + expect($contents)->not->toContain("new Array(<?php print grv('graph_list'); ?>)"); |
| 53 | +}); |
| 54 | + |
| 55 | +test('graph pages encode JS-bound PHP strings safely', function () use ($htmlGraphPath) { |
| 56 | + $contents = file_get_contents($htmlGraphPath); |
| 57 | + |
| 58 | + expect($contents)->toContain('var pageAction = <?php print json_encode($action); ?>'); |
| 59 | + expect($contents)->toContain('var graphPage = <?php print json_encode($page); ?>'); |
| 60 | + expect($contents)->toContain("json_encode((string) \$suffix)"); |
| 61 | +}); |
| 62 | + |
| 63 | +test('data debug escapes tooltip title values before rendering', function () use ($dataDebugPath) { |
| 64 | + $contents = file_get_contents($dataDebugPath); |
| 65 | + |
| 66 | + expect($contents)->toContain('$value_title = htmle((string) $value);'); |
| 67 | +}); |
| 68 | + |
| 69 | +test('auth reset password encodes return location in onclick handlers', function () use ($authResetpasswordPath) { |
| 70 | + $contents = file_get_contents($authResetpasswordPath); |
| 71 | + |
| 72 | + expect($contents)->toContain("document.location=<?php print json_encode((string) \$return); ?>"); |
| 73 | + expect($contents)->not->toContain("document.location=\"<?php print \$return; ?>\""); |
| 74 | +}); |
| 75 | + |
| 76 | +test('auth change password encodes return location in onclick handler', function () use ($authChangepasswordPath) { |
| 77 | + $contents = file_get_contents($authChangepasswordPath); |
| 78 | + |
| 79 | + expect($contents)->toContain("document.location=<?php print json_encode((string) \$return); ?>"); |
| 80 | + expect($contents)->not->toContain("onClick='document.location=\\\"\$return\\\"'"); |
| 81 | +}); |
0 commit comments