Skip to content

x/tools/godoc/vfs/mapfs: easy to accidentally misuse API, causing severe problems #34591

Closed

Description

@dmitshur

mapfs.New documentation says:

[...] Map keys should be forward slash-separated pathnames and not contain a leading slash.

However, there is no validation on the input. If the user accidentally provides map keys with leading slashes, it results in incorrect programs and hard-to-diagnose problems.

For example:

package main

import (
	"fmt"

	"golang.org/x/tools/godoc/vfs/mapfs"
)

func main() {
	fs := mapfs.New(map[string]string{
		// Incorrect mapfs.New usage, map key with a leading slash.
		"/doc/a.txt": "a",
	})
	for _, path := range []string{
		// Both of these are valid paths.
		"/doc/a.txt",
		"doc/a.txt",
	} {
		_, err := fs.Open(path)
		fmt.Printf("fs.Open(%q): %v\n", path, err)
		_, err = fs.Stat(path)
		fmt.Printf("fs.Stat(%q): %v\n", path, err)
		_, err = fs.Lstat(path)
		fmt.Printf("fs.Lstat(%q): %v\n", path, err)
	}
}

It produces output:

fs.Open("/doc/a.txt"): file does not exist
fs.Stat("/doc/a.txt"): file does not exist
fs.Lstat("/doc/a.txt"): file does not exist
fs.Open("doc/a.txt"): file does not exist
fs.Stat("doc/a.txt"): file does not exist
fs.Lstat("doc/a.txt"): file does not exist

When mapfs.New is used correctly, like so:

fs := mapfs.New(map[string]string{
	// Correct mapfs.New usage, map key has no leading slash.
	"doc/a.txt": "a",
})

Then the output is as expected:

fs.Open("/doc/a.txt"): <nil>
fs.Stat("/doc/a.txt"): <nil>
fs.Lstat("/doc/a.txt"): <nil>
fs.Open("doc/a.txt"): <nil>
fs.Stat("doc/a.txt"): <nil>
fs.Lstat("doc/a.txt"): <nil>

I think it would be net helpful to perform validation in New and help users learn as quickly as possible when they're misusing the API by panicking with a descriptive error message. Panics aren't good, but incorrect results can be even worse. Most mapfs.New usage happens at program initialization time and in tests.

/cc @jayconrod

Activity

  1. added
    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.
    ToolsThis label describes issues relating to any tools in the x/tools repository.
    on Sep 28, 2019
  2. added this to the Unreleased milestone on Sep 28, 2019
  3. gopherbot commented on Sep 28, 2019

    @gopherbot
    Contributor

    Change https://golang.org/cl/197859 mentions this issue: godoc/vfs/mapfs: panic on invalid New usage

  4. dmitshur commented on Sep 30, 2019

    @dmitshur
    MemberAuthor

    I think this can be fixed in the following ways:

    1. detect invalid API usage and panic with a helpful message

    2. detect invalid API usage and log a helpful message to stderr

    3. change API to allow leading slashes, and start to support them

    I've sent a CL that implements option 1 to see if it'd catch any invalid usages in x/tools, and there weren't any.

    The downside of option 3 is that it makes API harder to use: when leading slash is optional, it's no longer clear whether one should include it or not. It will lead to more inconsistent styles. It makes mapfs more complicated, and I'd prefer not to do this.

    That leaves 1 or 2. Given there are no invalid usages in x/tools (nor in x/website, I checked), I'd prefer 1. I believe a hard-to-miss panic will save people time and cause fewer problems in the long term. mapfs is primarily an internal godoc support package, so we should have more freedom to change it compared to more general-purpose packages.

  5. self-assigned this
    on May 8, 2020
  6. dmitshur commented on May 8, 2020

    @dmitshur
    MemberAuthor

    I've thought about this quite a bit, and I still think option 1 has the best trade-offs based on what we know now. If this newly added panic happens somewhere, I currently believe learning about that will bring greater positive value than the disruption it would cause. If we learn of new evidence suggesting otherwise, we can revert and consider another approach.

  7. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    and removed
    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.
    on May 8, 2020
  8. locked and limited conversation to collaborators on May 8, 2021
  9. added a commit that references this issue on Jun 13, 2024
    7072b74
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.ToolsThis label describes issues relating to any tools in the x/tools repository.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions