mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
fix(parseProtocol): fix regex to require colon in protocol separator (#10729)
The regex used `(:?\/\/|:)` where `:?` was intended to be a non-capturing group `(?:` but instead made the colon an optional literal character. This caused URLs without a colon separator like `http//example.com` to incorrectly match and return `http` as the protocol. Fix the regex to `:(?:\/\/)?` which requires the colon and optionally matches `//`, correctly rejecting malformed URLs. Signed-off-by: Nikita Skovoroda <chalkerx@gmail.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Nikita Skovoroda <chalkerx@gmail.com> Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
export default function parseProtocol(url) {
|
export default function parseProtocol(url) {
|
||||||
const match = /^([-+\w]{1,25})(:?\/\/|:)/.exec(url);
|
const match = /^([-+\w]{1,25}):(?:\/\/)?/.exec(url);
|
||||||
return (match && match[1]) || '';
|
return (match && match[1]) || '';
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,4 +25,8 @@ describe('helpers::parseProtocol', () => {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should not match URLs without a colon separator', () => {
|
||||||
|
assert.strictEqual(parseProtocol('http//example.com'), '');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user