mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-04 03:27:23 -05:00 
			
		
		
		
	fixing panic when root is symlink (#1429)
* fixing panic when root is symlink checking root path is a symlink before os.Stat which panics * fixing formatting * adding test to verify symlink root path check * fixing typo
This commit is contained in:
		
							parent
							
								
									dc3efc939c
								
							
						
					
					
						commit
						55bded68c2
					
				@ -28,7 +28,13 @@ func setupRoot(c *caddy.Controller) error {
 | 
				
			|||||||
			return c.ArgErr()
 | 
								return c.ArgErr()
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						//first check that the path is not a symlink, os.Stat panics when this is true
 | 
				
			||||||
 | 
						info, _ := os.Lstat(config.Root)
 | 
				
			||||||
 | 
						if info != nil && info.Mode()&os.ModeSymlink == os.ModeSymlink {
 | 
				
			||||||
 | 
							//just print out info, delegate responsibility for symlink validity to
 | 
				
			||||||
 | 
							//underlying Go framework, no need to test / verify twice
 | 
				
			||||||
 | 
							log.Printf("[INFO] Root path is symlink: %s", config.Root)
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
		// Check if root path exists
 | 
							// Check if root path exists
 | 
				
			||||||
		_, err := os.Stat(config.Root)
 | 
							_, err := os.Stat(config.Root)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
@ -40,6 +46,7 @@ func setupRoot(c *caddy.Controller) error {
 | 
				
			|||||||
				return c.Errf("Unable to access root path '%s': %v", config.Root, err)
 | 
									return c.Errf("Unable to access root path '%s': %v", config.Root, err)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -91,7 +91,7 @@ func TestRoot(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// getTempDirPath returnes the path to the system temp directory. If it does not exists - an error is returned.
 | 
					// getTempDirPath returns the path to the system temp directory. If it does not exists - an error is returned.
 | 
				
			||||||
func getTempDirPath() (string, error) {
 | 
					func getTempDirPath() (string, error) {
 | 
				
			||||||
	tempDir := os.TempDir()
 | 
						tempDir := os.TempDir()
 | 
				
			||||||
	_, err := os.Stat(tempDir)
 | 
						_, err := os.Stat(tempDir)
 | 
				
			||||||
@ -104,3 +104,39 @@ func getTempDirPath() (string, error) {
 | 
				
			|||||||
func getInaccessiblePath(file string) string {
 | 
					func getInaccessiblePath(file string) string {
 | 
				
			||||||
	return filepath.Join("C:", "file\x00name") // null byte in filename is not allowed on Windows AND unix
 | 
						return filepath.Join("C:", "file\x00name") // null byte in filename is not allowed on Windows AND unix
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestSymlinkRoot(t *testing.T) {
 | 
				
			||||||
 | 
						origDir, err := ioutil.TempDir("", "root_test")
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatalf("BeforeTest: Failed to create temp dir for testing! Error was: %v", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer func() {
 | 
				
			||||||
 | 
							os.Remove(origDir)
 | 
				
			||||||
 | 
						}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						tempDir, err := getTempDirPath()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatalf("BeforeTest: Failed to find an existing directory for testing! Error was: %v", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						symlinkDir := filepath.Join(tempDir, "symlink")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						err = os.Symlink(origDir, symlinkDir)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							if strings.Contains(err.Error(), "A required privilege is not held by the client") {
 | 
				
			||||||
 | 
								t.Skip("BeforeTest:  A required privilege is not held by the client and is required to create a symlink to run this test.")
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							t.Fatalf("BeforeTest: Cannot create symlink! Error was: %v", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer func() {
 | 
				
			||||||
 | 
							os.Remove(symlinkDir)
 | 
				
			||||||
 | 
						}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						input := fmt.Sprintf(`root %s`, symlinkDir)
 | 
				
			||||||
 | 
						c := caddy.NewTestController("http", input)
 | 
				
			||||||
 | 
						err = setupRoot(c)
 | 
				
			||||||
 | 
						_ = httpserver.GetConfig(c)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Test Symlink Root: Expected no error but found one for input %s. Error was: %v", input, err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user