Skip to content

Commit

Permalink
Change usage stats check for first-party request headers
Browse files Browse the repository at this point in the history
First implementation made a best-effort attempt to determine
whether or not a request was "first-party" (from the Kibana client)
by checking "kbn-version", "origin", and "referer". If these three
headers are all present, we thought it was very likely that the
request was first-party. However, further testing after adding
usage stats collection for additional SO APIs determined this to be
inaccurate; some actual first-party requests do not include the
"origin" header. I am not sure exactly why this is the case, but I
have changed the check to use "user-agent" instead. This is another
indicator that a request likely did not come from a programmatic
consumer.
  • Loading branch information
jportner committed Dec 12, 2020
1 parent 7fcaf5f commit 4d9d7f8
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('CoreUsageStatsClient', () => {
);
return { usageStatsClient, debugLoggerMock, basePathMock, repositoryMock };
};
const firstPartyRequestHeaders = { 'kbn-version': 'a', origin: 'b', referer: 'c' }; // as long as these three header fields are truthy, this will be treated like a first-party request
const firstPartyRequestHeaders = { 'kbn-version': 'a', 'user-agent': 'b', referer: 'c' }; // as long as these three header fields are truthy, this will be treated like a first-party request
const incrementOptions = { refresh: false };

describe('#getUsageStats', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/core_usage_data/core_usage_stats_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,5 +224,5 @@ function getAllCommonFields(prefix: string) {
function getIsKibanaRequest({ headers }: KibanaRequest) {
// The presence of these three request headers gives us a good indication that this is a first-party request from the Kibana client.
// We can't be 100% certain, but this is a reasonable attempt.
return headers && headers['kbn-version'] && headers.origin && headers.referer;
return headers && headers['kbn-version'] && headers['user-agent'] && headers.referer;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('UsageStatsClient', () => {
return { usageStatsClient, debugLoggerMock, repositoryMock };
};

const firstPartyRequestHeaders = { 'kbn-version': 'a', origin: 'b', referer: 'c' }; // as long as these three header fields are truthy, this will be treated like a first-party request
const firstPartyRequestHeaders = { 'kbn-version': 'a', 'user-agent': 'b', referer: 'c' }; // as long as these three header fields are truthy, this will be treated like a first-party request
const incrementOptions = { refresh: false };

describe('#getUsageStats', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,5 @@ export class UsageStatsClient {
function getIsKibanaRequest(headers?: Headers) {
// The presence of these three request headers gives us a good indication that this is a first-party request from the Kibana client.
// We can't be 100% certain, but this is a reasonable attempt.
return headers && headers['kbn-version'] && headers.origin && headers.referer;
return headers && headers['kbn-version'] && headers['user-agent'] && headers.referer;
}

0 comments on commit 4d9d7f8

Please sign in to comment.