[CSHARP-1155] GridFS: "metadata" can be set to a value which causes an exception when the file is subsequently queried (or handled at all) Created: 23/Dec/14  Updated: 02/Mar/17  Resolved: 02/Mar/17

Status: Closed
Project: C# Driver
Component/s: GridFS
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Andrew Ryder (Inactive) Assignee: Robert Stam
Resolution: Won't Fix Votes: 0
Labels: cs
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related

 Description   

using( var f = db.GridFS.Create( "test.txt" ) )
{
	var bytes = System.Text.Encoding.ASCII.GetBytes( "content content content" );
	f.Write( bytes, 0, bytes.Length );
}
 
db.GridFS.SetMetadata( db.GridFS.FindOne( "test.txt" ), "metadata metadata metadata" );
 
var info = db.GridFS.FindOne( "test.txt" );

The above code when run the first time will give an exception at the last line. As:

System.InvalidCastException: Unable to cast object of type 'MongoDB.Bson.BsonString' to type 'MongoDB.Bson.BsonDocument'.

The exception is generated internally by the C# driver when it assumes the 'metadata' field as a BsonDocument despite allowing us to set it to something else only a couple of lines above.

In other words, the C# driver can create a GridFS entry that it can't load.

All GridFS functions on this file entry are wedged. You cannot edit it, you cannot replace, update, or even delete it.

The fatal trigger is SetMetadata, although using another driver to do the same operation is a viable alternative. Ultimately, inside the C# driver the presence of the "metadata" field causes an unstoppable type cast to occur when doing any operation with the tainted file entry - even just trying to delete it is fatal!

For example, the following call now gives the same exception:

db.GridFS.Delete( "test.txt" );

Knowing the file ID does not help, the following also causes the same exception:

db.GridFS.DeleteById( new ObjectId( "5498dfa9f8efe9309c99e9d3" ) );

The offending line of code for v1.9.2 is here:
https://github.com/mongodb/mongo-csharp-driver/blob/v1.9.2/MongoDB.Driver/GridFS/MongoGridFSFileInfo.cs#L759



 Comments   
Comment by Robert Stam [ 02/Mar/17 ]

Since this only applies to the Legacy API and the Legacy API is no longer under active development this issue will not be fixed.

The 2.x GridFS API does not have this issue.

Comment by Robert Stam [ 22/Dec/16 ]

The GridFS spec requires that the metadata be a BsonDocument.

SetMetadata should not have allowed the metadata to be set to anything other than a BsonDocument.

In a future release we will add a SetMetadataDocument method that takes a BsonDocument parameter and deprecate the current SetMetadata method.

Note also that this is the Legacy API. The new 2.x GridFS API does not have this issue.

Generated at Wed Feb 07 21:38:49 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.