Skip to content

Commit

Permalink
Put extra checks in place for URL bar suggestions (to avoid function …
Browse files Browse the repository at this point in the history
…call on undefined)

Fixes brave#11128

Auditors: @srirambv

Test Plan:
`npm run unittest -- --grep="APP_URL_BAR_SUGGESTIONS_CHANGED"`
  • Loading branch information
bsclifton authored and syuan100 committed Nov 9, 2017
1 parent 2c8239b commit faa4054
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/renderer/reducers/urlBarReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ const updateUrlSuffix = (state, suggestionList, framePath) => {

if (autocompleteEnabled) {
const location = normalizeLocation(state.getIn(framePath.concat(['navbar', 'urlbar', 'location']))) || ''
const normalizedSuggestion = normalizeLocation(suggestion.get('location').toLowerCase())
const normalizedSuggestion = suggestion && suggestion.get && suggestion.get('location')
? normalizeLocation(suggestion.get('location').toLowerCase())
: ''
const index = normalizedSuggestion.indexOf(location.toLowerCase())
if (index === 0) {
suffix = normalizedSuggestion.substring(index + location.length)
Expand Down
20 changes: 20 additions & 0 deletions test/unit/app/renderer/reducers/urlBarReducerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,26 @@ describe('urlBarReducer', function () {
assert.equal(newState.getIn(['frames', 1, 'navbar', 'urlbar', 'suggestions', 'suggestionList']), suggestionList)
assert.equal(newState.getIn(['frames', 1, 'navbar', 'urlbar', 'suggestions', 'selectedIndex']), null)
})
describe('when autocompleteEnabled', function () {
it('handles a null urlbar location', function () {
let testState = windowState.setIn(['frames', 1, 'navbar', 'urlbar', 'suggestions', 'autocompleteEnabled'], true)
testState = testState.setIn(['frames', 1, 'navbar', 'urlbar', 'location'], undefined)
const suggestionList = Immutable.fromJS([{location: 'http://example.com'}])
urlBarReducer(testState, {actionType: appConstants.APP_URL_BAR_SUGGESTIONS_CHANGED, suggestionList, selectedIndex: null})
})
it('handles a suggestion that is not immutable', function () {
let testState = windowState.setIn(['frames', 1, 'navbar', 'urlbar', 'suggestions', 'autocompleteEnabled'], true)
testState = testState.setIn(['frames', 1, 'navbar', 'urlbar', 'location'], 'http://example.com')
const suggestionList = Immutable.fromJS(['test'])
urlBarReducer(testState, {actionType: appConstants.APP_URL_BAR_SUGGESTIONS_CHANGED, suggestionList, selectedIndex: null})
})
it('handles a suggestion without location', function () {
let testState = windowState.setIn(['frames', 1, 'navbar', 'urlbar', 'suggestions', 'autocompleteEnabled'], true)
testState = testState.setIn(['frames', 1, 'navbar', 'urlbar', 'location'], 'http://example.com')
const suggestionList = Immutable.fromJS([{}])
urlBarReducer(testState, {actionType: appConstants.APP_URL_BAR_SUGGESTIONS_CHANGED, suggestionList, selectedIndex: null})
})
})
})

describe('WINDOW_SET_URL_BAR_ACTIVE', function () {
Expand Down

0 comments on commit faa4054

Please sign in to comment.