Skip to content

Commit

Permalink
[Fleet] Logstash Output - being compliant to RFC-952 (elastic#176298)
Browse files Browse the repository at this point in the history
## Summary

The logstash output in fleet was not following the
[RFC-952](https://www.rfc-editor.org/rfc/rfc952) correctly, only
accepting lowercase url's for the hostname, even though according to the
standard, this should be irrelevant.

Additionally added two new unit tests, to check that this behaviour is
compliant in the future as well.

## Fix explanation

The issue was in the past that the URL Type in nodejs is automatically
lowercase, and then the value of the form is or the string provided is
checked against this url type. The types behaviour is explained here:
https://nodejs.org/api/url.html#the-whatwg-url-api

As we would like to be case insensitive, in the check, the value is also
lowerecased with: val.toLowerCase() to pass the check

### Checklist

Delete any items that are not applicable to this PR.

- [X] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

(cherry picked from commit 5542917)
  • Loading branch information
Terilia committed Feb 6, 2024
1 parent 341e9e6 commit aad4781
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ describe('Output form validation', () => {

expect(res).toBeUndefined();
});

it('should work with hostnames using uppercase letters', () => {
const res = validateLogstashHosts(['tEsT.fr:9200', 'TEST2.fr:9200', 'teSt.fR:9999']);

expect(res).toBeUndefined();
});

it('should throw for invalid hosts starting with http', () => {
const res = validateLogstashHosts(['https://test.fr:5044']);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export function validateLogstashHosts(value: string[]) {

const url = new URL(`http://${val}`);

if (url.host !== val) {
if (url.host !== val.toLowerCase()) {
throw new Error('Invalid host');
}
} catch (error) {
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/fleet/server/types/models/output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ describe('Output model', () => {
expect(validateLogstashHost('test.fr:5044')).toBeUndefined();
});

it('should support valid host with uppercase letters', () => {
expect(validateLogstashHost('tEsT.fr:5044')).toBeUndefined();
});

it('should return an error for an invalid host', () => {
expect(validateLogstashHost('!@#%&!#!@')).toMatchInlineSnapshot(`"Invalid Logstash host"`);
});
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/server/types/models/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function validateLogstashHost(val: string) {
try {
const url = new URL(`http://${val}`);

if (url.host !== val) {
if (url.host !== val.toLowerCase()) {
return 'Invalid host';
}
} catch (err) {
Expand Down

0 comments on commit aad4781

Please sign in to comment.