]> BookStack Code Mirror - website/blob - content/docs/admin/security.md
f9319e48d22dbd42ba3ed8b5805d695b36aa0328
[website] / content / docs / admin / security.md
1 +++
2 title = "Security"
3 description = "BookStack security concerns and considerations"
4 date = "2017-01-01"
5 type = "admin-doc"
6 +++
7
8 Since BookStack can hold important information for users you should be aware of any potential security concerns.
9 Read through the below to ensure you have secured your BookStack instance. Note, The below only
10 relates to BookStack itself. The security of the server BookStack is hosted on is not instructed below but should be taken into account.
11
12 If you'd like to be notified of new potential security concerns you can sign-up to the [BookStack security mailing list](https://updates.bookstackapp.com/signup/bookstack-security-updates). For reporting security vulnerabilities, please see the ["Security" section](https://github.com/BookStackApp/BookStack/blob/development/readme.md#-security) of the project readme on GitHub.
13
14
15 <ul>
16     <li><a href="#initial-security-setup">Initial Security Setup</a></li>
17     <li><a href="#mfa">Multi-Factor Authentication</a></li>
18     <li><a href="#securing-images">Securing Images</a></li>
19     <li><a href="#attachments">Attachments</a></li>
20     <li><a href="#user-passwords">User Passwords</a></li>
21     <li><a href="#javascript-in-page-content">JavaScript in Page Content</a></li>
22     <li><a href="#web-crawler-control">Web Crawler Control</a></li>
23     <li><a href="#secure-cookies">Secure Cookies</a></li>
24     <li><a href="#iframe-control">Host IFrame Control</a></li>
25     <li><a href="#failed-access-logging">Failed Access Logging</a></li>
26     <li><a href="#server-side-requests">Untrusted Server Side Requests</a></li>
27     <li><a href="#csp">Content Security Policy (CSP)</a></li>
28 </ul>
29
30 ---
31
32 <a name="initial-security-setup"></a>
33
34 ### Initial Security Setup
35
36 1. Ensure you change the password and email address for the initial `admin@admin.com` user.
37 2. Ensure only the `public` folder is being served by your webserver. Serving files above this folder
38 opens up a lot of code that does not need to be public. Triple check this if you have installed
39 BookStack within the commonly used `/var/www` folder.
40 3. Ensure the database user you've used for BookStack has limited permissions for only accessing
41 the database used for BookStack data.
42 4. Check that you've set the `APP_URL` option in your `.env` file so that system generated URLs cannot be manipulated.
43 5. Within BookStack, go through the settings to ensure registration and public access settings are as you expect.
44 6. Review the user roles in the settings area.
45 7. Read the below to further understand the security for images & attachments.
46
47 ---
48
49 <a name="mfa"></a>
50
51 ### Multi-Factor Authentication
52
53 Any user can enable multi-factor authentication (MFA) on their account. Upon login they would then need to use an extra proof of identity
54 to gain access. BookStack currently supports the following mechanisms:
55
56 - TOTP (Time-based One-Time Passwords)
57   - Labelled as "Mobile App" (Google/Microsoft Authenticator etc...).
58   - Uses a SHA1 algorithm internally (Greater algorithms have poor cross-app compatibility).
59 - Backup Codes
60   - These are a list of 16 one-time-use codes.
61   - Users will be warned once they have less than 5 codes remaining.
62
63 Secrets and values for these options are stored encrypted within the database.
64
65 Where required, MFA can be forced upon users via their roles. This can be found via
66 a "Requires Multi-Factor Authentication" checkbox seen when editing a role.
67 If a user does not already have an MFA method configured, they will be forced to set one up
68 upon next login.
69
70 ---
71
72 <a name="securing-images"></a>
73
74 ### Securing Images
75
76 By default, Images are stored in a way which is publicly accessible. This is done on purpose to ensure decent performance while using BookStack. Below are a couple of options to increasing image security:
77
78 #### Image Authentication
79
80 You can choose to store images behind authentication so only logged-in users can view images. This solution is currently still in testing you could experience performance issues. This option will only work if you have the  'Allow Public Viewing' setting disabled.
81
82 ***Back-up your BookStack instance before migrating to this option***
83
84 To use this option simply set `STORAGE_TYPE=local_secure` in your `.env` file. Uploaded images will be stored within the `storage/uploads/images` folder.
85
86 If you are migrating to this option with existing images you will need to move all content in the folder `public/uploads/images` to `storage/uploads/images`. Do not simply copy and leave content in the `public/uploads/images` as those images will still be publicly accessible. After doing this migration you may have to clean-up and re-upload any 'App Icon' images, found in settings, since these need to remain publicly accessible. 
87
88 #### Complex Urls
89
90 In the settings area of BookStack you can find the option 'Enable higher security image uploads?'. Enabling this will add a 16 character
91 random string to the name of image files to prevent easy guessing of URLs. This increases security without potential performance concerns.
92
93 It's important to ensure you disable 'directory indexes' to prevent unknown users from being able to navigate their way through your images. Here's the configuration for NGINX & Apache if your server allows directory indexes:
94
95 **NGINX**
96
97 ```nginx
98 # By default indexes are disabled on Nginx but if you have them enabled
99 # add this to your BookStack server block
100 location /uploads {
101        autoindex off;
102 }
103 ```
104
105 **Apache**
106
107 ```apache
108 # Add this to your Apache BookStack virtual host if Indexes are enabled.
109 # If .htaccess file are enabled then the below should already be active.
110 <Location "/uploads">
111     Options -Indexes
112 </Location>
113 ```
114
115 ---
116
117 <a name="attachments"></a>
118
119 ### Attachments
120
121 Attachments, if not using Amazon S3, are stored in the `storage/uploads` directory.
122 By default, unlike images, these are stored behind the application authentication layer so access
123 depends on permissions you have set up at a role level and page level.
124
125 If you are using Amazon S3 for file storage then access will depend on your S3 permission
126 settings. Unlike images, BookStack will not automatically attempt to make uploaded attachments
127 publically accessible.  
128
129 ---
130
131 <a name="user-passwords"></a>
132
133 ### User Passwords
134
135 User passwords, if not using an alternative authentication method, are stored in the database.
136 These are hashed using the standard Laravel hashing methods which use the Bcrypt hashing algorithm.
137
138 ---
139
140 <a name="javascript-in-page-content"></a>
141
142 ### JavaScript in Page Content
143
144 By default, JavaScript tags within page content is escaped when rendered. This can be turned off by setting `ALLOW_CONTENT_SCRIPTS=true` in your `.env` file. Note that even if you disable this escaping the WYSIWYG editor may still perform it's own JavaScript escaping. This option will also alter the [CSP rules](#csp) set by BookStack.
145
146 ***This option disables some fundemental cross-site-scripting protections. Only use this option on secure instances, where only very trusted users can edit content***
147
148 ---
149
150 <a name="web-crawler-control"></a>
151
152 ### Web Crawler Control
153
154 The rules found in the `/robots.txt` file are automatically controlled via the "Allow public viewing?" setting. This can be overridden by setting `ALLOW_ROBOTS=false` or `ALLOW_ROBOTS=true` in your `.env` file. If you'd like to customise the rules this can be done via theme overrides.
155
156 ---
157
158 <a name="secure-cookies"></a>
159
160 ### Secure Cookies
161
162 BookStack uses cookies to track sessions, remember logins and for XSRF protection. When using HTTPS you may want to ensure that cookies are only sent back to the browser if the connection is over HTTPS. If you have set a https `APP_URL` option in your `.env` this will enabled automatically but it can also be forced on by setting `SESSION_SECURE_COOKIE=true` in your `.env` file.
163
164 ---
165
166 <a name="iframe-control"></a>
167
168 ### Host Iframe Control
169
170 By default BookStack will only allow itself to be embedded within iframes on the same domain as you're hosting on. This is done through a [CSP: frame-ancestors](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/frame-ancestors) header. You can add additional trusted hosts by setting a `ALLOWED_IFRAME_HOSTS` option in your `.env` file like the example below:
171
172 ```bash
173 # Adding a single host
174 ALLOWED_IFRAME_HOSTS="https://example.com"
175
176 # Mulitple hosts can be separated with a space
177 ALLOWED_IFRAME_HOSTS="https://a.example.com https://b.example.com"
178 ```
179
180 Note: when this option is used, all cookies will served with `SameSite=None` [(info)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite#None) set so that
181 a user session can persist within the iframe.
182
183 ---
184
185 <a name="failed-access-logging"></a>
186
187 ### Failed Access Logging
188
189 An option is available to log failed login events to a log file which is useful to identify users having trouble logging in, track malicious login attempts or to use with tools such as Fail2Ban. This works with login attempts using the default email & password login mechanism or attempts via LDAP login. Failed attempts are **not logged** for "one-click" social or SAML2 options.
190
191 To enable this you simply need to define the `LOG_FAILED_LOGIN_MESSAGE` option in your `.env` file like so:
192
193 ```bash
194 LOG_FAILED_LOGIN_MESSAGE="Failed login for %u"
195 ```
196
197 The optional "%u" element of the message will be replaced with the username or email provided in the login attempt
198 when the message is logged. By default messages will be logged via the php `error_log` function which, in most
199 cases, will log to your webserver error log files.
200
201 ---
202
203 <a name="server-side-requests"></a>
204
205 ### Untrusted Server Side Requests
206
207 Some features, such as the PDF exporting, have the option to make http calls to external user-defined locations to do things
208 such as load images or styles. This is disabled by default but can be enabled if desired. This is required for using 
209 WKHTMLtoPDF as your PDF export renderer.
210
211 To enable untrusted server side requests, you need to define the `ALLOW_UNTRUSTED_SERVER_FETCHING` option in your `.env` file like so:
212
213 ```bash
214 ALLOW_UNTRUSTED_SERVER_FETCHING=true
215 ```
216
217 ---
218
219 <a name="csp"></a>
220
221 ### Content Security Policy (CSP)
222
223 BookStack serves responses with multiple CSP headers to increase protection again malicious content.
224 This is especially important in a system such as BookStack where users can create a variety of HTML content, 
225 especially so if you allow untrusted users to create content in your instance.
226 The CSP headers set by BookStack are as follows:
227
228 - `frame-ancestors 'self'`
229   - Restricts what websites can embed BookStack pages via iframes.
230   - See the "[Host Iframe Control](#iframe-control)" section above for details on expanding this rule to other hosts.
231 - `script-src http: https: 'nonce-abc123' 'strict-dynamic'`
232   - Restricts what scripts can be ran on a BookStack-served page.
233   - Will not be set if the `ALLOW_CONTENT_SCRIPTS` .env option is active.
234   - The nonce value used is randomly generated upon each request. It is automatically applied to any "Custom HTML Head Content" scripts.
235 - `object-src 'self'`
236   - Restricts which embeddable content can be loaded onto a BookStack-served page.
237   - Will not be set if the `ALLOW_CONTENT_SCRIPTS` .env option is active.
238 - `base-uri 'self'`
239   - Restricts what `<base>` tags can be added to a BookStack-served page.
240
241 If needed you should be able to set additional CSP headers via your webserver.
242 If there's a clash with an existing BookStack CSP header then browsers will generally favour the most restrictive policy.