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

richard/imageHash #89

Merged
merged 13 commits into from
Sep 20, 2021
Merged

richard/imageHash #89

merged 13 commits into from
Sep 20, 2021

Conversation

chomosuke
Copy link
Owner

@chomosuke chomosuke commented Sep 15, 2021

speed baby.

Thanks to a certain person's good coding style that he went on to forget about during the meeting.

closes #76.

pre-apologize for any mistake I might have made. pls check.

@chomosuke chomosuke added the enhancement New feature or request label Sep 15, 2021
@chomosuke chomosuke added this to the Sprint 2 milestone Sep 15, 2021
@chomosuke chomosuke self-assigned this Sep 15, 2021
@chomosuke chomosuke added this to In progress in Sprint 2 via automation Sep 15, 2021
@chomosuke chomosuke added this to Sprint 2 in Product backlog via automation Sep 15, 2021
@chomosuke chomosuke changed the title Richard/image hash richard/imageHash Sep 15, 2021
Copy link
Collaborator

@waltervan00 waltervan00 left a comment

Choose a reason for hiding this comment

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

lgtm

@shangzhel
Copy link
Collaborator

shangzhel commented Sep 16, 2021

When I said "use an image hash" I meant that the image URL becomes /api/image/<hash>. The card ID should no longer be used because in the event that more than one card uses the same image, the image hash will be identical, and a single fetch by the browser can cover all cases with its cache.

@chomosuke
Copy link
Owner Author

chomosuke commented Sep 16, 2021

But this does work though? with less changes for the API?

For api/image/hash, we'd have to search the entire card collection, a lot more work for the server in my opinion.

Alternatively image will no longer be associated with card, so they'll have their own collections. And imageHash should probably be replaced by image id. This does mean we no longer search the entire collection for every fetch, but every upload the server will check the hash against every single image in the database. Unsure if this is worth the improvement in UI.

@waltervan00
Copy link
Collaborator

I think we should keep the image to card association.
In regards to the api accepting the hash of an image, we could do a query on cards for the hash image and return if found. This could lead to problems regarding multiple cards having the same hash value (a collision may be unlikely but I wouldn't leave it to chance), and in those cases the incorrect image may be retrieved. For this reason I think it would be fine keep the CardId field in the query.

The GET /image endpoint also needs to check the authorisation of whether the user owns the card the image belongs to. This can be done within the transaction code, however it still means that we need to fetch the card record the imagehash falls under.

Currently the improvement is front-end centric and the goal is to ensure that the Image component makes the calls whenever an image is changed and a new url is loaded into the component.

@chomosuke
Copy link
Owner Author

chomosuke commented Sep 16, 2021

I agree with walter.

I'll list the (what i think is the) pros and cons here:

We do what I did here: pretty much nothing changes, hash is only used by the front-end to get browser to behave.

We do /api/image/hash: two options:
options no 1, nothing change on the server except GET image, get image will now scan through every card to find the user/image combination this can potentially be accomplished quickly by creating an index of card with imageHash.

options no 2, image unassociate with cards. Every card will have an imageId with it. Sever scan for identical hash on every upload to save space (or alteratively not, but in that case you might as well keep the association). Server will also still have to check if user can access image, this means image will have a list of user's foreign keys.

@shangzhel
Copy link
Collaborator

This could lead to problems regarding multiple cards having the same hash value (a collision may be unlikely but I wouldn't leave it to chance), and in those cases the incorrect image may be retrieved.

You are correct, and the MD5 hash is susceptible to this. Use the createHash('sha256') function from the crypto module to create a Hash object with the SHA-256 hash algorithm. If you are worried about hash collisions with SHA-256, NIST calls it secure in the FIPS 180-4 Secure Hash Standard.

@shangzhel
Copy link
Collaborator

We do /api/image/hash: two options:

Neither options are good. There is one easy way to do it. Have every card store its image and the imageHash, and in GET /api/image call await cards.findOne({'user':req.user._id,'imageHash':hash}). If the promise resolves with null, then no such combination exists (either the card belongs to another user, or no card with the hash exists at all, we don't care about why either way) and the request should fail with 404 Not Found. Otherwise such a card image is found, and it is sent as the response.

@chomosuke
Copy link
Owner Author

So basically the first option?

Sprint 2 automation moved this from In progress to In review Sep 16, 2021
Copy link
Collaborator

@shangzhel shangzhel left a comment

Choose a reason for hiding this comment

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

Almost done

server/src/api/authenticated/image.ts Outdated Show resolved Hide resolved
server/src/api/authenticated/image.ts Outdated Show resolved Hide resolved
web/src/__tests__/controllers/Card.ts Outdated Show resolved Hide resolved
Copy link
Collaborator

@shangzhel shangzhel left a comment

Choose a reason for hiding this comment

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

One minor change, then it should be good.

web/src/controllers/Card.ts Outdated Show resolved Hide resolved
Copy link
Collaborator

@shangzhel shangzhel left a comment

Choose a reason for hiding this comment

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

Looks good to me

@chomosuke chomosuke merged commit 62d1157 into master Sep 20, 2021
Sprint 2 automation moved this from In review to Done Sep 20, 2021
Product backlog automation moved this from Sprint 2 to Sprint 2 Done Sep 20, 2021
@chomosuke chomosuke deleted the richard/imageHash branch September 20, 2021 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Product backlog
  
Sprint 2 Done
Sprint 2
  
Done
Development

Successfully merging this pull request may close these issues.

Change API to send hash instead of hasImage
3 participants