Skip to content
Myr

fix(accepts, language): skip accept entries with quality 0 when matching (#5311)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(accepts, language): skip accept entries with quality 0 when matching A quality value of 0 marks a range as explicitly not acceptable (RFC 9110 §12.5.1/§12.5.4), but accepts()'s defaultMatch and the language detector's header detection still matched such entries, e.g. Accept: application/json;q=0 was served application/json. The compress middleware already applies this rule; align the other two consumers with it. * test(accepts): add test for subtype wildcard rejected with q=0 * test(language): continue to next detector after zero-quality language

authored by LuckTerence · committed by GitHub · parent 90e1b94 · Browse files · 4 files+108 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+108 added−7 removed

  • tests:+92 added−0 removed
  • other:+16 added−7 removed

4 files+108 added−7 removed

modified:src/helper/accepts/accepts.test.ts+48 added
188 unmodified lines
189 }190 const result = accepts(c, options)191 expect(result).toBe('application/xml')192 })193194 test('should not match entries explicitly rejected with q=0', () => {195 const c = {196 req: {197 header: () => 'application/json;q=0',198 },199 // eslint-disable-next-line @typescript-eslint/no-explicit-any200 } as any201 const options: acceptsConfig = {202 header: 'Accept',203 supports: ['application/json'],204 default: 'text/html',205 }206 const result = accepts(c, options)207 expect(result).toBe('text/html')208 })209210 test('should skip q=0 entries and match the remaining ones', () => {211 const c = {212 req: {213 header: () => 'text/html;q=0.5, application/json;q=0',214 },215 // eslint-disable-next-line @typescript-eslint/no-explicit-any216 } as any217 const options: acceptsConfig = {218 header: 'Accept',219 supports: ['application/json'],220 default: 'application/octet-stream',221 }222 const result = accepts(c, options)223 expect(result).toBe('application/octet-stream')224 })225226 test('should not match a subtype wildcard the client rejected with q=0', () => {227 const c = {228 req: {229 header: () => 'application/*;q=0',230 },231 // eslint-disable-next-line @typescript-eslint/no-explicit-any232 } as any233 const options: acceptsConfig = {234 header: 'Accept',235 supports: ['application/json'],236 default: 'text/html',237 }238 const result = accepts(c, options)239 expect(result).toBe('text/html')240 })241})242243describe('Usage', () => {244 test('decide compression by Accept-Encoding header', async () => {
65 unmodified lines
modified:src/helper/accepts/accepts.ts+10 added-6 removed
modified:src/middleware/language/index.test.ts+44 added
90 unmodified lines
91 })92 expect(await res.text()).toBe('ja')93 })9495 it('should not detect a language explicitly rejected with q=0', async () => {96 const app = createTestApp({97 supportedLanguages: ['fr', 'en'],98 fallbackLanguage: 'en',99 order: ['header'],100 })101102 const res = await app.request('/', {103 headers: {104 'accept-language': 'fr;q=0',105 },106 })107 expect(await res.text()).toBe('en')108 })109110 it('should skip q=0 languages before falling back to the default', async () => {111 const app = createTestApp({112 supportedLanguages: ['fr', 'en'],113 fallbackLanguage: 'en',114 order: ['header'],115 })116117 const res = await app.request('/', {118 headers: {119 'accept-language': 'fr;q=0,de;q=0.8',120 },121 })122 expect(await res.text()).toBe('en')123 })124125 it('should continue to the next detector after a zero-quality language', async () => {126 const app = createTestApp({127 supportedLanguages: ['fr', 'en', 'ja'],128 fallbackLanguage: 'en',129 order: ['header', 'querystring'],130 })131132 const res = await app.request('/?lang=ja', {133 headers: { 'accept-language': 'fr;q=0' },134 })135 expect(await res.text()).toBe('ja')136 expect(res.headers.get('set-cookie')).toContain('language=ja')137 })138139 it('should match after multiple truncations', async () => {140 const app = createTestApp({141 supportedLanguages: ['zh-Hant', 'en'],142 fallbackLanguage: 'en',
410 unmodified lines
modified:src/middleware/language/language.ts+6 added-1 removed
156 unmodified lines
157 return undefined158 }159160 const languages = parseAcceptLanguage(acceptLanguage)161 for (const { lang } of languages) {161 for (const { lang, q } of languages) {162 // A quality value of 0 means the language is explicitly rejected163 // (RFC 9110 §12.5.4), so it must not be detected.164 if (q === 0) {165 continue166 }167 const normalizedLang = normalizeLanguage(lang, options)168 if (normalizedLang) {169 return normalizedLang170 }
144 unmodified lines