Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions cmd/internal/pages/assets/js/containers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@

google.charts.load('current', {packages: ['corechart', 'gauge', 'default', 'format', 'ui', 'table']});

// Escape HTML special characters to prevent XSS when rendering in tables with allowHtml.
function escapeHtml(str) {
if (typeof str !== 'string') return str;
return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#039;');
}

function humanize(num, size, units) {
var unit;
for (unit = units.pop(); units.length && num >= size; unit = units.pop()) {
Expand Down Expand Up @@ -622,10 +628,10 @@ function drawProcesses(isRoot, rootDir, processInfo) {
var data = [];
for (var i = 0; i < processInfo.length; i++) {
var elements = [];
elements.push(processInfo[i].user);
elements.push(escapeHtml(processInfo[i].user));
elements.push(processInfo[i].pid);
elements.push(processInfo[i].parent_pid);
elements.push(processInfo[i].start_time);
elements.push(escapeHtml(processInfo[i].start_time));
elements.push({
v: processInfo[i].percent_cpu,
f: processInfo[i].percent_cpu.toFixed(2)
Expand All @@ -639,15 +645,16 @@ function drawProcesses(isRoot, rootDir, processInfo) {
v: processInfo[i].virtual_size,
f: humanizeIEC(processInfo[i].virtual_size)
});
elements.push(processInfo[i].status);
elements.push(processInfo[i].running_time);
elements.push(processInfo[i].cmd);
elements.push(escapeHtml(processInfo[i].status));
elements.push(escapeHtml(processInfo[i].running_time));
elements.push(escapeHtml(processInfo[i].cmd));
elements.push(processInfo[i].psr);
if (isRoot) {
var cgroup = processInfo[i].cgroup_path;
// Use the raw cgroup link as it works for all containers.
var cgroupLink = '<a href="' + rootDir + 'containers/' + cgroup + '">' +
cgroup.substr(0, 30) + ' </a>';
var escapedCgroup = escapeHtml(cgroup);
var cgroupLink = '<a href="' + encodeURI(rootDir + 'containers/' + cgroup) + '">' +
escapedCgroup.substr(0, 30) + ' </a>';
elements.push({v: cgroup, f: cgroupLink});
}
data.push(elements);
Expand Down
4 changes: 4 additions & 0 deletions manager/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ func (cd *containerData) ReadFile(filepath string, inHostNamespace bool) ([]byte
}
for _, pid := range pids {
filePath := path.Join(rootfs, "/proc", pid, "/root", filepath)
expectedPrefix := path.Join(rootfs, "/proc", pid, "/root") + "/"
if !strings.HasPrefix(filePath, expectedPrefix) {
return nil, fmt.Errorf("invalid file path %q: resolves outside container root", filepath)
}
klog.V(3).Infof("Trying path %q", filePath)
data, err := os.ReadFile(filePath)
if err == nil {
Expand Down