|
| 1 | +/** |
| 2 | + * @fileoverview Check if non HTML resources responses contain certain |
| 3 | + * unneeded HTTP headers. |
| 4 | + */ |
| 5 | + |
| 6 | +// ------------------------------------------------------------------------------ |
| 7 | +// Requirements |
| 8 | +// ------------------------------------------------------------------------------ |
| 9 | + |
| 10 | +import { IFetchEndEvent, IResponse, IRule, IRuleBuilder } from '../../interfaces'; // eslint-disable-line no-unused-vars |
| 11 | +import { RuleContext } from '../../rule-context'; // eslint-disable-line no-unused-vars |
| 12 | +import { getIncludedHeaders, mergeIgnoreIncludeArrays } from '../../util/rule-helpers'; |
| 13 | + |
| 14 | +// ------------------------------------------------------------------------------ |
| 15 | +// Public |
| 16 | +// ------------------------------------------------------------------------------ |
| 17 | + |
| 18 | +const rule: IRuleBuilder = { |
| 19 | + create(context: RuleContext): IRule { |
| 20 | + |
| 21 | + let unneededHeaders = [ |
| 22 | + 'content-security-policy', |
| 23 | + 'x-content-security-policy', |
| 24 | + 'x-frame-options', |
| 25 | + 'x-ua-compatible', |
| 26 | + 'x-webkit-csp', |
| 27 | + 'x-xss-protection' |
| 28 | + ]; |
| 29 | + |
| 30 | + const loadRuleConfigs = () => { |
| 31 | + const includeHeaders = (context.ruleOptions && context.ruleOptions.include) || []; |
| 32 | + const ignoreHeaders = (context.ruleOptions && context.ruleOptions.ignore) || []; |
| 33 | + |
| 34 | + unneededHeaders = mergeIgnoreIncludeArrays(unneededHeaders, ignoreHeaders, includeHeaders); |
| 35 | + }; |
| 36 | + |
| 37 | + const willBeTreatedAsHTML = (response: IResponse) => { |
| 38 | + const mediaType = response.headers['content-type'].split(';')[0].trim(); |
| 39 | + |
| 40 | + // By default, browsers will treat resource sent with the |
| 41 | + // following media types as HTML documents. |
| 42 | + |
| 43 | + if (['text/html', 'application/xhtml+xml'].includes(mediaType)) { |
| 44 | + return true; |
| 45 | + } |
| 46 | + |
| 47 | + // That is not the situation for other cases where the media |
| 48 | + // type is in the form of `<type>/<subtype>`. |
| 49 | + |
| 50 | + if (mediaType.indexOf('/') > 0) { |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + // If the media type is not specified or invalid, browser |
| 55 | + // will try to sniff the content. |
| 56 | + // |
| 57 | + // https://mimesniff.spec.whatwg.org/ |
| 58 | + // |
| 59 | + // At this point, even if browsers may decide to treat |
| 60 | + // the content as a HTML document, things are obviously |
| 61 | + // not done correctly, so the decision was to not try to |
| 62 | + // also sniff the content, and instead, just signal this |
| 63 | + // as a problem. |
| 64 | + |
| 65 | + return false; |
| 66 | + }; |
| 67 | + |
| 68 | + const checkHeaders = (fetchEnd: IFetchEndEvent) => { |
| 69 | + const { element, resource, response } = fetchEnd; |
| 70 | + |
| 71 | + if (!willBeTreatedAsHTML(response)) { |
| 72 | + const headers = getIncludedHeaders(response.headers, unneededHeaders); |
| 73 | + |
| 74 | + if (headers.length > 0) { |
| 75 | + context.report(resource, element, `Unneeded HTTP header${headers.length > 1 ? 's' : ''} found: ${headers.join(', ')}`); |
| 76 | + } |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + loadRuleConfigs(); |
| 81 | + |
| 82 | + return { |
| 83 | + 'fetch::end': checkHeaders, |
| 84 | + 'targetfetch::end': checkHeaders |
| 85 | + }; |
| 86 | + }, |
| 87 | + meta: { |
| 88 | + docs: { |
| 89 | + category: 'performance', |
| 90 | + description: 'Disallow unneeded HTTP headers for non-HTML resources', |
| 91 | + recommended: true |
| 92 | + }, |
| 93 | + fixable: 'code', |
| 94 | + schema: { |
| 95 | + additionalProperties: false, |
| 96 | + definitions: { |
| 97 | + 'string-array': { |
| 98 | + items: { type: 'string' }, |
| 99 | + minItems: 1, |
| 100 | + type: 'array', |
| 101 | + uniqueItems: true |
| 102 | + } |
| 103 | + }, |
| 104 | + properties: { |
| 105 | + ignore: { $ref: '#/definitions/string-array' }, |
| 106 | + include: { $ref: '#/definitions/string-array' } |
| 107 | + }, |
| 108 | + type: ['object', null] |
| 109 | + } |
| 110 | + } |
| 111 | +}; |
| 112 | + |
| 113 | +module.exports = rule; |
0 commit comments