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

Move filename validation into adaptor for AWS "directories". Add preserveFileName Support #76

Merged
merged 10 commits into from
Dec 11, 2019

Conversation

mpatnode
Copy link
Contributor

@mpatnode mpatnode commented Oct 26, 2019

Sorry for mixing the two features here, but they were interdependent. The first was to implement file validation for AWS files. This validation method will be ignored until parse-community/parse-server#6157 is merged.

The end result here is that now you can specify an AWS "directory" path in your filename and it won't be rejected by a Router class that doesn't understand your filesystem. You can now supply methods for filename validation and AWS key generation, so you can decide what's a valid filename, and how you want to deal with duplicate filenames. Note the default behavior for the later needs to be disabled in parse-server:FilesController if you want to use directories in AWS.

index.js Outdated
@@ -2,6 +2,8 @@
//
// Stores Parse files in AWS S3.

const FilesAdapter = require('parse-server/lib/Adapters/Files/FilesAdapter').FilesAdapter;
const Parse = require('parse/lib/node/Parse').Parse;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I needed to bring in Parse to throw the right error codes.

@codecov
Copy link

codecov bot commented Oct 26, 2019

Codecov Report

Merging #76 into master will increase coverage by 0.27%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #76      +/-   ##
==========================================
+ Coverage   94.26%   94.54%   +0.27%     
==========================================
  Files           2        2              
  Lines         157      165       +8     
  Branches       33       34       +1     
==========================================
+ Hits          148      156       +8     
  Misses          9        9
Impacted Files Coverage Δ
lib/optionsFromArguments.js 96% <100%> (+0.22%) ⬆️
index.js 93.33% <100%> (+0.31%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fa3f3a5...bb38423. Read the comment docs.

index.js Outdated
@@ -11,11 +13,12 @@ const awsCredentialsDeprecationNotice = function awsCredentialsDeprecationNotice
'See: https://github.com/parse-server-modules/parse-server-s3-adapter#aws-credentials for details');
};

class S3Adapter {
class S3Adapter extends FilesAdapter {
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 seemed like the right thing to do, even though the parse-server PR doesn't actually change the signature.

}, 100),
deleteObject: (params, callback) => setTimeout(() => {
const { Key } = params;
function makeS3Adaptor(options) {
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 code came in handy for more createFile requests.

@dplewis
Copy link
Member

dplewis commented Oct 26, 2019

@acinader What do you think? This is similar to #16. I think it fits the suggestions.

Copy link
Member

@dplewis dplewis left a comment

Choose a reason for hiding this comment

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

@yomybaby Can you test this and provide feedback? I think this should solve your issue.

package.json Outdated Show resolved Hide resolved
spec/test.spec.js Outdated Show resolved Hide resolved
@yomybaby
Copy link

@dplewis Ok. I'll take a look at it tonight.

README.md Outdated
@@ -169,6 +176,8 @@ var s3Options = {
baseUrl: process.env.SPACES_BASE_URL,
region: process.env.SPACES_REGION,
directAccess: true,
preserveFilename: "always",
fileNameCheck: "safe",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FWIW: I'm only using these settings in my app. Happy to remove the others if they are at controversial.

@yomybaby
Copy link

Move filename validation out of the Router and into the FilesAdaptor(parse-community/parse-server#6157) is great idea.

How about make this more customizable?
There is a two options instead of preserveFilename and fileNameCheck.
There are function.

like this

new S3Adapter({
  bucket: "my_bucket",
  generateKey: (filename) => {
    return 'origin/'+filename;
  },
  validateFilename: (filename) => {
    if (filename.length > 1024) {
       return 'Filename too long.';
     }
     return null;
  }
})

@mpatnode
Copy link
Contributor Author

Thought about it, but use my case was fairly simple. If it's needed in the future, the validation code could simply check the type of the validate/preserveFilename parameter, and do the right thing. I wanted to preserve the same filename check behavior from the old code, while enabling the directory capability. Happy to remove the preserveFilename feature until someone needs it. I'm not using it.

@mpatnode
Copy link
Contributor Author

mpatnode commented Nov 3, 2019

How about make this more customizable?
There is a two options instead of preserveFilename and fileNameCheck.
There are function.

So don't know when I'll have time to get back to this. @yomybaby Any interest in implementing that on top of my branch?

@dplewis
Copy link
Member

dplewis commented Nov 20, 2019

@mpatnode Sorry for the late reply. Adding parse module (not parse-server) for Parse.Error should be ok here as we use it in the parse-server-push-adapter

https://github.com/parse-community/parse-server-push-adapter/blob/master/package.json#L39

@mpatnode
Copy link
Contributor Author

mpatnode commented Nov 27, 2019

@mpatnode Sorry for the late reply. Adding parse module (not parse-server) for Parse.Error should be ok here as we use it in the parse-server-push-adapter

Ignore previous comment. Will do!

@mpatnode mpatnode force-pushed the validate-filename branch 2 times, most recently from 43738f4 to 1ec0b10 Compare November 27, 2019 21:16
@@ -90,6 +92,8 @@ const optionsFromArguments = function optionsFromArguments(args) {
options = fromEnvironmentOrDefault(options, 'baseUrlDirect', 'S3_BASE_URL_DIRECT', false);
options = fromEnvironmentOrDefault(options, 'signatureVersion', 'S3_SIGNATURE_VERSION', 'v4');
options = fromEnvironmentOrDefault(options, 'globalCacheControl', 'S3_GLOBAL_CACHE_CONTROL', null);
options = fromOptionsDictionaryOrDefault(options, 'generateKey', null);
options = fromOptionsDictionaryOrDefault(options, 'validateFilename', null);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

JS code in env vars didn't seem like a good idea to me...

},
generateKey: (filename) => {
return `${Date.now()}_${filename}`; // unique prefix for every filename
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@yomybaby This look good to you?

package.json Show resolved Hide resolved
README.md Outdated
validateFilename: (filename) => {
if (filename.length > 1024) {
return new Parse.Error(
Parse.Error.INVALID_FILE_NAME,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dplewis Hmm... this might be problematic?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No longer necessary: parse-community/parse-server#6246

Copy link
Member

Choose a reason for hiding this comment

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

You should throw new Parse.Error instead of returning

Copy link
Member

Choose a reason for hiding this comment

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

Actually ignore that last comment this is correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dplewis @acinader So are we good to go here?

@mpatnode
Copy link
Contributor Author

mpatnode commented Dec 3, 2019

@dplewis This is happy now: parse-community/parse-server#6246

@acinader acinader requested a review from dplewis December 10, 2019 22:46
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.

3 participants