Skip to content

Commit

Permalink
Checking for image URL (#14)
Browse files Browse the repository at this point in the history
* Checking for image URL
  • Loading branch information
EugeneElkin authored and ignatvilesov committed Oct 27, 2017
1 parent 59ff848 commit 9eff668
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,2 +1,5 @@
## 1.3.0
* ADD: new module validationHelper and function checkIsImageUrlAllowable for checking of images URLs

## 1.2.0
* Removed `lodash` as dependencies
5 changes: 5 additions & 0 deletions lib/index.d.ts
Expand Up @@ -60,6 +60,11 @@ declare module powerbi.extensibility.utils.dataview {
function hasImageUrlColumn(dataView: DataView): boolean;
}
}
declare module powerbi.extensibility.utils.dataview {
module validationHelper {
function checkIsImageUrlAllowable(url: string): boolean;
}
}
declare module powerbi.extensibility.utils.dataview {
import DataViewObjectPropertyIdentifier = powerbi.DataViewObjectPropertyIdentifier;
import DataView = powerbi.DataView;
Expand Down
47 changes: 47 additions & 0 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.2.0",
"version": "1.3.0",
"description": "dataviewutils",
"main": "lib/index.js",
"repository": {
Expand Down
35 changes: 35 additions & 0 deletions src/validationHelper.ts
@@ -0,0 +1,35 @@
/*
* Power BI Visualizations
*
* Copyright (c) Microsoft Corporation
* All rights reserved.
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the ""Software""), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

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);
}
}
}
58 changes: 58 additions & 0 deletions test/validationHelperTest.ts
@@ -0,0 +1,58 @@
/*
* Power BI Visualizations
*
* Copyright (c) Microsoft Corporation
* All rights reserved.
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the ""Software""), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

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("invalid URL wrong extension", () => {
expect(validationHelper.checkIsImageUrlAllowable("https://someHostsomeTestImage.exe")).toBe(false);
});

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

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("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);
});
});
}
1 change: 1 addition & 0 deletions tsconfig.json
Expand Up @@ -13,6 +13,7 @@
"src/dataViewObject.ts",
"src/dataViewObjects.ts",
"src/converterHelper.ts",
"src/validationHelper.ts",
"src/dataViewObjectsParser.ts"
]
}

0 comments on commit 9eff668

Please sign in to comment.