beaugunderson / ip-address

💻 a library for parsing and manipulating IPv4 and IPv6 addresses in JavaScript
http://ip-address.js.org/
MIT License
582 stars 73 forks source link

Address6.fromURL unexpected behaviour: Valid (IPv6) address truncated from invalid address #158

Open buffless-matt opened 2 years ago

buffless-matt commented 2 years ago

Description

> Address6.fromURL('http://1234:5678::abcdxyz');
{
  address: Address6 {
    addressMinusSuffix: '1234:5678::abcd',
    parsedSubnet: '',
    subnet: '/128',
    subnetMask: 128,
    v4: false,
    zone: '',
    isInSubnet: [Function: isInSubnet],
    isCorrect: [Function (anonymous)],
    groups: 8,
    address: '1234:5678::abcd',
    elidedGroups: 5,
    elisionBegin: 2,
    elisionEnd: 7,
    parsedAddress: [
      '1234', '5678',
      '0',    '0',
      '0',    '0',
      '0',    'abcd'
    ]
  },
  port: null
}
> Address6.fromURL('http://[1234:5678::abcdxyz]');
{
  address: Address6 {
    addressMinusSuffix: '1234:5678::abcd',
    parsedSubnet: '',
    subnet: '/128',
    subnetMask: 128,
    v4: false,
    zone: '',
    isInSubnet: [Function: isInSubnet],
    isCorrect: [Function (anonymous)],
    groups: 8,
    address: '1234:5678::abcd',
    elidedGroups: 5,
    elisionBegin: 2,
    elisionEnd: 7,
    parsedAddress: [
      '1234', '5678',
      '0',    '0',
      '0',    '0',
      '0',    'abcd'
    ]
  },
  port: null
}

I.e. the trailing "xyz" (in both cases) is truncated to extract a valid address from the sub-string before it. The valid sub-string and the invalid suffix can be generalised (beyond the above examples).

Not sure if this is a bug, or expected behaviour. Regardless, it was unexpected for me.