-
Notifications
You must be signed in to change notification settings - Fork 2.7k
fix: sanitize non-UTF-8 label values in filesystem collector #3675
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
toller892
wants to merge
1
commit into
prometheus:master
Choose a base branch
from
toller892:fix/filesystem-non-utf8-mountpoint
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,13 +18,133 @@ | |
| import ( | ||
| "io" | ||
| "log/slog" | ||
| "strings" | ||
| "testing" | ||
| "unicode/utf8" | ||
|
|
||
| "github.com/alecthomas/kingpin/v2" | ||
|
|
||
| "github.com/prometheus/procfs" | ||
| ) | ||
|
|
||
| func Test_parseFilesystemLabelsNonUTF8(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| in []*procfs.MountInfo | ||
| wantMountPoint string | ||
| wantDevice string | ||
| }{ | ||
| { | ||
| name: "non-utf8 mount point is sanitized", | ||
| in: []*procfs.MountInfo{ | ||
| { | ||
| MajorMinorVer: "8:1", | ||
| MountPoint: "/mnt/cass\xe9", | ||
| Source: "/dev/sda1", | ||
| FSType: "ext4", | ||
| Options: map[string]string{}, | ||
| SuperOptions: map[string]string{}, | ||
| }, | ||
| }, | ||
| wantMountPoint: "/mnt/cass\ufffd", | ||
| wantDevice: "/dev/sda1", | ||
| }, | ||
| { | ||
| name: "non-utf8 device is sanitized", | ||
| in: []*procfs.MountInfo{ | ||
| { | ||
| MajorMinorVer: "8:1", | ||
| MountPoint: "/mnt/data", | ||
| Source: "/dev/sd\xe9", | ||
| FSType: "ext4", | ||
| Options: map[string]string{}, | ||
| SuperOptions: map[string]string{}, | ||
| }, | ||
| }, | ||
| wantMountPoint: "/mnt/data", | ||
| wantDevice: "/dev/sd\ufffd", | ||
| }, | ||
| { | ||
| name: "valid utf8 is preserved", | ||
| in: []*procfs.MountInfo{ | ||
| { | ||
| MajorMinorVer: "8:1", | ||
| MountPoint: "/mnt/caf\u00e9", | ||
| Source: "/dev/sda1", | ||
| FSType: "ext4", | ||
| Options: map[string]string{}, | ||
| SuperOptions: map[string]string{}, | ||
| }, | ||
| }, | ||
| wantMountPoint: "/mnt/caf\u00e9", | ||
| wantDevice: "/dev/sda1", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| filesystems, err := parseFilesystemLabels(tt.in) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if len(filesystems) != 1 { | ||
| t.Fatalf("expected 1 filesystem, got %d", len(filesystems)) | ||
| } | ||
| fs := filesystems[0] | ||
| if fs.mountPoint != tt.wantMountPoint { | ||
| t.Errorf("mountPoint = %q, want %q", fs.mountPoint, tt.wantMountPoint) | ||
| } | ||
| if fs.device != tt.wantDevice { | ||
| t.Errorf("device = %q, want %q", fs.device, tt.wantDevice) | ||
| } | ||
| // Verify all label values are valid UTF-8. | ||
| for _, v := range []string{fs.device, fs.mountPoint, fs.fsType} { | ||
| if !utf8.ValidString(v) { | ||
| t.Errorf("label value %q is not valid UTF-8", v) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func Test_parseFilesystemLabelsNonUTF8DoesNotPanic(t *testing.T) { | ||
| // Verify that non-UTF-8 data does not cause a panic when | ||
| // constructing Prometheus metrics (the root cause of #3662). | ||
| input := []*procfs.MountInfo{ | ||
| { | ||
| MajorMinorVer: "8:1", | ||
| MountPoint: "/mnt/bad\xffpath", | ||
| Source: "/dev/sd\x00a1", | ||
| FSType: "ext\xfe4", | ||
| Options: map[string]string{}, | ||
| SuperOptions: map[string]string{}, | ||
| }, | ||
| } | ||
|
|
||
| filesystems, err := parseFilesystemLabels(input) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if len(filesystems) != 1 { | ||
| t.Fatalf("expected 1 filesystem, got %d", len(filesystems)) | ||
| } | ||
|
|
||
| // All label values must be valid UTF-8. | ||
| for _, v := range []string{ | ||
| filesystems[0].device, | ||
| filesystems[0].mountPoint, | ||
| filesystems[0].fsType, | ||
| } { | ||
| if !utf8.ValidString(v) { | ||
| t.Errorf("label value %q is not valid UTF-8", v) | ||
| } | ||
| // Must not contain any raw invalid bytes (replacement char is OK). | ||
| if strings.ContainsRune(v, 0xFFFD) { | ||
| t.Logf("label value contains replacement character (expected for invalid input): %q", v) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func Test_parseFilesystemLabelsError(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
|
|
@@ -122,8 +242,8 @@ | |
| "/run/rpc_pipefs": "", | ||
| "/run/user/1000": "", | ||
| "/run/user/1000/gvfs": "", | ||
| "/var/lib/kubelet/plugins/kubernetes.io/vsphere-volume/mounts/[vsanDatastore] bafb9e5a-8856-7e6c-699c-801844e77a4a/kubernetes-dynamic-pvc-3eba5bba-48a3-11e8-89ab-005056b92113.vmdk": "", | ||
| "/var/lib/kubelet/plugins/kubernetes.io/vsphere-volume/mounts/[vsanDatastore] bafb9e5a-8856-7e6c-699c-801844e77a4a/kubernetes-dynamic-pvc-3eba5bba-48a3-11e8-89ab-005056b92113.vmdk": "", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this required by the Non-UTF8 bug fix? |
||
| "/var/lib/kubelet/plugins/kubernetes.io/vsphere-volume/mounts/[vsanDatastore]\tbafb9e5a-8856-7e6c-699c-801844e77a4a/kubernetes-dynamic-pvc-3eba5bba-48a3-11e8-89ab-005056b92113.vmdk": "", | ||
| "/var/lib/containers/storage/overlay": "", | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test largely overlaps the table test, both guard the same revert. Its only unique coverage is an invalid fsType; consider folding that into the table test and dropping this one. If you'd rather keep a named regression test for #3662, make it exercise MustNewConstMetric so it actually reproduces the panic, right now neither test hits that path.