---
logosub: "Technical Specialist, Automation"
language: "jp"
title: "Attachment module"
year: "2017"
date: "2017-01-01"
---

<div class="alert alert-warning" role="alert">
   <div>
<span>申し訳ありませんが、このページはまだ日本語に翻訳されていません。</span>
   </div>
</div>

Sometimes a project owner wants a feature implemented in their project that the developers somehow are unable to realize. The project was a website containing a form that customers would enter text data on. The project owner asked for a feature which allowed customers to attach files to this form.

As a "temporary" solution, I suggested creating a stand-alone server that would handle file uploads that would be linked to the ID number of the form. This consisted of 2 servers. One server would be a simple web interface, the other a database server that hosted a simple API.

Both servers were VMs running <a href="https://en.wikipedia.org/wiki/Red_Hat_Enterprise_Linux" target="_blank" rel="noopener noreferrer">RHEL</a>.

The web server used the <a href="https://en.wikipedia.org/wiki/Nginx" target="_blank" rel="noopener noreferrer">Nginx</a> engine and the <a href="https://en.wikipedia.org/wiki/Django_(web_framework)" target="_blank" rel="noopener noreferrer">Django</a> framework. Python handles the HTTP requests with <a href="https://en.wikipedia.org/wiki/Gunicorn" target="_blank" rel="noopener noreferrer">Gunicorn</a>, a <a href="https://en.wikipedia.org/wiki/Web_Server_Gateway_Interface" target="_blank" rel="noopener noreferrer">WSGI</a> gateway.

The database server used a <a href="https://en.wikipedia.org/wiki/MySQL" target="_blank" rel="noopener noreferrer">MySQL</a> server to host uploaded file data. To expose this data, <a href="https://flask-restful.readthedocs.io/" target="_blank" rel="noopener noreferrer">Flask RESTful</a> was used to serve a simple API.

This was the first separate project I created for the company, so I set up a <a href="https://en.wikipedia.org/wiki/Web_Server_Gateway_Interface" target="_blank" rel="noopener noreferrer">GitLab</a> server for version control.

## Workflow

* The website hosting the forms, would serve a hyperlink to the "attachment module", along with the form's ID.
* The user is redirected to the website with a simple input field and browse button where a file can be uploaded.
* The file is validated by type, size and upload time.
* The file is copied to the database server.
* The file is also cached for 2 weeks on the web server, for quicker access.
* The database server creates a database entry.
* A link to the file is created for access on the website.
* After a file is uploaded, someone else will want to access it.
* The user accesses the website and clicks on the link with the file name.
* If the file is cached on the web server, then this file is opened.
* If the file is not cached, the web server downloads the file from the database server and then opens it.

<figure class="text-center">
  <picture class="d-flex justify-content-center">
    <source
      media="(max-width: 510px)"
      srcset="/images/software/projects/attachment-module/file-uploader-w240.webp"
    />
    <source
      media="(max-width: 850px)"
      srcset="/images/software/projects/attachment-module/file-uploader-w480.webp"
    />
    <img
      src="/images/software/projects/attachment-module/file-uploader-w800.webp"
      alt="Flow diagram"
      class="img-fluid"
    />
  </picture>
    <figcaption class="figure-caption mt-2">
    <a href="/images/software/projects/attachment-module/file-uploader-w800.webp">
      Flow diagram
    </a>
  </figcaption>
</figure>

## Lessons learnt

The project owner did not want the users to separately log in to the web server of the attachment module. So a big security issue is that anyone with the link, can access uploaded files. At the time this was agreed to be a "temporary" solution. The project was deployed in 2017 and in 2021 this project was still running though. After all, the business must go on and if the problem's symptoms seems fixed, why bother with it further...

Because other users could access other uploaded files using the right link, it was decided that users could only upload and never delete any files.

A big learning point for me was that not to compromise security, even for projects that are considered temporary. A problem never occurred. This compromise caused silly decisions to not allow users to delete their own files. I will defend my standpoint much more and use this project as an example.

The actual problem here though, is why the other developers were not able to create a simple file upload feature.
