Skip to content
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

Error when building to a folder not owned by the Hugo process #7302

Open
klakegg opened this issue May 21, 2020 · 15 comments
Open

Error when building to a folder not owned by the Hugo process #7302

klakegg opened this issue May 21, 2020 · 15 comments

Comments

@klakegg
Copy link
Contributor

klakegg commented May 21, 2020

What version of Hugo are you using (hugo version)?

$ hugo version
Hugo Static Site Generator v0.71.0-06150C87/extended linux/amd64 BuildDate: 2020-05-18T16:15:29Z

Does this issue reproduce with the latest release?

Yes it does.

The error occures when the Hugo process have write access to the destination folder but does not own it.

To reproduce, create a public folder owned by root in your Hugo site project (requires sudo access):

sudo sh -c "mkdir public && chmod a+w public"

Run Hugo:

hugo

Log:

Building sites … WARN 2020/05/21 14:09:25 found no layout file for "HTML" for kind "section": You should create a template file which matches Hugo Layouts Lookup Rules for this combination.
Total in 111 ms
Error: Error copying static files: chtimes [..]/public/: operation not permitted
@davidsneighbour
Copy link
Contributor

Can you please check the file rights on the folder? ls -al foldername should do the trick on a linux system. Nobody should be able to use a folder with only root rights, so that is not a bug. The issue is that you want to give write access to group and all. Worst case something around 0777. chmod 0777 public will solve the issue. The question is if that is safe enough for your environment and setup. If you need to run the creation of the folder with root then something is wrong with your users and setup, not Hugo. Hugo is doing the right thing.

@klakegg
Copy link
Contributor Author

klakegg commented May 21, 2020

@davidsneighbour Thank you for having a look at my issue report.

I'm aware of the safety aspects of this, I simply constructed an example that should make it easy to reproduce what I reported.

Let me make it step by step, basically as described in my initial post.

Listing before manually creating public folder:

hugo:/workspaces/site-klakegg/src$ ls -la
total 32
drwxr-xr-x 7 hugo hugo 4096 May 21 15:13 .
drwxr-xr-x 7 hugo hugo 4096 May 21 06:34 ..
drwxr-xr-x 3 hugo hugo 4096 May  1 20:20 assets
-rw-r--r-- 1 hugo hugo  282 May  1 20:09 config.yml
drwxr-xr-x 3 hugo hugo 4096 May  1 20:18 content
drwxr-xr-x 4 hugo hugo 4096 May  1 20:18 layouts
drwxr-xr-x 3 hugo hugo 4096 May  1 19:19 resources
drwxr-xr-x 2 hugo hugo 4096 May  1 20:17 static

Creating the public folder, and setting rights accoring to your suggestion:

hugo:$ sudo mkdir public
hugo:$ sudo chmod 0777 public

Here is the listing with the public folder created:

hugo:$ ls -la        
total 36
drwxr-xr-x 8 hugo hugo 4096 May 21 14:27 .
drwxr-xr-x 7 hugo hugo 4096 May 21 06:34 ..
drwxr-xr-x 3 hugo hugo 4096 May  1 20:20 assets
-rw-r--r-- 1 hugo hugo  282 May  1 20:09 config.yml
drwxr-xr-x 3 hugo hugo 4096 May  1 20:18 content
drwxr-xr-x 4 hugo hugo 4096 May  1 20:18 layouts
drwxrwxrwx 4 root root 4096 May 21 14:27 public
drwxr-xr-x 3 hugo hugo 4096 May  1 19:19 resources
drwxr-xr-x 2 hugo hugo 4096 May  1 20:17 static

Triggering build:

hugo:$ hugo
Building sites … WARN 2020/05/21 15:10:36 found no layout file for "HTML" for kind "section": You should create a template file which matches Hugo Layouts Lookup Rules for this combination.
Total in 103 ms
Error: Error copying static files: chtimes [..]/public/: operation not permitted

Clearly chmod 0777 public did not solve the issue.

It is not clear to me why it is important to trigger chtimes on the folder containing the produced content.

@davidsneighbour
Copy link
Contributor

davidsneighbour commented May 21, 2020

That is weird. With those file rights I would expect everyone being able to change things in the folder. I googled around and found, that chtimes is a GO function, not a shell thing - assumed the first. It might still be file rights related, see the following issue unrelated:

rclone/rclone#738 (comment)

  1. Why do you need to create the folder public as user root, not user hugo? Can you try creating it as user hugo (should somehow work via sudo -u hugo or the like)?
  2. Is the file location somehow mounted and are the mount rights either set restrictive or without rights as the issue above discussed?

What's the operating system you are using here?

@klakegg
Copy link
Contributor Author

klakegg commented May 21, 2020

  1. I've create an example using the root user as I guess that user is accessible to most non-Windows users following this repo.
  2. The folder is not mounted, however everything runs inside a Docker container without any mounting magic.

I've verified it using both Debian and Ubuntu.

@davidsneighbour
Copy link
Contributor

davidsneighbour commented May 21, 2020

I can confirm that it's not docker related. I tried on my own repo locally with a root user created public folder (process got frozen) that was then set to 0777 (process ended up with a chtimes error).

So it's probably an issue in how chtimes is called in Go or Hugo.

sudo hugo (the script kiddie solution) by the way ends up in Hugo not finding the configuration.

This all does not solve the mystery why you would create a folder owned by root instead of creating a folder owned by a more public user (the one running hugo, or a completely public one).

Some ideas that probably won't work:

  • add root to the usergroup hugo
  • add hugo to the usergroup root/sudo (unsafe)
  • and still, why is root creating a public directory?

This is where my knowledge about the system ends and @bep or another Go-God might have a look at it.

@klakegg
Copy link
Contributor Author

klakegg commented May 21, 2020

It is not related to the root user. Try making the folder this way (1337 or other uid not used):

$ mkdir public
$ chmod 0777 public
$ sudo chown 1337 public

Please notice the group ownership is not changed in the above commands.

This will result in the same error when triggering Hugo. The root ownership is simply not relevant, the fact that the folder is owned by any other entity than id -u is the important part.

@bep bep added this to the v0.72 milestone May 22, 2020
@bep
Copy link
Member

bep commented May 22, 2020

Error: Error copying static files: chtimes [..]/public/: operation not permitted

Does it work if you do

hugo --noChmod

Or maybe

hugo --noChmod --noTimes

@klakegg
Copy link
Contributor Author

klakegg commented May 22, 2020

Using --noTimes makes it work, both with and without --noChmod.

@bep
Copy link
Member

bep commented May 22, 2020

Using --noTimes

OK, so this is what's giving a permission error https://golang.org/pkg/os/#Chtimes

@bep bep modified the milestones: v0.72, v0.73 May 31, 2020
@bep bep modified the milestones: v0.73, v0.74 Jun 23, 2020
@bep bep modified the milestones: v0.74, v0.75 Jul 13, 2020
@bep bep modified the milestones: v0.75, v0.76 Sep 14, 2020
@bep bep modified the milestones: v0.76, v0.77 Oct 6, 2020
@bep bep modified the milestones: v0.77, v0.78 Oct 30, 2020
@bep bep modified the milestones: v0.78, v0.83 Apr 23, 2021
@bep bep modified the milestones: v0.83, v0.84 May 3, 2021
@bep bep modified the milestones: v0.84, v0.85 Jun 18, 2021
@bep bep added this to the v0.119.0 milestone Sep 15, 2023
@bep bep modified the milestones: v0.119.0, v0.120.0 Oct 4, 2023
@bep bep modified the milestones: v0.120.0, v0.121.0 Oct 31, 2023
@bep bep modified the milestones: v0.121.0, v0.122.0 Dec 6, 2023
@smaudet
Copy link

smaudet commented Jan 15, 2024

I also ran into this, I want a user to be able to run this without owning the files in question (group access).

I guess chtimes is a user-level permission?

I made a uhugo shortcut that aliases to hugo --noTimes - maybe this shouldn't be a default option (or detect if you have the permissions and warn instead of fail).

@jmooring
Copy link
Member

@smaudet You can set noTimes = true in the site configuration instead of the command line flag. That might be a bit easier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants