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

fs: do basic arg validation of truncate in js #2498

Closed

Conversation

thefourtheye
Copy link
Contributor

This patch moves the basic validation of arguments to truncate family
of functions to the JavaScript from the C++ layer.

cc @bnoordhuis

CI Run: https://jenkins-iojs.nodesource.com/job/node-test-pull-request/131/

@thefourtheye thefourtheye added the fs Issues and PRs related to the fs subsystem / file system. label Aug 22, 2015
len = 0;
} else if (typeof len !== 'number' || !isFinite(len)) {
Copy link
Member

Choose a reason for hiding this comment

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

I think Number.isFinite does both checks in one call

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@targos Ah, you are correct. I updated now. PTAL.

@mscdex
Copy link
Contributor

mscdex commented Aug 22, 2015

Out of curiousity, what was the motivation behind these changes?

@thefourtheye
Copy link
Contributor Author

len = 0;
} else if (!Number.isFinite(len) || len < 0) {
throw new TypeError('length must be a positive integer');
Copy link
Member

Choose a reason for hiding this comment

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

Just seeing the error message. If you want to ensure it is an integer, you can drop Number.isFinite and use Number.isInteger instead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@targos Man, that's wonderful. I didn't know about that function and that is exactly what we need here. Thanks :-) Updated the PR.

@thefourtheye
Copy link
Contributor Author

@bnoordhuis Ping!

@@ -688,14 +688,19 @@ fs.renameSync = function(oldPath, newPath) {
};

fs.truncate = function(path, len, callback) {
if (typeof path === 'number') {
if (Number.isInteger(path) && path >= 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Change in functionality. Here's an approximate test:

if (typeof path === 'number' && !Number.isInteger(path))
  throw new TypeError('Not an integer');

Copy link
Contributor

Choose a reason for hiding this comment

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

or are you just short-circuiting for the fast path?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a legacy functionality it seems. If we pass an fd to truncate it internally delegates it to ftruncate. So we cannot throw error here. We are just checking if it is a positive integer and move on to the next check if is not.

Are you suggesting to not to call ftruncate at all?

Copy link
Contributor

Choose a reason for hiding this comment

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

I just followed the logic paths for current and this patch, and there seemed to be a discrepancy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@trevnorris Agree, but wouldn't this check be better?

Copy link
Contributor

Choose a reason for hiding this comment

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

My assumption was to land most these changes on a minor, then making any breaking changes, that push this to semver-major, to another PR. If you're cool with this staying on major then I'll have another look from that perspective.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@trevnorris I am okay with this being in major :-) Let's get this done right. Please suggest improvements as well :-)

@thefourtheye
Copy link
Contributor Author

Bump!

!IsInt64(len_v->NumberValue())) {
return env->ThrowTypeError("Not an integer");
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Change this and the below line to:

CHECK(args[1]->IsNumber());
const int64_t = args[1]->IntegerValue();

Reason for the CHECK() is because we're now making an assumption about the incoming value. Always want to catch that if incorrect, because at this point if the value is incorrect then we've screwed up.

Copy link
Contributor

Choose a reason for hiding this comment

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

wait. nm. read this as the fd value, not the len value.

EDIT: meaning, no need for the CHECK(). can still collapse the other.

@trevnorris
Copy link
Contributor

to recap

  • Change all fd conversion in native from Int32Value() to Uint32Value().
  • Change all fd checks in JS to check if valid uint32
    • Will probably need to do a fd = +fd at the top of the function for string coercion. This is simply so we can have a solid native CHECK() to make sure we didn't screw up.
  • Make sure there is a CHECK(val->IsUint32()) just before doing Uint32Value() to all fd's.

@Fishrock123
Copy link
Contributor

ping @thefourtheye

@jasnell jasnell added the stalled Issues and PRs that are stalled. label Nov 16, 2015
@targos
Copy link
Member

targos commented Jan 31, 2016

ping @thefourtheye ;)

@thefourtheye
Copy link
Contributor Author

@targos Ah, I have this open for a loooong time. I'll get to it this week. Thanks for reminding :-) Last week @benjamingr also reminded me of this.

@thefourtheye thefourtheye force-pushed the fs-improve-validation branch 2 times, most recently from 000eee8 to 8233f9a Compare February 12, 2016 19:11
@thefourtheye
Copy link
Contributor Author

@trevnorris Sorry for the delay in getting your review comments addressed. PTAL.

@jasnell jasnell added the semver-major PRs that contain breaking changes and should be released in the next major version. label Feb 13, 2016
@jasnell
Copy link
Member

jasnell commented Feb 13, 2016

Added semver-major tag based on the discussion thread. Feel free to change that if it turns out not to be major after all.

function sanitizeFD(inputFD) {
const fd = +inputFD;
if (!Number.isInteger(fd) || fd < 0 || fd > 0xFFFFFFFF) {
throw new Error('file descriptor must be a positive 32-bit integer');
Copy link
Contributor

Choose a reason for hiding this comment

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

Change this to a TypeError. Also I'd replace "positive" with "unsigned".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@thefourtheye thefourtheye force-pushed the fs-improve-validation branch 2 times, most recently from 6036668 to 3aab026 Compare March 14, 2016 16:39
@thefourtheye
Copy link
Contributor Author

@trevnorris Changed positive to unsigned now.

@thefourtheye
Copy link
Contributor Author

Included another commit now, which just doesn't coerce the fds.

@trevnorris
Copy link
Contributor

Thanks for the change, and test. LGTM.

@thefourtheye thefourtheye force-pushed the fs-improve-validation branch 2 times, most recently from 7a953f9 to f19c403 Compare July 21, 2016 04:30
@thefourtheye
Copy link
Contributor Author

Rebased and improved the isFD function. CI Run: https://ci.nodejs.org/job/node-test-pull-request/3365/

This patch moves the basic validation of arguments to `truncate` family
of functions to the JavaScript from the C++ layer.
@thefourtheye
Copy link
Contributor Author

Rebased, after landing #7168. CI Run: https://ci.nodejs.org/job/node-test-pull-request/3378/

@thefourtheye
Copy link
Contributor Author

Landed in c86c1ee.

@thefourtheye thefourtheye deleted the fs-improve-validation branch July 21, 2016 22:32
thefourtheye added a commit that referenced this pull request Jul 21, 2016
This patch

 1. moves the basic validation of arguments to `truncate` family
    of functions to the JavaScript layer from the C++ layer.

 2. makes sure that the File Descriptors are validated strictly.

PR-URL: #2498
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
MylesBorins pushed a commit to MylesBorins/node that referenced this pull request Jul 22, 2016
This reverts commit c86c1ee.

original commit message:

    This patch

     1. moves the basic validation of arguments to `truncate` family
        of functions to the JavaScript layer from the C++ layer.

     2. makes sure that the File Descriptors are validated strictly.

    PR-URL: nodejs#2498
    Reviewed-By: Trevor Norris <trev.norris@gmail.com>
@Fishrock123 Fishrock123 removed the stalled Issues and PRs that are stalled. label Jul 26, 2016
MylesBorins pushed a commit to MylesBorins/node that referenced this pull request Aug 4, 2016
This reverts commit c86c1ee.

original commit message:

    This patch

     1. moves the basic validation of arguments to `truncate` family
        of functions to the JavaScript layer from the C++ layer.

     2. makes sure that the File Descriptors are validated strictly.

    PR-URL: nodejs#2498
    Reviewed-By: Trevor Norris <trev.norris@gmail.com>

PR-URL: nodejs#7950
Reviewed-By: Julien Gilli <jgilli@nodejs.org>
Reviewed-By: Rod Vagg <rod@vagg.org>
Reviewed-By: Minwoo Jung <jmwsoft@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@gibfahn gibfahn mentioned this pull request Jun 15, 2017
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fs Issues and PRs related to the fs subsystem / file system. semver-major PRs that contain breaking changes and should be released in the next major version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants