Skip to content

Commit

Permalink
fix(ingest): sanitise token (#26918)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverb123 authored Dec 15, 2024
1 parent e1d346e commit 185082b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { PluginEvent } from '@posthog/plugin-scaffold'

import { eventDroppedCounter } from '../../../main/ingestion-queues/metrics'
import { PipelineEvent } from '../../../types'
import { sanitizeString } from '../../../utils/db/utils'
import { UUID } from '../../../utils/utils'
import { captureIngestionWarning } from '../utils'
import { tokenOrTeamPresentCounter } from './metrics'
Expand Down Expand Up @@ -42,6 +43,9 @@ export async function populateTeamDataStep(
} else if (event.team_id) {
team = await runner.hub.teamManager.fetchTeam(event.team_id)
} else if (event.token) {
// HACK: we've had null bytes end up in the token in the ingest pipeline before, for some reason. We should try to
// prevent this generally, but if it happens, we should at least simply fail to lookup the team, rather than crashing
event.token = sanitizeString(event.token)
team = await runner.hub.teamManager.getTeamByToken(event.token)
}

Expand Down
15 changes: 15 additions & 0 deletions rust/capture/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub enum InvalidTokenReason {
TooLong,
NotAscii,
PersonalApiKey,
NullByte,
}

impl InvalidTokenReason {
Expand All @@ -22,6 +23,7 @@ impl InvalidTokenReason {
// Self::IsNotString => "not_string",
Self::TooLong => "too_long",
Self::PersonalApiKey => "personal_api_key",
Self::NullByte => "null_byte",
}
}
}
Expand Down Expand Up @@ -57,6 +59,11 @@ pub fn validate_token(token: &str) -> Result<(), InvalidTokenReason> {
return Err(InvalidTokenReason::PersonalApiKey);
}

// We refuse tokens with null bytes
if token.contains('\0') {
return Err(InvalidTokenReason::NullByte);
}

Ok(())
}

Expand Down Expand Up @@ -96,4 +103,12 @@ mod tests {
assert!(valid.is_err());
assert_eq!(valid.unwrap_err(), InvalidTokenReason::PersonalApiKey);
}

#[test]
fn blocks_null_byte() {
let valid = validate_token("hello\0there");

assert!(valid.is_err());
assert_eq!(valid.unwrap_err(), InvalidTokenReason::NullByte);
}
}

0 comments on commit 185082b

Please sign in to comment.