Skip to content

Commit

Permalink
#486 - test path traversal vulnerability
Browse files Browse the repository at this point in the history
  • Loading branch information
mashpie committed Dec 29, 2021
1 parent 04f49be commit 5a08ecc
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 0 deletions.
1 change: 1 addition & 0 deletions locales_traversal/is/not/possible/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
keep me
173 changes: 173 additions & 0 deletions test/i18n.noTraversal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
const fs = require('fs')
const path = require('path')
const { I18n } = require('..')
require('should')

const config = {
locales: ['en'],
directory: path.join(__dirname, '../locales_traversal/is/not/possible')
}
const testfile = path.join(config.directory, 'en.json')

const getJson = () => {
return JSON.parse(fs.readFileSync(testfile))
}

describe('No directory traversal for writing', () => {
before('cleanup assertions', () => {
try {
fs.unlinkSync(testfile)
} catch (_) {}
})

it(`setLocale('../../foo') SHOULD write to testfile`, () => {
const i18n = new I18n(config)
const req = {}
i18n.init(req)
req.setLocale('../../foo')
req.getLocale().should.equal('en')
req.__('Hello setLocale').should.equal('Hello setLocale')
getJson()['Hello setLocale'].should.equal('Hello setLocale')
})

it(`accept-language: '../../en-US' SHOULD write to testfile`, () => {
const i18n = new I18n(config)
const req = {
headers: {
'accept-language': '../../en-US;q=1.0;fr-FR,fr;q=0.7,tr;q=0.3'
}
}
i18n.init(req)
req.getLocale().should.equal('en')
req.__('Hello Header 1').should.equal('Hello Header 1')
getJson()['Hello Header 1'].should.equal('Hello Header 1')
})

it(`accept-language: 'en/../..' SHOULD write to testfile`, () => {
const i18n = new I18n(config)
const req = {
headers: {
'accept-language': 'en/../..;q=1.0;fr-FR,fr;q=0.7,tr;q=0.3'
}
}
i18n.init(req)
req.getLocale().should.equal('en')
req.__('Hello Header 2').should.equal('Hello Header 2')
getJson()['Hello Header 2'].should.equal('Hello Header 2')
})

it(`cookie: '../../en' SHOULD write to testfile`, () => {
const i18n = new I18n({ ...config, cookie: 'i18nCookie' })
const req = {
cookies: { i18nCookie: '../../en' }
}
i18n.init(req)
req.getLocale().should.equal('en')
req.__('Hello Cookie 1').should.equal('Hello Cookie 1')
getJson()['Hello Cookie 1'].should.equal('Hello Cookie 1')
})

it(`cookie: 'en/../..' SHOULD write to testfile`, () => {
const i18n = new I18n({ ...config, cookie: 'i18nCookie' })
const req = {
cookies: { i18nCookie: 'en/../..' }
}
i18n.init(req)
req.getLocale().should.equal('en')
req.__('Hello Cookie 2').should.equal('Hello Cookie 2')
getJson()['Hello Cookie 2'].should.equal('Hello Cookie 2')
})

it(`query: '../../en' SHOULD write to testfile`, () => {
const i18n = new I18n({ ...config, queryParameter: 'lang' })
const req = { url: '/test?lang=../../en' }
i18n.init(req)
req.getLocale().should.equal('en')
req.__('Hello Query 1').should.equal('Hello Query 1')
getJson()['Hello Query 1'].should.equal('Hello Query 1')
})

it(`query: 'en/../..' SHOULD write to testfile`, () => {
const i18n = new I18n({ ...config, queryParameter: 'lang' })
const req = { url: '/test?lang=en/../..' }
i18n.init(req)
req.getLocale().should.equal('en')
req.__('Hello Query 2').should.equal('Hello Query 2')
getJson()['Hello Query 2'].should.equal('Hello Query 2')
})
})

describe('No directory traversal for reading', () => {
before('prepare assertion', () => {
fs.writeFileSync(testfile, `{ "Hello": "Hello from 'en.json'" }`)
})

it(`setLocale('../some') SHOULD read from testfile`, () => {
const i18n = new I18n(config)
const req = {}
i18n.init(req)
req.setLocale('../../foo')
req.getLocale().should.equal('en')
req.__('Hello').should.equal(`Hello from 'en.json'`)
})

it(`accept-language: '../../en-US' SHOULD read from testfile`, () => {
const i18n = new I18n(config)
const req = {
headers: {
'accept-language': '../../en-US;q=1.0;fr-FR,fr;q=0.7,tr;q=0.3'
}
}
i18n.init(req)
req.getLocale().should.equal('en')
req.__('Hello').should.equal(`Hello from 'en.json'`)
})

it(`accept-language: 'en/../..' SHOULD read from testfile`, () => {
const i18n = new I18n(config)
const req = {
headers: {
'accept-language': 'en/../..;q=1.0;fr-FR,fr;q=0.7,tr;q=0.3'
}
}
i18n.init(req)
req.getLocale().should.equal('en')
req.__('Hello').should.equal(`Hello from 'en.json'`)
})

it(`cookie: '../../en' SHOULD read from testfile`, () => {
const i18n = new I18n({ ...config, cookie: 'i18nCookie' })
const req = {
cookies: { i18nCookie: '../../en' }
}
i18n.init(req)
req.getLocale().should.equal('en')
req.__('Hello').should.equal(`Hello from 'en.json'`)
})

it(`cookie: 'en/../..' SHOULD read from testfile`, () => {
const i18n = new I18n({ ...config, cookie: 'i18nCookie' })
const req = {
cookies: { i18nCookie: 'en/../..' }
}
i18n.init(req)
req.getLocale().should.equal('en')
req.__('Hello').should.equal(`Hello from 'en.json'`)
})

it(`query: '../../en' SHOULD read from testfile`, () => {
const i18n = new I18n({ ...config, queryParameter: 'lang' })
const req = { url: '/test?lang=../../en' }
i18n.init(req)
req.getLocale().should.equal('en')
req.__('Hello').should.equal(`Hello from 'en.json'`)
})

it(`query: 'en/../..' SHOULD read from testfile`, () => {
const i18n = new I18n({ ...config, queryParameter: 'lang' })
const req = { url: '/test?lang=en/../..' }
i18n.init(req)
req.getLocale().should.equal('en')
req.__('Hello').should.equal(`Hello from 'en.json'`)
})
})

0 comments on commit 5a08ecc

Please sign in to comment.