Opened 3 months ago

Closed 8 weeks ago

Last modified 8 weeks ago

#28170 closed Bug (fixed)

File upload to a CIFS mount fails with Permissions Denied

Reported by: Derrick Jackson Owned by: Peter J. Farrell
Component: File uploads/storage Version: 1.11
Severity: Release blocker Keywords:
Cc: pjf@…, derrick.jackson73@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

I upgraded from DJango 1.10.6 to 1.11 and can no longer use FileField to upload files larger than 2.5 MB. When an attempt is made to upload a larger file I get the error below occurs. Please note if I downgrade to 1.10.6 again the upload feature works just fine.

Django Version:	1.11
Exception Type:	PermissionError
Exception Value:	
[Errno 1] Operation not permitted
Exception Location:	/usr/lib/python3.5/shutil.py in copystat, line 195
Python Executable:	/usr/bin/python3
Python Version:	3.5.2

Environment:


Request Method: POST
Request URL: http://localhost:8000/orders/service/vpn/5/

Django Version: 1.11
Python Version: 3.5.2
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'impersonate',
 'stronghold',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'ajax_select',
 'django_object_actions',
 'ckeditor',
 'mail_templated',
 'administrative',
 'changes',
 'decommission',
 'ecm',
 'feedback',
 'file_manager',
 'gunicorn',
 'orders',
 'old_net_chg_req',
 'server_request',
 'security',
 'shared',
 'system',
 'utilities',
 'downtime',
 'debug_toolbar']
Installed Middleware:
('django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'shared.middleware.CustomDowntimeMiddleware',
 'stronghold.middleware.LoginRequiredMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'shared.middleware.GlobalRequestMiddleware',
 'shared.middleware.UserTracebackMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'impersonate.middleware.ImpersonateMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware')



Traceback:

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _legacy_get_response
  249.             response = self._get_response(request)

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/vagrant/orders/views_services.py" in service_vpn
  538.                     service_order_item.save()

File "/vagrant/orders/models.py" in save
  1150.         super(ServiceOrder, self).save(*args, **kwargs)

File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py" in save
  806.                        force_update=force_update, update_fields=update_fields)

File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py" in save_base
  836.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)

File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py" in _save_table
  900.                       for f in non_pks]

File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py" in <listcomp>
  900.                       for f in non_pks]

File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/files.py" in pre_save
  297.             file.save(file.name, file.file, save=False)

File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/files.py" in save
  95.         self.name = self.storage.save(name, content, max_length=self.field.max_length)

File "/usr/local/lib/python3.5/dist-packages/django/core/files/storage.py" in save
  54.         return self._save(name, content)

File "/usr/local/lib/python3.5/dist-packages/django/core/files/storage.py" in _save
  338.                     file_move_safe(content.temporary_file_path(), full_path)

File "/usr/local/lib/python3.5/dist-packages/django/core/files/move.py" in file_move_safe
  70.     copystat(old_file_name, new_file_name)

File "/usr/lib/python3.5/shutil.py" in copystat
  195.         follow_symlinks=follow)

Exception Type: PermissionError at /orders/service/vpn/5/
Exception Value: [Errno 1] Operation not permitted

Change History (15)

comment:1 Changed 3 months ago by Tim Graham

Component: Core (Other)File uploads/storage
Description: modified (diff)

I'm not sure if there's much we can do with the report without a way to reproduce the problem. If you can bisect to determine the commit where the behavior changed, that would be useful.

comment:2 Changed 3 months ago by Tim Graham

Resolution: needsinfo
Status: newclosed

comment:3 Changed 3 months ago by Peter J. Farrell

Cc: pjf@… added

comment:4 Changed 3 months ago by Derrick Jackson

Tim,

After chatting with a colleague of mine we were able to determine that this commit f734e2d is where the issue I reported started. Specifically changing
file.save(file.name, file, save=False) to file.save(file.name, file.save, save=False) in files.py causes the copystat error.

Please let me know if you need additional details regarding this issue.
Thanks,
Derrick

comment:5 Changed 3 months ago by Derrick Jackson

Cc: derrick.jackson73@… added
Resolution: needsinfo
Status: closednew

meant to re-open this ticket once I added my comment.

comment:6 Changed 3 months ago by Derrick Jackson

Additional Details: In my case, the files are larger than 2.5 MB. The tmp directory is on a local linux file system and the final location is a mounted CIFS drive. This Stack Overflow post covers my exact situation.

Last edited 3 months ago by Derrick Jackson (previous) (diff)

comment:7 Changed 3 months ago by Tim Graham

Severity: NormalRelease blocker
Summary: FileField Upload Permissions DeniedFile upload to a CIFS mount fails with Permissions Denied
Triage Stage: UnreviewedAccepted

Thanks for the additional details. Can you offer a patch that fixes the issue for you? I guess a regression test might involve some mocking to simulate a CIFS mount.

comment:8 Changed 3 months ago by Derrick Jackson

Tim... I will see what I can do. The tough part will be simulating a CIFS mount. I know the easy route would be to wrap line 296 of django/db/models/fields/files.py with a try/except but I know there's more to it.

try:
    file.save(file.name, file.file, save=False)
except Exception as e:
    file.save(file.name, file, save=False)

I'll keep digging into it.

comment:9 Changed 3 months ago by Derrick Jackson

Has patch: set

Tim,

A co-worker (pj@…) and I put together a suggested patch for the reported issue. We created the following pull request: https://github.com/django/django/pull/8486

comment:10 Changed 3 months ago by Tim Graham

Needs tests: set

Looks okay at first glance. The next step is to add some tests, probably by mocking shutils.copystat().

comment:11 Changed 2 months ago by Derrick Jackson

Needs tests: unset

The requested modifications and tests have been made. Here is the corresponding Pull Request: PR

comment:12 Changed 2 months ago by felixxm

Patch needs improvement: set

comment:13 Changed 8 weeks ago by Peter J. Farrell

Owner: changed from nobody to Peter J. Farrell
Patch needs improvement: unset
Status: newassigned
Triage Stage: AcceptedReady for checkin

comment:14 Changed 8 weeks ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 789c2901:

Fixed #28170 -- Fixed file_move_safe() crash when moving files to a CIFS mount.

comment:15 Changed 8 weeks ago by Tim Graham <timograham@…>

In 7250393:

[1.11.x] Fixed #28170 -- Fixed file_move_safe() crash when moving files to a CIFS mount.

Backport of 789c290150a0a5e7312e152df281dbcaf4ec174e from master

Note: See TracTickets for help on using tickets.
Back to Top