Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove from URLs query parameters that might have secrets #195

Merged
merged 6 commits into from
Jul 17, 2023

Conversation

yihezkel
Copy link
Member

Security

Remove query parameters from URL when they might have secrets and might be traced (even if part of error flow)

@github-actions
Copy link

github-actions bot commented Jun 15, 2023

Test Results

457 tests  ±0   457 ✔️ ±0   14m 50s ⏱️ + 6m 40s
  25 suites ±0       0 💤 ±0 
    1 files   ±0       0 ±0 

Results for commit 861268b. ± Comparison against base commit 376bdb3.

♻️ This comment has been updated with latest results.

Copy link
Contributor

@AsafMah AsafMah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove the failed test, testing against the exact JSON output is bad anyway, and not related to your change.

func InferFormatFromFileName(fName string) DataFormat {
return properties.DataFormatDiscovery(fName)
return properties.DataFormatDiscovery(properties.RemoveQueryParamsFromUrl(fName))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come this is relevant here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every place I did this, it's a place where:

  1. Secrets are unnecessary for the called function to work.
  2. The function (perhaps transitively) can throw an error when doing something with the URL. I'm less familiar with Go's error handling, but my concern is that the exception may include the parameter.

@@ -139,7 +139,7 @@ func (i *Ingestion) FromFile(ctx context.Context, fPath string, options ...FileO

// fromFile is an internal function to allow managed streaming to pass a properties object to the ingestion.
func (i *Ingestion) fromFile(ctx context.Context, fPath string, options []FileOption, props properties.All) (*Result, error) {
local, err := queued.IsLocalPath(fPath)
local, err := queued.IsLocalPath(properties.RemoveQueryParamsFromUrl(fPath))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come this is relevant here?

@@ -219,7 +219,7 @@ func (i *Ingestion) Blob(ctx context.Context, from string, fileSize int64, props

props.Ingestion.RetainBlobOnSuccess = !props.Source.DeleteLocalSource

err = CompleteFormatFromFileName(&props, from)
err = CompleteFormatFromFileName(&props, properties.RemoveQueryParamsFromUrl(from))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do this only where we return an error or an object where the full URI is not needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, I'm no Go expert, so I can look into whether we can remove the unnecessary params just when throwing the error.

But in each instance, the secrets are unnecessary to the function of the function. And perhaps from a "least privilege" standpoint, it's best not to pass secrets when not needed, notably in case a new trace/error/etc will be added in the future.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the right way to enforce this.
If you want to do something covering all possible paths, create a SecretiveUri like structure and have String() and SecureString() differ on the output.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not 100% sure

I get the idea, and type safety is good, but I wonder if it can even be done correctly without infecting the whole code and having breaking changes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, I'm going to reduce the scope of this to only the explicit prints.

Afterwards, when I have time, I'll pass an object that contains both a secretive and a sanitized URL, and pass the latter to the methods that don't require the secret.

@@ -416,6 +416,13 @@ func (i Ingestion) validate() error {
return nil
}

func RemoveQueryParamsFromUrl(url string) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might be easier to use if the API was func (s string) RemoveQueryParamsFromUrl() string
calling would simply be s.RemoveQueryParamsFromUrl()

Copy link
Member Author

@yihezkel yihezkel Jun 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm definitely not an expert in Go, but I tried that (it was my preferred approach as well), and it didn't work. IIRC the issue was that we're not allowed to add a function on the string class.

IIRC we do have a String class, and I was able to add this method to there, but I believe it would've required more extensive changes to pass that type of parameter instead of the existing string.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yihezkel is right, you can't monkeypatch classes on go (you can't do anything fun on go).
The capital String class is for the string type in kusto (parsing them, escaping them, etc), and should not be used for general purpose.

@@ -59,7 +59,7 @@ func (i *Streaming) FromFile(ctx context.Context, fPath string, options ...FileO
}

func prepFileAndProps(fPath string, props *properties.All, options []FileOption, client ClientScope) (*os.File, error) {
local, err := queued.IsLocalPath(fPath)
local, err := queued.IsLocalPath(properties.RemoveQueryParamsFromUrl(fPath))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

@@ -82,7 +82,7 @@ func prepFileAndProps(fPath string, props *properties.All, options []FileOption,
props.Source.DontCompress = true
}

err = queued.CompleteFormatFromFileName(props, fPath)
err = queued.CompleteFormatFromFileName(props, properties.RemoveQueryParamsFromUrl(fPath))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

@yihezkel yihezkel requested a review from yogilad July 2, 2023 08:48
@@ -416,6 +416,13 @@ func (i Ingestion) validate() error {
return nil
}

func RemoveQueryParamsFromUrl(url string) string {
if idx := strings.Index(url, "?"); idx != -1 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add also
strings.Index(url, ";")

@codecov
Copy link

codecov bot commented Jul 13, 2023

Codecov Report

Merging #195 (861268b) into master (376bdb3) will not change coverage.
The diff coverage is 25.00%.

@@           Coverage Diff           @@
##           master     #195   +/-   ##
=======================================
  Coverage   49.59%   49.59%           
=======================================
  Files          53       53           
  Lines        5835     5835           
=======================================
  Hits         2894     2894           
  Misses       2733     2733           
  Partials      208      208           
Impacted Files Coverage Δ
kusto/ingest/file_options.go 47.48% <ø> (ø)
kusto/ingest/status.go 17.16% <0.00%> (ø)
kusto/ingest/internal/queued/queued.go 22.82% <100.00%> (ø)

@AsafMah AsafMah self-requested a review July 16, 2023 06:35
@yihezkel yihezkel merged commit 4c3a9c6 into master Jul 17, 2023
8 checks passed
@yihezkel yihezkel deleted the removeQueryParamsWithPossibleSecretsFromUrls branch July 17, 2023 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants