Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use regex to avoid falsely replacing ports starting with 80 #124

Merged
merged 3 commits into from
Sep 26, 2020
Merged

fix: use regex to avoid falsely replacing ports starting with 80 #124

merged 3 commits into from
Sep 26, 2020

Conversation

azrikahar
Copy link
Contributor

This PR fixes #120.

Problem Statement

Following what the issue addresses, the following logic is currently used to remove :80 when the baseURL starts with http://.

http/lib/module.js

Lines 117 to 120 in c002aaa

// Remove port 80 when http
if (options.baseURL.includes('http://')) {
options.baseURL = options.baseURL.replace(':80', '')
}

However, this logic will also be true for ANY ports that starts with :80, such as :8081, :8000 and so on since they do technically contain :80 in them.

PR Fix

We can use the regex /^http:\/\/.*:80$/ to make the check stricter, which requires the baseURL to fulfill all the following conditions:

  • Starts with http:// (^http:\/\/ : ^ means "start of line" and we need to escape / into \/)
  • contains whatever characters in between (.*: . means any character and * means appearing zero or more times)
  • Ends with :80 only (:80$: $ means "end of line")

Test Results

Test Case 1 - baseURL ends with :80

chrome_BpMxhForHB

Both logic works as intended.

Test Case 2 - baseURL ends with :8081

chrome_A6KRw1HBEe

Here we can see the original logic falsely turns http://localhost:8081 to http://localhost81 (as reported by the issue) whereas the regex logic will not trigger as intended.

@codecov-commenter
Copy link

codecov-commenter commented Aug 27, 2020

Codecov Report

Merging #124 into dev will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##              dev     #124   +/-   ##
=======================================
  Coverage   97.18%   97.18%           
=======================================
  Files           1        1           
  Lines          71       71           
  Branches       39       39           
=======================================
  Hits           69       69           
  Misses          2        2           
Impacted Files Coverage Δ
lib/module.js 97.18% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c002aaa...6d721f4. Read the comment docs.

@azrikahar
Copy link
Contributor Author

I was hesitant at first, but I double checked and noticed the test cases includes a trailing slash to the baseURL here:

expect(options.baseURL).toBe('http://localhost:3000/')

So I added another commit that checks for this as well since my original regex will fail for baseURLs with trailing slash.

Original Regex

/^http:\/\/.*:80$/

New Regex

/^http:\/\/.*:80\/?$/

The \/? means the forward slash \/ can occur once or zero times, as indicated by the ?. Sorry for not double checking before making the initial PR.

@pi0
Copy link
Member

pi0 commented Sep 1, 2020

Hi @azrikahar thanks for PR and nice explanations. I think we only have one issue with current regex which is when baseURL also has a path like (http://foo.com:80/foo) /^http:\/\/.*:80\/?$/.test('http://foo.com:80/foo') does not matches for replace. So /^http:\/\/.*:80\/|^http:\/\/.*:80$/ would do the trick (or if you have better idea)

@pi0 pi0 changed the base branch from dev to master September 1, 2020 12:49
@azrikahar
Copy link
Contributor Author

@pi0 thank you for the review :) Can't believe that scenario slipped my mind 🤦‍♂️

From my understanding, /^http:\/\/.*:80\/|^http:\/\/.*:80$/ means either it is :80/(anything or empty) or ends with :80 right? I believe we can shorten it to /^http:\/\/.*:80(\/|$)/ (which just uses your | and shorten it).

Definitely not super experienced in regex so here's additional tests for you to review in case I still miss anything.

Test code

Click to toggle source code
const baseUrls = [
  'http://foo.com:80',
  'http://foo.com:8080',
  'http://foo.com:80/',
  'http://foo.com:8080/',
  'http://foo.com:80/foo',
  'http://foo.com:8080/foo',
]

const rg = /^http:\/\/.*:80(\/|$)/

baseUrls.forEach(baseUrl => {
  console.log('before:', baseUrl)
  console.log('after: ', rg.test(baseUrl) ? baseUrl.replace(':80', '') : baseUrl)
  console.log('---')
})

Test result

chrome_go8KIMgov2

@pi0 pi0 changed the title Use regex to avoid falsely replacing ports starting with 80 fix: use regex to avoid falsely replacing ports starting with 80 Sep 26, 2020
@pi0 pi0 merged commit e981e62 into nuxt:master Sep 26, 2020
@pi0
Copy link
Member

pi0 commented Sep 26, 2020

Thanks for investigation @azrikahar <3 I merged with /^http:\/\/.*:80(\/|$)/ seems perfect!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BaseURL incorrectly modified : ENOTFOUND localhost81
3 participants