-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow comments to be inserted into the security.txt file
We provide a way for developers to insert comments within a security.txt file (i.e, lines starting with a #) by allowing special objects and additional special keys. We also document this, as well as other features we've added in the past. #33
- Loading branch information
Showing
4 changed files
with
259 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,14 +34,88 @@ and use it as a middleware for an express app. | |
const securityTxt = require('express-security-txt') | ||
|
||
const options = { | ||
contact: '[email protected]', | ||
contact: 'mailto:[email protected]', | ||
encryption: 'https://www.mykey.com/pgp-key.txt', | ||
acknowledgement: 'thank you' | ||
} | ||
|
||
app.use(securityTxt.setup(options)) | ||
``` | ||
### Chaining | ||
|
||
Where allowed, you can provide multiple values for a single directive by passing an array. | ||
|
||
```js | ||
const securityTxt = require('express-security-txt') | ||
|
||
const options = { | ||
contact: [ | ||
'https://firstMethodOfContact.example.com', | ||
'https://secondMethodOfContact.example.com' | ||
] | ||
} | ||
|
||
app.use(securityTxt.setup(options)) | ||
``` | ||
|
||
### Comments | ||
|
||
To add a comment at the beggining or end of the security.txt file, one may use the keys `_prefixComment` and `_postfixComment` respectively. If one wishes to place a comment immediately before a field, one may use an object which specifies the value of the field and the comment which must come before it. | ||
|
||
```js | ||
const securityTxt = require('express-security-txt') | ||
|
||
const options = { | ||
_prefixComment: 'This comment goes at the very beggining of the file', | ||
contact: { | ||
comment: 'This comment goes directly before the Contact: directive', | ||
value: 'mailto:[email protected]' | ||
}, | ||
encryption: [ | ||
'https://example.com/encryption', | ||
{ | ||
comment: 'Comments can appear in the middle of an array of values', | ||
value: 'https://example.com/alternativeEncryption' | ||
} | ||
], | ||
_postfixComment: 'This comment goes at the very end of the file' | ||
} | ||
|
||
app.use(securityTxt.setup(options)) | ||
``` | ||
|
||
Would generate the file | ||
|
||
```txt | ||
# This comment goes at the very beggining of the file | ||
# This comment goes directly before the Contact: directive | ||
Contact: mailto:[email protected] | ||
Encryption: https://example.com/encryption | ||
# Comments can appear in the middle of an array of values | ||
Encryption: https://example.com/alternativeEncryption | ||
# This comment goes at the very end of the file | ||
``` | ||
|
||
If your comment spans multiple lines, you can use `\n` to split it. express-security-txt will automatically insert the relevant `#` symbols. Alternatively, one can use an array of lines instead of a string. | ||
|
||
For example: | ||
|
||
```js | ||
const options = { | ||
_prefixComment: ['this is a', 'comment\nwhich', 'spans many lines'], | ||
contact: 'mailto:[email protected]' | ||
} | ||
``` | ||
|
||
Would generate | ||
|
||
```txt | ||
# this is a | ||
# comment | ||
# which | ||
# spans many lines | ||
Contact: mailto:[email protected] | ||
``` | ||
## Tests | ||
|
||
Project tests: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,3 +136,65 @@ test('validate fails when providing arrays for signature/permission', () => { | |
|
||
expect(() => securityTxt.validatePolicyFields(options)).toThrow() | ||
}) | ||
|
||
test('validate successfully when using prefix/postfix comments', () => { | ||
const options = { | ||
_prefixComment: ['This is a\nprefix', 'comment'], | ||
_postfixComment: 'This is a \npostfix comment', | ||
contact: 'mailto:[email protected]' | ||
} | ||
|
||
expect(() => securityTxt.validatePolicyFields(options)).not.toThrow() | ||
}) | ||
|
||
test('validate successfully when using objects for comments', () => { | ||
const options = { | ||
contact: [ | ||
{ | ||
comment: ['...', '...'], | ||
value: 'mailto:[email protected]' | ||
}, | ||
{ | ||
value: 'tel:+123' | ||
} | ||
], | ||
encryption: { | ||
comment: '...', | ||
value: 'https://encryption.example.com' | ||
} | ||
} | ||
|
||
expect(() => securityTxt.validatePolicyFields(options)).not.toThrow() | ||
}) | ||
|
||
test('validate fails when not providing a value in comment object', () => { | ||
const singleObject = { | ||
contact: { | ||
comment: '' | ||
} | ||
} | ||
|
||
const arrayOfObjects = { | ||
contact: [ | ||
{ | ||
comment: '...', | ||
value: 'tel:+123' | ||
}, | ||
{ | ||
comment: '...' | ||
} | ||
] | ||
} | ||
|
||
expect(() => securityTxt.validatePolicyFields(singleObject)).toThrow() | ||
expect(() => securityTxt.validatePolicyFields(arrayOfObjects)).toThrow() | ||
}) | ||
|
||
test('validate fails when using a [{value: [...]}] nested array', () => { | ||
const options = { | ||
contact: [{ value: ['test'] }], | ||
encryption: [{ value: ['test'] }] | ||
} | ||
|
||
expect(() => securityTxt.validatePolicyFields(options)).toThrow() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters