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

Extended image checkings #15

Merged
merged 4 commits into from Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## 1.4.0
* ADD: isFileImage function was added for checking of files content-type
* UPD: checkIsImageUrlAllowable function was renamed to isImageUrlAllowed and checks was extended

## 1.3.0
* ADD: new module validationHelper and function checkIsImageUrlAllowable for checking of images URLs

Expand Down
36 changes: 36 additions & 0 deletions karma.conf.js
Expand Up @@ -63,6 +63,42 @@ module.exports = (config) => {
pattern: srcOriginalRecursivePath,
included: false,
served: true
},
{
pattern: 'test/images/*.png',
Copy link
Contributor

Choose a reason for hiding this comment

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

Pattern allows us to use a regexp instead of specifying each image type independently.
Please write a RegExp that covers all of types that you want to using with unit tests.

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 tried to use something like (jpg|gif|png|svg), but it doesn't work. I will try regexp expression.

watched: false,
included: false,
served: true
},
{
pattern: 'test/images/*.jpg',
watched: false,
included: false,
served: true
},
{
pattern: 'test/images/*.gif',
watched: false,
included: false,
served: true
},
{
pattern: 'test/images/*.svg',
watched: false,
included: false,
served: true
},
{
pattern: 'test/images/*.bmp',
watched: false,
included: false,
served: true
},
{
pattern: 'test/data/*.txt',
watched: false,
included: false,
served: true
}
],
preprocessors: {
Expand Down
3 changes: 2 additions & 1 deletion lib/index.d.ts
Expand Up @@ -62,7 +62,8 @@ declare module powerbi.extensibility.utils.dataview {
}
declare module powerbi.extensibility.utils.dataview {
module validationHelper {
function checkIsImageUrlAllowable(url: string): boolean;
function isImageUrlAllowed(url: string): boolean;
function isFileImage(url: string, imageCheckResultCallBack: (isImage: boolean, contentType: string) => void): void;
}
}
declare module powerbi.extensibility.utils.dataview {
Expand Down
26 changes: 21 additions & 5 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "powerbi-visuals-utils-dataviewutils",
"version": "1.3.0",
"version": "1.4.0",
"description": "dataviewutils",
"main": "lib/index.js",
"repository": {
Expand Down
28 changes: 24 additions & 4 deletions src/validationHelper.ts
Expand Up @@ -26,10 +26,30 @@

module powerbi.extensibility.utils.dataview {
export module validationHelper {
export function checkIsImageUrlAllowable(url: string): boolean {
// Excludes all URLs that don't contain .gif .jpg .png or .svg extensions.
// Also excludes directives "javascript:" and "data:".
return (/\.(gif|jpg|png|svg)$/i).test(url) && !(/(javascript:|data:)/i).test(url);
export function isImageUrlAllowed(url: string): boolean {
// Excludes all URLs that don't contain .gif .jpg .png or .svg extensions and don't start from "http(s)://".
// As a result -- also excludes all directives such as "javascript:", "data:" and "blob:".
return (/^https?:\/\/.+\.(gif|jpg|png|svg)$/i).test(url);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering why this RegExp excludes base64 encoded images?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we really need base64 images?

}

export function isFileImage(url: string, imageCheckResultCallBack: (isImage: boolean, contentType: string) => void) {
let request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState !== this.HEADERS_RECEIVED) {
return;
}

let contentType = request.getResponseHeader("Content-Type"),
supportedTypes = ["image/png", "image/jpeg", "image/gif", "image/svg+xml"];

if (supportedTypes.indexOf(contentType) > -1) {
return imageCheckResultCallBack(true, contentType);
Copy link
Contributor

Choose a reason for hiding this comment

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

I suppose that it'd be nice to return image data as the third argument of callback.
What is your point of view?

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 don't see how this info may be useful, especially if we use HEAD request

Copy link
Contributor

@uve uve Nov 13, 2017

Choose a reason for hiding this comment

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

It doesn't make a sense

}

return imageCheckResultCallBack(false, contentType);
};
request.open("HEAD", url, true);
request.send();
}
}
}
1 change: 1 addition & 0 deletions test/data/someplaintext.txt
@@ -0,0 +1 @@
Plain text with some data taht is used in unit tests
Copy link
Contributor

Choose a reason for hiding this comment

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

Spelling mistake: that

Binary file added test/images/access.bmp
Binary file not shown.
Binary file added test/images/access.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/images/access.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/images/access.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions test/images/sun.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
96 changes: 81 additions & 15 deletions test/validationHelperTest.ts
Expand Up @@ -28,31 +28,97 @@ module powerbi.extensibility.utils.dataview.test {
import validationHelper = powerbi.extensibility.utils.dataview.validationHelper;

describe("validationHelper", () => {
it("valid URLs supported extensions", () => {
expect(validationHelper.checkIsImageUrlAllowable("https://someHost/someTestImage.PnG")).toBe(true);
expect(validationHelper.checkIsImageUrlAllowable("https://someHost/someTestImage.jPG")).toBe(true);
expect(validationHelper.checkIsImageUrlAllowable("https://someHost/someTestImage.GIf")).toBe(true);
expect(validationHelper.checkIsImageUrlAllowable("https://someHost/someTestImage.SVG")).toBe(true);
it("valid URLs supported protocols and extensions", () => {
expect(validationHelper.isImageUrlAllowed("https://someHost/someTestImage.PnG")).toBe(true);
expect(validationHelper.isImageUrlAllowed("https://someHost/someTestImage.jPG")).toBe(true);
expect(validationHelper.isImageUrlAllowed("https://someHost/someTestImage.GIf")).toBe(true);
expect(validationHelper.isImageUrlAllowed("https://someHost/someTestImage.SVG")).toBe(true);
});

it("valid URLs ports are supported", () => {
expect(validationHelper.isImageUrlAllowed("https://someHost:7777/someTestImage.SVG")).toBe(true);
});

it("invalid URL wrong extension", () => {
expect(validationHelper.checkIsImageUrlAllowable("https://someHostsomeTestImage.exe")).toBe(false);
expect(validationHelper.isImageUrlAllowed("https://someHostsomeTestImage.exe")).toBe(false);
});

it("invalid URL no extension", () => {
expect(validationHelper.checkIsImageUrlAllowable("https://someHostsomeGeneratedImage")).toBe(false);
expect(validationHelper.isImageUrlAllowed("https://someHostsomeGeneratedImage")).toBe(false);
});

it("invalid URL unsupported protocols", () => {
expect(validationHelper.isImageUrlAllowed("ftp://someHostsomeGeneratedImage.jpg")).toBe(false);
expect(validationHelper.isImageUrlAllowed("jAvAscrIpt:alert('XSS');")).toBe(false);
expect(validationHelper.isImageUrlAllowed("jAvAscrIpt:alert('XSS');.png")).toBe(false);
expect(validationHelper.isImageUrlAllowed("jaascript:alert('XSS');.png")).toBe(false);
expect(validationHelper.isImageUrlAllowed("data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD")).toBe(false);
expect(validationHelper.isImageUrlAllowed("data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD.png")).toBe(false);
expect(validationHelper.isImageUrlAllowed("blob:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD.png")).toBe(false);
});

it("invalid URL no protocol", () => {
expect(validationHelper.isImageUrlAllowed("someHostsomeGeneratedImage.jpg")).toBe(false);
});

it("invalid URL using of parameters", () => {
expect(validationHelper.isImageUrlAllowed("https://someHostsomeGeneratedImage.jpg?param1=val1&param2=val2")).toBe(false);
});

it("check file is PNG image", (done) => {
validationHelper.isFileImage("/base/test/images/access.png", (isImage, contentType) => {
expect(isImage).toBe(true);
expect(contentType).toBe("image/png");
done();
});
});

it("check file is JPG image", (done) => {
validationHelper.isFileImage("/base/test/images/access.jpg", (isImage, contentType) => {
expect(isImage).toBe(true);
expect(contentType).toBe("image/jpeg");
done();
});
});

it("check file is GIF image", (done) => {
validationHelper.isFileImage("/base/test/images/access.gif", (isImage, contentType) => {
expect(isImage).toBe(true);
expect(contentType).toBe("image/gif");
done();
});
});

it("check file is SVG image", (done) => {
validationHelper.isFileImage("/base/test/images/sun.svg", (isImage, contentType) => {
expect(isImage).toBe(true);
expect(contentType).toBe("image/svg+xml");
done();
});
});

it("check file is unsupported BMP image", (done) => {
validationHelper.isFileImage("/base/test/images/access.bmp", (isImage, contentType) => {
expect(isImage).toBe(false);
expect(contentType).toBe("image/x-ms-bmp");
done();
});
});

it("URL javascript: directive checking", () => {
expect(validationHelper.checkIsImageUrlAllowable("jAvAscrIpt:alert('XSS');")).toBe(false);
expect(validationHelper.checkIsImageUrlAllowable("jAvAscrIpt:alert('XSS');.png")).toBe(false);
expect(validationHelper.checkIsImageUrlAllowable("jaascript:alert('XSS');.png")).toBe(true);
it("check file is not image", (done) => {
validationHelper.isFileImage("/base/test/data/someplaintext.txt", (isImage, contentType) => {
expect(isImage).toBe(false);
expect(contentType).toBe("text/plain");
done();
});
});

it("URL data: directive checking", () => {
expect(validationHelper.checkIsImageUrlAllowable("data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD")).toBe(false);
expect(validationHelper.checkIsImageUrlAllowable("data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD.png")).toBe(false);
expect(validationHelper.checkIsImageUrlAllowable("dat:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD.png")).toBe(true);
it("file doesn't exist", (done) => {
validationHelper.isFileImage("/base/test/images/notexisitingimage.png", (isImage, contentType) => {
expect(isImage).toBe(false);
expect(contentType).toBeNull();
done();
});
});
});
}