express-security-txt
Advanced tools
Comparing version 2.4.0 to 2.5.0
@@ -145,2 +145,4 @@ const securityTxt = require('../index') | ||
global.console.warn = jest.fn() | ||
const res = securityTxt.formatSecurityPolicy(options) | ||
@@ -152,2 +154,32 @@ | ||
) | ||
expect(global.console.warn).toHaveBeenCalled() | ||
}) | ||
test('preferredLanguages directive works with multiple values', () => { | ||
const options = { | ||
contact: 'mailto:security@example.com', | ||
preferredLanguages: ['en', 'ru', 'es'] | ||
} | ||
const res = securityTxt.formatSecurityPolicy(options) | ||
expect(res).toBe( | ||
'Contact: mailto:security@example.com\n' + | ||
'Preferred-Languages: en, ru, es\n' | ||
) | ||
}) | ||
test('preferredLanguages directive works with one value only', () => { | ||
const options = { | ||
contact: 'mailto:security@example.com', | ||
preferredLanguages: 'en' | ||
} | ||
const res = securityTxt.formatSecurityPolicy(options) | ||
expect(res).toBe( | ||
'Contact: mailto:security@example.com\n' + | ||
'Preferred-Languages: en\n' | ||
) | ||
}) |
@@ -220,1 +220,33 @@ const securityTxt = require('../index') | ||
}) | ||
test('validate successfully for the preferredLanguages key', () => { | ||
const optionsWithArray = { | ||
contact: '...', | ||
preferredLanguages: ['en', 'es'] | ||
} | ||
const optionsWithString = { | ||
contact: '...', | ||
preferredLanguages: 'ru' | ||
} | ||
const optionsWithComment = { | ||
contact: '...', | ||
preferredLanguages: { comment: 'I am fluent in these', value: ['en', 'ru'] } | ||
} | ||
expect(() => securityTxt.validatePolicyFields(optionsWithArray)).not.toThrow() | ||
expect(() => securityTxt.validatePolicyFields(optionsWithString)).not.toThrow() | ||
expect(() => securityTxt.validatePolicyFields(optionsWithComment)).not.toThrow() | ||
}) | ||
test('validate fails if Array<object> fed to preferredLanguages', () => { | ||
const options = { | ||
contact: '...', | ||
preferredLanguages: [ | ||
{ comment: '...', value: 'en' } | ||
] | ||
} | ||
expect(() => securityTxt.validatePolicyFields(options).toThrow()) | ||
}) |
11
index.js
'use strict' | ||
const Joi = require('joi') | ||
const DIRECTIVES = ['Contact', 'Encryption', 'Acknowledgments', 'Signature', 'Policy', 'Hiring', 'Permission'] | ||
const DIRECTIVES = ['Contact', 'Encryption', 'Acknowledgments', 'Preferred-Languages', 'Signature', 'Policy', 'Hiring', 'Permission'] | ||
@@ -107,2 +107,10 @@ /** | ||
// For the other fields, arrays are used to represent multiple occurences | ||
// of a field. However, for the Preferred-Language: directive, an array shows | ||
// a comma separated list. Convert the provided array into an array of one | ||
// value: a string with commas. | ||
if (outputDirective === 'Preferred-Languages') { | ||
value = [ value.map(languageCode => languageCode.trim()).join(', ') ] | ||
} | ||
value.forEach(valueOption => { | ||
@@ -191,2 +199,3 @@ if (valueOption.hasOwnProperty('value')) { | ||
encryption: fieldValue({ singleValue: string.regex(/^(?!http:)/i) }), | ||
preferredLanguages: fieldValue({ canBeArray: false, singleValue: array.items(string) }), | ||
policy: fieldValue(), | ||
@@ -193,0 +202,0 @@ hiring: fieldValue(), |
{ | ||
"name": "express-security-txt", | ||
"version": "2.4.0", | ||
"version": "2.5.0", | ||
"description": "Express middleware that implements a security.txt path and policy", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
311571
617