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

Capacitor code not injected to external websites when using * in allowNavigation #1389

Closed
4 of 7 tasks
jcesarmobile opened this issue Apr 2, 2019 · 4 comments · Fixed by #1417
Closed
4 of 7 tasks

Comments

@jcesarmobile
Copy link
Member

jcesarmobile commented Apr 2, 2019

Description of the problem:
When using * in allowNavigation, the Capacitor javascript is not injected to the external page
Create an app that just redirects to https://www.google.es (or your country one) and add

"server": {
    "allowNavigation": [
      "*.google.es"
    ]
  }

Check Capacitor object and it won't be there.

Change to

"server": {
    "allowNavigation": [
      "www.google.es"
    ]
  }

and Capacitor object should be there

Affected platform

  • Android
  • iOS
  • electron
  • web

OS of the development machine

  • Windows
  • macOS
  • linux

Other information:

Capacitor version: beta 19

node version:

npm version:

CocoaPods version:

Steps to reproduce:

Link to sample project:

@leshik
Copy link
Contributor

leshik commented Apr 3, 2019

@jcesarmobile I did some tests on the fresh project and here is what I have found (let's use www.example.com for consistency and reproducibility; it offers https too):

  1. Wildcards aren't working as you already mentioned in this issue;
  2. When the domain matches exactly the invoked URL, it works (e.g. you put www.example.com to allowNavigation, then open https://www.example.com in WebView);
  3. If the domain matches, but the URL has some path, the Capacitor object is not injected. For example:

capacitor.config.json:

{
  "appId": "com.example.app",
  "appName": "App",
  "bundledWebRuntime": true,
  "webDir": "www",
  "server": {
    "allowNavigation": [
      "www.example.com"
    ]
  }
}

www/index.html:

<!DOCTYPE html>
<html>
<head>
  <script>
    location.href = 'https://www.example.com/test'
  </script>
</head>
<body>
</body>
</html>

results in Uncaught ReferenceError: Capacitor is not defined error.

Plain http isn't working in this case either, this error isn't related to https.

jcesarmobile pushed a commit that referenced this issue Apr 25, 2019
Makes it possible use star masks in `server.allowNavigation` option of `capacitor.config.json`.
Examples:
- `*.example.com` would match `app.example.com`, `www.example.com` but not `example.com`
- `192.168.*.*` is valid

What the changes are:
- `HostMask` is a matcher, it parses text into masks and check strings against it `boolean matches(String host)`, i.e. `HostMask.Parser.parse("192.168.*.*).matches("192.168.1.1")` would return `true`
- `HostMaskTest` is a test suite for the above, more mask examples are inside
- `UriMatcher` was tought to use `HostMask` for the host part
- `Bridge` contained some matching of its own, that was replaced with use of `HostMask`

Closes #1389
@Venryx
Copy link

Venryx commented May 26, 2020

Just wanted to mention that, although the pull-request adding wildcard support is very helpful, there is an edge-case: the URI matcher does not consider the port segment to be a separate segment.

Example: For the URI 192.168.0.31:4001, it counts 31:4001 as one segment. (ie. it splits only on the . character, not :)

So to match it, you must use 192.168.*.*, rather than 192.168.*.*:4001 or 192.168.*.*:*.


What was confusing me is that I tried all of these:

  1. 192.168.0.31 -> fails, since 31:4001 doesn't match 31.
  2. 192.168.*.*:4001 -> fails, since 31:4001 doesn't match *:4001. (wildcard not recognized)
  3. 192.168.*.*:* -> fails, since 31:4001 doesn't match *:*. (wildcard not recognized)

However, these work just fine:

  1. 192.168.0.31:4001
  2. 192.168.*.*

In other words, a "non-wildcard" 4th-digit requires that you enter the port, whereas a "wildcard" 4th-digit requires that you do not enter the port. (which is a bit confusing)

Real-world drawback: It forces you to either...

  1. Hard-code what ip-address all developers must have their webpack-dev-server on: 192.168.0.31:4001
  2. Remove the port requirement, to fix recognition of the wildcard: 192.168.*.*

The second is obviously better -- though it is a bit unfortunate that you can't restrict it to a specific port, if using a wildcard for the last ip-address segment.

@leo-buneev
Copy link

leo-buneev commented Jun 10, 2020

For whoever will stumble upon this issue - Capacitor is also not injected if your app uses ServiceWorkers.

Solution available in #1655

@ionitron-bot
Copy link

ionitron-bot bot commented Nov 11, 2022

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Nov 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
4 participants